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 Managing Lambda dependencies with layers.
Before you can configure a Lambda function to use a layer, you must:
-
Make sure that you have permission to call the GetLayerVersion API on the layer version. For functions in your AWS account, you must have this permission in your user policy. To use a layer in another account, the owner of that account must grant your account permission in a resource-based policy. For examples, see Granting Lambda layer access to other accounts.
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)
Open the Functions page
of the Lambda console. -
Choose the function to configure.
-
Under Layers, choose Add a layer
-
Under Choose a layer, choose a layer source:
-
For the AWS 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.
-
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)
Open the Functions page
of the Lambda console. -
Choose the function to configure.
-
Under Layers, choose Edit
-
Choose one of the layers.
-
Choose Merge earlier or Merge later to adjust the order of the layers.
-
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)
-
Open the Layers page
of the Lambda console. -
Choose the layer you want to update the version for.
-
Choose the Functions using this version tab.
-
Choose the functions you want to modify, then choose Edit.
-
For Layer version, choose the layer version to change to.
-
Choose Update functions.
You cannot update function layer versions across AWS 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
AWS 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:lambda:us-east-2:123456789012:layer:my-layer", "LatestMatchingVersion": { "LayerVersionArn": "arn:aws:lambda:us-east-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:lambda:us-east-2:123456789012:layer:my-layer:2", "Version": 2, "Description": "My layer", "CreatedDate": "2023-11-15T00:37:46.592+0000", "CompatibleRuntimes": [ "java11" ] }, { "LayerVersionArn": "arn:aws:lambda:us-east-2:123456789012:layer:my-layer:1", "Version": 1, "Description": "My layer", "CreatedDate": "2023-11-15T00:27:46.592+0000", "CompatibleRuntimes": [ "java11" ] } ] }