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'"