Creating and deleting layers in Lambda - 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).

Creating and deleting layers in Lambda

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 create and delete layers in Lambda. For more conceptual information about layers and why you might consider using them, see Working with Lambda layers.

After you’ve packaged your layer content, the next step is to create the layer in Lambda. This section demonstrates how to create and delete layers using the Lambda console or the Lambda API only. To create a layer using Amazon CloudFormation, see Using Amazon CloudFormation with layers. To create a layer using the Amazon Serverless Application Model (Amazon SAM), see Using Amazon SAM with layers.

Creating a layer

To create a layer, you can either upload the .zip file archive from your local machine or from Amazon Simple Storage Service (Amazon S3). Lambda extracts the layer contents into the /opt directory when setting up the execution environment for the function.

Layers can have one or more layer versions. When you create a layer, Lambda sets the layer version to version 1. You can change the permissions on an existing layer version at any time. However, to update the code or make other configuration changes, you must create a new version of the layer.

To create a layer (console)
  1. Open the Layers page of the Lambda console.

  2. Choose Create layer.

  3. Under Layer configuration, for Name, enter a name for your layer.

  4. (Optional) For Description, enter a description for your layer.

  5. To upload your layer code, do one of the following:

    • To upload a .zip file from your computer, choose Upload a .zip file. Then, choose Upload to select your local .zip file.

    • To upload a file from Amazon S3, choose Upload a file from Amazon S3. Then, for Amazon S3 link URL, enter a link to the file.

  6. (Optional) For Compatible architectures, choose one value or both values. For more information, see Lambda instruction set architectures (ARM/x86).

  7. (Optional) For Compatible runtimes, choose the runtimes that your layer is compatible with.

  8. (Optional) For License, enter any necessary license information.

  9. Choose Create.

Alternatively, you can also use the PublishLayerVersion API to create a layer. For example, you can use the publish-layer-version Amazon Command Line Interface (CLI) command with a name, description, and .zip file archive specified. The license info, compatible runtimes, and compatible architecture parameters are optional.

aws lambda publish-layer-version --layer-name my-layer \ --description "My layer" \ --license-info "MIT" \ --zip-file fileb://layer.zip \ --compatible-runtimes python3.10 python3.11 \ --compatible-architectures "arm64" "x86_64"

You should see output similar to the following:

{ "Content": { "Location": "https://awslambda-us-east-2-layers.s3.us-east-2.amazonaws.com/snapshots/123456789012/my-layer-4aaa2fbb-ff77-4b0a-ad92-5b78a716a96a?versionId=27iWyA73cCAYqyH...", "CodeSha256": "tv9jJO+rPbXUUXuRKi7CwHzKtLDkDRJLB3cC3Z/ouXo=", "CodeSize": 169 }, "LayerArn": "arn:aws-cn:lambda:us-west-2:123456789012:layer:my-layer", "LayerVersionArn": "arn:aws-cn:lambda:us-west-2:123456789012:layer:my-layer:1", "Description": "My layer", "CreatedDate": "2023-11-14T23:03:52.894+0000", "Version": 1, "CompatibleArchitectures": [ "arm64", "x86_64" ], "LicenseInfo": "MIT", "CompatibleRuntimes": [ "python3.10", "python3.11" ] }

Each time you call publish-layer-version, you create a new version of the layer.

Deleting a layer version

To delete a layer version, use the DeleteLayerVersion API. For example, you can use the delete-layer-version CLI command with the layer name and layer version specified.

aws lambda delete-layer-version --layer-name my-layer --version-number 1

When you delete a layer version, you can no longer configure a Lambda function to use it. However, any function that already uses the version continues to have access to it. Also, Lambda never reuses version numbers for a layer name.