How to Use AWS Lambda Layers for Efficiency

How to Use AWS Lambda Layers for Efficiency

AWS Lambda Layers is a distribution mechanism for libraries, custom runtimes, and other function dependencies in Lambda. A layer is a package of code, library dependencies, or even custom runtimes that can be reused across multiple Lambda functions. Instead of including common code in each individual Lambda function, you can place it in a layer and reference it from multiple functions.

Benefits of Using Lambda Layers

Using Lambda layers offers several benefits that can greatly enhance your serverless development workflow:[1]

  1. Simplified code management: One way to simplify your codebase and minimize duplication is by organizing common code into layers. As a result, it becomes easier to maintain and update shared code across various functions.
  2. Improved development speed: Whenever modifications are made to a layer, they automatically apply to all functions that utilize that particular layer. As a result, this feature facilitates more efficient code updates and maintenance, ultimately saving valuable time during development.
  3. Reduced deployment package size: Lambda layers play a vital role in reducing the size of deployment packages. They keep the common code separate from function-specific code, which results in smaller package sizes and a more efficient deployment process.
  4. Version control and sharing: Versioning layers simplify change management and facilitate the sharing of common libraries and dependencies among teams and projects. This fosters collaboration and guarantees uniformity of code across various functions.
  5. Cost optimization: When several functions utilize a common layer, AWS Lambda merely loads the layer once, which reduces the memory usage and execution time. Which can lead to cost savings, particularly for applications with heavy traffic or frequent function calls.

Creating a Lambda Layer

Let’s walk through the process of creating a Lambda layer using a practical example of a custom logging library.

  1. Preparing the Layer Content: Let’s imagine we created a personalized logging library that we intend to utilize in various Lambda functions. To achieve this, we arranged the library code in a directory structure, along with all the required configuration files and supplementary assets.
  2. Packaging the Layer: To produce the layer, it is necessary to bundle the contents of our personalized logging library into a ZIP file, guaranteeing that the directory configuration is preserved within the archive. Furthermore, if the library relies on other components, we bundle them in the same structure and format they would have if installed through a package manager.
  3. To upload the layer to AWS Lambda:
    • Open the AWS Lambda console and navigate to the Layers section.
    • Click on “Create Layer” and provide a name, description, and compatible runtimes for the layer.
    • Upload the ZIP archive containing the custom logging library.
    • Once uploaded, publish a new version of the layer.

Add layers to Lambda functions

Now that we have created our custom logging layer, let’s see how we can add it to multiple Lambda functions.

  1. Open the AWS Lambda console and select the function to which you want to add the layer.
  2. Scroll down to the “Layers” section and click on “Add a layer.”
  3. Choose the desired layer from the list of available layers. You can search for your custom logging layer by name or browse through the layers available in your account.
  4. If multiple versions of the layer exist, select the appropriate version.
  5. Save the changes to update the function’s configuration.

Example: Publish and Use Lambda Layer Using CLI

Let us go through the steps to publish and use a Lambda Layer using the CLI. We are assuming that you have the AWS CLI installed and authentication configured already, if not please see the AWS documentation on how to set it up.[2][3]

You’ll need to create a directory to store your layer code and dependencies. For this example, let’s call it my-layer.

mkdir my-layer
cd my-layer

Place your code and any dependencies in the my-layer directory. The structure might look like this:

└── my_dependency/

You need to package your code into a ZIP file. In the my-layer directory, run the following command:

zip -r .

Now you’ll use the AWS CLI to publish the Lambda Layer. Replace my-layer-name, description,, and my-bucket-name with your own values.

aws lambda publish-layer-version \
--layer-name my-layer-name \
--description "My Lambda Layer" \
--content S3Bucket=my-bucket-name, \
--compatible-runtimes python3.11
  • my-layer-name: The name for your Lambda Layer.
  • description: A description for your Lambda Layer.
  • my-bucket-name: Replace with your S3 bucket name where you want to store the layer package.
  • The name of the ZIP file you created.

After the publication is successful, you’ll receive an ARN (Amazon Resource Name) for your Lambda Layer. You’ll need this ARN when attaching the layer to your Lambda functions.

You can attach the created Lambda Layer to a Lambda function using the AWS Management Console or the AWS CLI. To do it with the AWS CLI:

aws lambda update-function-configuration \
--function-name YourFunctionName \
--layers arn:aws:lambda:REGION:ACCOUNT_ID:layer/my-layer-name:LAYER_VERSION
  • YourFunctionName: Replace this with the name of your Lambda function.
  • REGION: Replace with your AWS region.
  • ACCOUNT_ID: Replace with your AWS account ID.
  • my-layer-name: The name of your Lambda Layer.
  • LAYER_VERSION: The version number of your Lambda Layer.

Your Lambda Layer is now created and attached to your Lambda function.

Managing and Updating Layers

To keep your layers up to date and manage them effectively, follow these best practices:

  1. Updating a Layer: To update a layer, modify its contents, package the dependencies accordingly, and create a new ZIP archive. Make sure to maintain the directory structure and upload it as a new version of the layer.
  2. Function Versioning and Layer Compatibility: It’s important to keep a record of the layer versions utilized by each function. If you make changes to a layer, functions that are still using older versions of that layer will continue to use the previous version until you manually update them to use the latest version. Make sure that any changes made to the updated layer are compatible with the functions that reference it.
  3. Managing Layer Versions: AWS Lambda allows you to manage different versions of a layer. This lets you track changes, roll back to previous versions if needed, and maintain compatibility with existing functions.

Best Practices for Using Lambda Layers

To make the most out of Lambda layers, follow these best practices:[4]

  1. Keep layers focused: It is important to ensure that each layer serves a distinct purpose and only includes relevant code or dependencies for a specific functionality. It is recommended to avoid adding unrelated or unnecessary files to the layers.
  2. Minimize layer size: To speed up your deployment and execution times, it’s best to optimize the size of your layers. One way to do this is by removing any unnecessary files and dependencies.
  3. Test and monitor: To ensure optimal function performance, it is important to conduct a comprehensive testing of all layers and closely monitor their usage and impact. It’s also crucial to be attentive to potential issues or bottlenecks that may arise as a result of layer dependencies or compatibility.


  1. Cui, Y. (2023, September 18). AWS Lambda Use Cases: When to use Lambda layers. Lumigo. ↩︎
  2. Srivastava C. (2023, June 16). Hi, this is Charu from Classmethod and in this blog, we will walk through the process of adding lay. クラスメソッド発「やってみた」系技術メディア | DevelopersIO. ↩︎
  3. Polpagedar, S. (2023, June 16). Creating Lambda Layers Made Easy with Docker: A Developer’s Guide | Simform Engineering. Medium. ↩︎
  4. Isenberg, R. (2023, February 18). AWS Lambda Layers Best Practices. Ran The Builder. ↩︎