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'"
CodePudding user response:
Putting the lambda layer in a subfolder 'python' solved this issue:
/---lambda-functions/
/---shared-layer/
/---python/
boto3/
my_shared_functions.py
...etc
I was running on the assumption that the folder structure for CDK was somehow different to uploading a layer manually.
CodePudding user response:
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.
import * as lambda from 'aws-cdk-lib/aws-lambda';
import * as pylambda from "@aws-cdk/aws-lambda-python-alpha";
const layerForCommonCode = new pylambda.PythonLayerVersion(
this,
"python-lambda-layer-for-common",
{
layerVersionName: "python-lambda-layer-for-common",
entry: "../lambda-source/common-layer",
compatibleRuntimes: [lambda.Runtime.PYTHON_3_9],
}
);
The lambda-source structure as follows
--lambda-source
--common-layer
--requirements.txt
commonfiles.py