Adding layers to functions - Amazon Lambda
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Adding layers to functions

A Lambda layer is a .zip file archive that contains supplementary code or data. Layers usually contain library dependencies, a custom runtime, or configuration files.

This section explains how to add a layer to a Lambda function. For more conceptual information about layers and why you might consider using them, see Working with Lambda layers.

Before you can configure a Lambda function to use a layer, you must:

You can add up to five layers to a Lambda function. The total unzipped size of the function and all layers cannot exceed the unzipped deployment package size quota of 250 MB. For more information, see Lambda quotas.

Your functions can continue to use any layer version that you’ve already added, even after that layer version has been deleted, or after your permission to access the layer is revoked. However, you cannot create a new function that uses a deleted layer version.

Note

Make sure that the layers you add to a function are compatible with the runtime and instruction set architecture of the function.

To add a layer to a function (console)
  1. Open the Functions page of the Lambda console.

  2. Choose the function to configure.

  3. Under Layers, choose Add a layer

  4. Under Choose a layer, choose a layer source:

    1. For the Amazon layers or Custom layers layer sources, choose a layer from the pull-down menu. Under Version, choose a layer version from the pull-down menu.

    2. For the Specify an ARN layer source, enter an ARN in the text box and choose Verify. Then, choose Add.

The order in which you add the layers is the order in which Lambda merges the layer content into the execution environment. You can change the layer merge order using the console.

To update layer merge order for your function (console)
  1. Open the Functions page of the Lambda console.

  2. Choose the function to configure.

  3. Under Layers, choose Edit

  4. Choose one of the layers.

  5. Choose Merge earlier or Merge later to adjust the order of the layers.

  6. Choose Save.

Layers are versioned. The content of each layer version is immutable. The owner of a layer can release new layer versions to provide updated content. You can use the console to update the layer version attached to your functions.

To update layer versions for your function (console)
  1. Open the Layers page of the Lambda console.

  2. Choose the layer you want to update the version for.

  3. Choose the Functions using this version tab.

  4. Choose the functions you want to modify, then choose Edit.

  5. For Layer version, choose the layer version to change to.

  6. Choose Update functions.

You cannot update function layer versions across Amazon accounts.

Accessing layer content from your function

If your Lambda function includes layers, Lambda extracts the layer contents into the /opt directory in the function execution environment. Lambda extracts the layers in the order (low to high) listed by the function. Lambda merges folders with the same name. If the same file appears in multiple layers, the function uses the version in the last extracted layer.

Each Lambda runtime adds specific /opt directory folders to the PATH variable. Your function code can access the layer content without having to specify the path. For more information about path settings in the Lambda execution environment, see Defined runtime environment variables.

Refer to Layer paths for each Lambda runtime to learn where to include your libraries when creating a layer.

If you’re using a Node.js or Python runtime, you can use the built-in code editor in the Lambda console. You should be able to import any library that you’ve added as a layer to the current function.

Finding layer information

To find layers in your account that are compatible with your function’s runtime, use the ListLayers API. For example, you can use the following list-layers Amazon Command Line Interface (CLI) command:

aws lambda list-layers --compatible-runtime python3.9

You should see output similar to the following:

{ "Layers": [ { "LayerName": "my-layer", "LayerArn": "arn:aws-cn:lambda:us-west-2:123456789012:layer:my-layer", "LatestMatchingVersion": { "LayerVersionArn": "arn:aws-cn:lambda:us-west-2:123456789012:layer:my-layer:2", "Version": 2, "Description": "My layer", "CreatedDate": "2023-11-15T00:37:46.592+0000", "CompatibleRuntimes": [ "python3.9", "python3.10", "python3.11", ] } } ] }

To list all layers in your account, omit the --compatible-runtime option. The response details show the latest version of each layer.

You can also get the latest version of a layer using the ListLayerVersions API. For example, you can use the following list-layer-versions CLI command:

aws lambda list-layer-versions --layer-name my-layer

You should see output similar to the following:

{ "LayerVersions": [ { "LayerVersionArn": "arn:aws-cn:lambda:us-west-2:123456789012:layer:my-layer:2", "Version": 2, "Description": "My layer", "CreatedDate": "2023-11-15T00:37:46.592+0000", "CompatibleRuntimes": [ "java11" ] }, { "LayerVersionArn": "arn:aws-cn:lambda:us-west-2:123456789012:layer:my-layer:1", "Version": 1, "Description": "My layer", "CreatedDate": "2023-11-15T00:27:46.592+0000", "CompatibleRuntimes": [ "java11" ] } ] }