AWS Lambda layers for Python (using CDK)

What are Lambda layers?

AWS Lambda layer is a custom code package that can be shared and used across multiple lambda functions.

A lambda layer can contain libraries, custom runtimes, and other function dependencies that Lambda functions can refer to.

Why use Lambda Layers?

There are loads of advantages of using Lambda layers apart from the obvious code-reusability aspect

Code Reusability/Consistency: The first one is the obvious one. Lambda layers can contain common code that multiple Lambda functions can reference thereby reducing code duplication and ensuring consistency across functions. This not only makes the code reusable but also easier to maintain.

Dependency management: Using Lambda layers all dependencies can be bundled together and managed separately. They can include libraries, custom runtimes, or any code or assets that your Lambda functions need.

Versioning: Lambda layers has support for versioning. You can can manage different versions of the same layer. This helps in maintaining compatibility of code for older versions if there is a need.

Separation of Concerns: Lambda layers lets you separate your business logic code from dependencies/shared code. Managing and testing your code should be easier due to separation of concerns. This also simplifies the update process to your lambda functions. You can update the shared libary or the dependency without modifying the lambda functions.

Smaller Deployment Packages and faster deployment times: Layers reduce the size of the lamba functions as dependencies are isolated and not bundled with every lambda function thereby improving deployment times of individual lambda functions.

Security and Compliance: You can ensure that shared code complies to security policies and standards by centralizing dependencies through use of Lambda layers.

Using CDK to create python lambda layers

Creating a python lambda layer using CDK is really simple. The CDK API is in alpha and it has been that way for a long time, but it works very well. The is the simplest CDK code skeleton that creates a Python lambda layer

export class SharedLambdaLayer extends cdk.Stack {

  constructor(scope: Construct, id: string, props: cdk.StackProps) {
    super(scope, id, props);

    const myLambdaLayer = new pylambda.PythonLayerVersion(this, "my-lambda-layer", {
      compatibleRuntimes: [lambda.Runtime.PYTHON_3_11],
      layerVersionName: "my-lambda-layer",
      entry: "shared-code/path-to-code",
    });
  }
}

The above code creates a lambda layer with the name my-lambda-layer that uses Python 3.11 as base run-time for python.

The most important element of this code is the entry attribute that takes the relative path to the requirements.txt file. CDK looks for a requirements.txt file under the shared-code/path-to-code directory, and creates a Lambda layer bundled with the modules and dependencies specified in the requirements file

Referencing the layer in Lambda

Now, lets create a Python Lambda function that references this Lambda layer

const functionProps: lambda.FunctionProps = {
  runtime: lambda.Runtime.PYTHON_3_11,
  handler: "main.handler",
  code: lambda.Code.fromAsset(path.join(__dirname, "usecases/lambdasource")),
  environment: {
    LOGGING_LEVEL: loggingLevel,
  },
  layers: [
    lambda.LayerVersion.fromLayerVersionArn(
      this,
      "MyLambdaLayerVersion",
      `arn:aws:lambda:${this.region}:${this.account}:layer:my-lambda-layer:1`,
    ),
  ],
  timeout: cdk.Duration.seconds(240),
};

There are a few things to keep in mind. You can refer to any lambda layer not just the one deployed in your account (although in this example, we are referencing to the lambda layer deployed in the same region and account). In other words, an external lambda layer deployed in a different region and account can be referenced the same way with the Lambda layer arn.

You can reference a different version of the layer by using the layer version at the end of the Layer arn. You can also reference multiple layers from within your lambda function.

This is a simple yet powerful way of building and maintaining shared code. Although this example concerns itself with Python Lambda functions and layers, with small variations, the concept can be applied across different programming languages supported by AWS Lambda.

Author

Niranjan Manjunath