How do I create a custom Lambda layer for python runtime using CDK?
Javascript CDK code for defining the lambda layer & function:
this.sharedLayer = new lambda.LayerVersion(this, 'shared-layer', {
code: lambda.Code.fromAsset('./lambda-functions/shared-layer'),
compatibleRuntimes: [lambda.Runtime.PYTHON_3_8],
layerVersionName: 'shared-layer',
})
}
this.testFunction = new lambda.Function(this, 'TestFunction', {
runtime: lambda.Runtime.PYTHON_3_8,
handler: 'function.lambda_handler',
code: lambda.Code.fromAsset('./lambda-functions/test'),
layers: [this.sharedLayer]
})
The actual Lambda function contains a direct import of .py file in the shared-layer folder, like this:
import my_shared_functions
The Python layer folder in ./lambda-functions/shared-layer contains:
/---lambda-functions/
/---shared-layer/
boto3/
my_shared_functions.py
...etc
Generate the template file:
cdk synth --no-staging my-lambda-stack > template.yml
Build and test locally using SAM:
sam build TestFunction && sam local invoke --profile siri-dev HeartbeatFunction
Error:
"Unable to import module 'function': No module named 'my_shared_functions'"
2
Answers
Putting the lambda layer in a subfolder 'python' solved this issue:
I was running on the assumption that the folder structure for CDK was somehow different to uploading a layer manually.
if you are using CDK V2 use the @aws-cdk/aws-lambda-python-alpha package. Managing the python dependencies becomes easier using this package.
Please check below code, the aws-lambda-python-alpha uses the docker containers under hood to create the package.