Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Move boto3 client creation to a computed property #1059

Merged
merged 5 commits into from
May 23, 2019

Conversation

jfuss
Copy link
Contributor

@jfuss jfuss commented Mar 13, 2019

Issue #, if available:
#885

Description of changes:

Checklist:

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.


@property
def lambda_client(self):
return self._lambda_client or boto3.client('lambda')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm ok with the change, just curious if it will change our failure mode scenarios anywhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. This just means we create the default boto3 client when the property is accessed instead of on creation. This will mean we will be creating a new boto3 client on every access though. Maybe this is a even better way?

 self._lambda_client = self._lambda_client or boto3.client('lambda')
 return self._lambda_client

@jfuss jfuss force-pushed the computed-property-for-boto3 branch from 7cac013 to 73c20a6 Compare May 21, 2019 21:12
@jfuss
Copy link
Contributor Author

jfuss commented May 23, 2019

Finally had some time to dig into the build failures. Turns out, boto3 sessions are not thread safe.

LOG.debug("Loading AWS credentials from session with profile '%s'", self.aws_profile)
# boto3.session.Session is not thread safe. To ensure we do not run into a race condition with start-lambda
# or start-api, we create the session object here on every invoke.
session = boto3.session.Session(profile_name=self.aws_profile, region_name=self.aws_region)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we explicitly call out new session in debug statement as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the customer, it really doesn't matter. I don't think a debug statement for this makes sense.

For further context, this is how we did it prior to the work we did on Layers. We got fancy in using the default session. Turns out that only really worked because we were calling boto3.client. Once we moved this call to be computed, I was seeing sporadic failures on a KeyError within botocore. So I reverted us back to our previous state, which ensures Sessions get created per invoke. Which means for start-lambda and start-api that could be per request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants