I am trying to implement this AWS Lambda Rest API Handler in my lambda code to handle proper response code. For this I needed to repackage the library aws_lambda_powertools
and add as a layer in lambda function.
All the import related to this lib below are working.
from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.event_handler import APIGatewayRestResolver
from aws_lambda_powertools.logging import correlation_paths
from aws_lambda_powertools.utilities.typing import LambdaContext
But When I am creating object of above Tracer class below its giving error(Rest two commented object logger
and app
are working fine.
tracer = Tracer()
# logger = Logger()
# app = APIGatewayRestResolver()
Error I am getting while declaring tracer object is below:
Response
{
"errorMessage": "Unable to import module 'lambda_function': No module named 'aws_xray_sdk'",
"errorType": "Runtime.ImportModuleError",
"stackTrace": []
}
Function Logs
OpenBLAS WARNING - could not determine the L2 cache size on this system, assuming 256k
START RequestId: ae8b006b-e7f7-495b-99a0-eb5231c3f81c Version: $LATEST
[ERROR] Runtime.ImportModuleError: Unable to import module 'lambda_function': No module named 'aws_xray_sdk'
Traceback (most recent call last):
I tried to install pip install aws_xray_sdk
and repackaged it and re-added to layer still its giving the same error.
Can anyone help me with this? I am new to lambda. Thanks in advance.
3
Answers
Fixed the error by using AWS Arn
arn:aws:lambda:{region}:017000801446:layer:AWSLambdaPowertoolsPythonV2:18📋
instead of using my own custom repackaged library layer.Reference Link: https://awslabs.github.io/aws-lambda-powertools-python/2.6.0/
Tracing (like validation and parsing) requires additional dependencies. They’re not included by default in an attempt to keep the resulting package as small as possible.
When packaging with AWS SAM, I’m using this in my
my_code/requirements.txt
, which I thenpip install
in my local virtual env:Additionally, I’m including this in
tests/requirements.txt
, which I alsopip install
locally but which is not picked up by SAM (keeping the image small again, and it’s not required at runtime anyhow).In version 2.0 the xray SDK is not included by default, as this would introduce a size overhead even for those who would not be using Tracer.
To solve this problem, just put in your requirements.txt the tracer dependency using aws-lambda-powertools[tracer] or all dependencies using aws-lambda-powertools[all].
Refer This link:
https://github.com/awslabs/aws-lambda-powertools-python/issues/1872