aws / aws-xray-sdk-python

AWS X-Ray SDK for the Python programming language
Apache License 2.0
327 stars 143 forks source link

[ERROR] Runtime.ImportModuleError: Unable to import module 'lambda_function': No module named 'aws_xray_sdk' #187

Open PedroMBPinheiro opened 4 years ago

PedroMBPinheiro commented 4 years ago

Hi,

I am using AWS Codestar to develop a python lambda function.

I added X-Ray support following https://docs.aws.amazon.com/codestar/latest/userguide/customize-project-xray.html and added pip install aws-xray-sdk to my buildspec.yml

  # Upgrade AWS CLI to the latest version
  - pip install --upgrade awscli

  # Upgrade AWS X-Ray SDK
  - pip install --upgrade aws-xray-sdk

Add instrumented my code as per https://docs.aws.amazon.com/lambda/latest/dg/python-tracing.html.

But CloudWatch Logs always show this error.

START RequestId: f785062e-6687-496a-91a9-653e0127d2d0 Version: 8

[ERROR] Runtime.ImportModuleError: Unable to import module 'lambda/s3_trigger_start_notebook_function': No module named 'aws_xray_sdk'

END RequestId: f785062e-6687-496a-91a9-653e0127d2d0

REPORT RequestId: f785062e-6687-496a-91a9-653e0127d2d0 Duration: 13.87 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 69 MB Init Duration: 293.24 ms
XRAY TraceId: 1-5de586ab-3073f132c67bc0163d18fd9b SegmentId: 1ebd10451b253bf7 Sampled: true

There is no requirements.txt by default in CodeStar projects. What is missing here?

willarmiros commented 4 years ago

Hi @PedroMBPinheiro, Are you sure that running pip install through your buildspec.yml will actually work in Lambda? I wasn't able to find much public documentation about Code Suite's integration with Lambda, so the buildspec.yml might not be compatible with serverless environments. I would recommend bundling the X-Ray SDK package with your application code manually and uploading that bundle to Lambda. Alternatively, you could place the X-Ray SDK into a Lambda layer and include that layer in your Lambda function.

PedroMBPinheiro commented 4 years ago

Hi @willarmiros,

By following https://docs.aws.amazon.com/codestar/latest/userguide/customize-project-xray.html I can enable XRay to work for the default segments and subsegments, but it does not accept me adding my own subsegments in code since it does not recognize the module.

If there is an official way to enable XRay in CodeStar, it should also accept custom subsegments in code, and avoid all the extra packaging and layer.

It would be nice to have this by default, in a PFR.

willarmiros commented 4 years ago

The documentation there does describe the official way of enabling X-Ray in CodeStar. It allows CodeStar to automatically generate and send segments and subsegments to X-Ray's backend without any further action from the customer and without forcing customers who are satisfied with the generated segments to add a dependency on the aws-xray-sdk.

This integration, therefore, is independent of adding the SDK to your project to generate custom subsegments and segments. As far as I know, you do still need to bundle the SDK with your Lambda code as I described above.

What do you mean by a PFR?

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs in next 7 days. Thank you for your contributions.