Skip to content

Conversation

@PapaPedro
Copy link
Contributor

@PapaPedro PapaPedro commented Sep 6, 2021

When running on lambda functions, the CodeGuru Profiler module should be
loaded by lambda bootstrap framework instead of the customer's code.
Then we load the customer's handler function by calling the bootstarp
code directly.

We currently handle python 3.8, 3.7 and 3.6 (with some tweeks), however
for python 3.9 the lambda bootstrap module is now delegating to the
runtime interface client
so our code must be changed to also work with this.

This changes moves the code about finding the proper function that
can load a handler function into a separate method. Unit tests are
updated to cope with this.

Issue #, if available:

Tests

  • First I ran the content of lambda_handler.py code into a lambda function and added some prints to check that the imports were working on these runtimes: 3.6, 3.7, 3.8 and 3.9
  • Then I released the agent layer in my own account and used it in a hello world lambda and checked that profiler was started and lambda function was executed properly. I tested with the layer onboarding for all runtimes (3.6, 3.7, 3.8 and 3.9) and tested the exec wrapper method for 3.8 and 3.9
  • I did NOT do any tests for the one click solution as I cannot use a customer layer for this anyway.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

When running on lambda functions, the CodeGuru Profiler module should be
loaded by lambda bootstrap framework instead of the customer's code.
Then we load the customer's handler function by calling the bootstarp
code directly.

We currently handle python 3.8, 3.7 and 3.6 (with some tweeks), however
for python 3.9 the lambda bootstrap module is now delegating to the
[runtime interface client](https://github.com/aws/aws-lambda-python-runtime-interface-client)
so our code must be changed to also work with this.

This changes moves the code about finding the proper function that
can load a handler function into a separate method. Unit tests are
updated to cope with this.
try:
return importlib.import_module(module_name)
except ModuleNotFoundError:
return None
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we log the error at least at debug level?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could but I do not want every customer using python <3.9 (most customers) to see a log about not being able to find a module. Also I removed logger which was not being used in this code, originally I was afraid to use the logger here as I was not sure the lambda framework is already setup enough at this point in the process.

return ric_bootstrap_module._get_handler

# If no RIC module is available there should be a bootstrap module available
# do not catch ModuleNotFoundError exceptions here as we cannot do anything if this fails.
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand this comment and its place here. Should this be moved in the _try_to_load_module method?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

basically I first try to load module awslambdaric.bootstrap and if that is not available I try to load bootstrap.
While the first one is never available for lambdas with runtime < 3.9 (at the moment), the other should always be there. So I use _try_to_load_module to get the first one but for the second I do import_module directly, not trying to catch any exception if it fails.

Copy link
Contributor

@mirelap-amazon mirelap-amazon left a comment

Choose a reason for hiding this comment

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

LGTM, minor comments. Can you add in the description how did you test this please? Thanks.

@PapaPedro PapaPedro merged commit 4da26bb into main Sep 10, 2021
@PapaPedro PapaPedro deleted the python39_bootstrap branch September 10, 2021 07:38
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.

2 participants