1

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 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Layer path for each runtime: docs.aws.amazon.com/lambda/latest/dg/…
1

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

1 Comment

Thanks Vikram, I'm sure this will be helpful for some people. I'd use this construct if it wasn't still in Alpha. Strange how it's been moved to alpha in cdkv2, but not in v1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.