How to add layers in the Lambda function

Key takeaways:

  • AWS Lambda runs code without managing servers, simplifying deployment.

  • Lambda layers add external libraries, tools, or custom runtimes to functions.

  • Layers improve reusability, reduce package size, and simplify updates.

  • They can be created using AWS CLI or AWS Management Console with runtime and architecture compatibility.

  • Layers decouple dependencies from code, making maintenance easier.

  • They optimize execution environments for specific use cases, enhancing efficiency.

AWS Lambda is a powerful tool that allows us to run our code without the hassle of managing the computers it operates on. When we use AWS Lambda, all we need to do is write our code, and Lambda takes care of the rest, such as deciding the best place and method to run it. However, sometimes, our code requires additional components, like specific tools or libraries, that aren’t included in the standard AWS Lambda setup. In these situations, Lambda layers prove to be extremely useful.

Understanding Lambda layers

Lambda layers are essentially add-on features that enhance the functionality of our AWS Lambda functions. They are similar to zip files and are packed with various elements that our Lambda function might need. Here’s what they can include:

  1. Libraries: Our Lambda function might require additional code libraries to function correctly. Using Lambda layers, we can easily add these libraries to our function, ensuring it has all the necessary components to execute successfully.

  2. Custom runtimes: Sometimes, our Lambda function might need to run in a specific programming environment that isn’t available by default in AWS Lambda. Lambda layers allow us to incorporate these custom runtimes, giving us the flexibility to use the programming environment that our project demands.

  3. Configuration files: These files are crucial as they contain settings and instructions that guide our Lambda function. They dictate how the function should operate and can be used to fine-tune its behavior to suit our specific needs.

Lambda layer architecture diagram
Lambda layer architecture diagram

Create a Lambda layer package

Let’s say we need an image processing library in Lambda to process the images, but the default environment doesn’t provide it. We can create a Lambda layer with an image-processing pillow library. We can use the following commands to create the Lambda layer package:

  • Create a PIL_Layer named folder and go to this folder on our system. Here, create the python named folder and install the pillow library.

mkdir -p PIL_Layer/python
cd PIL_Layer
pip install pillow -t python/
  • Execute the following command to compress the contents into a .zip file:

zip -r pillow_layer.zip python/

We’ve successfully created the pillow library’s package for the Lambda layer.

AWS imposes a size limit of 50 MB (zipped) or 250 MB (unzipped) per layer.

Deploy a Lambda layer

There are different ways to upload the Lambda layer packages to our created Lambda function.

Upload Lambda layer package using CLI

The Lambda layer package can be uploaded using the CLI. We can execute the following command to create the “pillow_layer” with the package we’ve created with the pillow library.

Note: AWS credentials should be configured before executing this command.

aws lambda publish-layer-version --layer-name pillow_layer --zip-file fileb://pillow_layer.zip --compatible-runtimes python3.13 --no-cli-pager

This command is used to publish a new version of a Lambda layer using the AWS Command Line Interface (CLI). It creates a layer named pillow_layer with the contents of the pillow_layer.zip file. The layer is specified as compatible with the Python 3.13 runtime. The --no-cli-pager option ensures the output is displayed directly in the command line window without invoking a pager, like “less” or “more,” which is useful for scripting or automation.

Upload Lambda layer package using AWS Management Console

The Lambda layer package can be uploaded using the AWS Management Console. Follow these steps to add the layer to the Lambda function:

  • Go to the “Lambda” dashboard.

  • Click the “Layers” option in the “Additional resources” section in the left navigation bar.

  • Click the “Create layer” button, and it’ll open a new page.

  • Enter “pillow_layer” in the “Name” field. In the “Description-optional” field, add the description of our choice.

  • Ensure that the “Upload a .zip file” option is selected, and then click the “Upload” button. Now, select the “pillow_layer.zip” file we recently created.

  • Select the “x86_64” option in the “Compatible architectures-optional” field.

  • Select the “Python 3.13” option in the “Compatible runtimes” field.

  • Keep the rest of the fields as it is, and click the “Create” button to create the layer for the Lambda function.

We’ve successfully created the Lambda layer. Now, we can attach it to a Lambda function and start using the pillow library in the Lambda function code.

Benefits of AWS Lambda layers

Here are some benefits of using Lambda layers:

Benefit

Description

Code Reusability

Enables the reuse of code across multiple Lambda functions.

Simplified Dependencies

Allows us to manage external libraries and dependencies separately from our Lambda function code, simplifying deployment and updates.

Easier Updates and Maintenance

Updating a shared library or dependency can be done in a single layer, rather than updating each Lambda function individually.

Reduced Deployment Package Size

By removing the need to include common libraries in every function's deployment package, the overall size of each deployment is reduced, leading to faster deployments.

Decoupling of Code and Dependencies

Layers allow for a clear separation between our application code and its dependencies, enhancing modularity and maintainability.

Optimized Execution Environment

By including additional binaries and custom runtime interfaces, layers can help optimize the Lambda execution environment for specific needs.

Try it yourself

Click the terminal given below to connect it and execute the following command:

aws configure

You need to provide your AWS credentials to configure AWS. After executing the above command, provide the following:

  • Enter your AWS Access Key ID.

  • Enter your AWS Secret Access Key.

  • Enter your preferred region, such as us-east-1 or us-west-2.

  • In the output format, you can leave this as the default json or choose table/text.

Terminal 1
Terminal
Loading...

Conclusion

AWS Lambda layers make developing easier and faster by turning common tools and dependencies into reusable parts. This approach helps in managing and deploying code more efficiently and allows it to fit various programming needs without overcomplicating the main function code. Developers get a strong tool in Lambda layers, ensuring necessary components like libraries and settings are included cleanly and effectively.

To learn how to create and use Lambda layers effectively, explore this Cloud Lab: Mastering AWS Lambda Layers.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


Why should we use layers in Lambda?

AWS Lambda layers allow you to package and share common libraries, dependencies, and custom runtimes across multiple Lambda functions. This improves code reusability, reduces deployment package size, simplifies updates and maintenance, and decouples dependencies from the main function code, leading to cleaner and more modular architecture.


How to add node modules to Lambda layer

  1. Create a folder structure:
    Create a directory, e.g., nodejs, and install the required Node.js modules into it using:
    mkdir nodejs && cd nodejs
    npm install <module_name>
    
  2. Package the layer:
    Compress the folder into a .zip file:
    zip -r layer.zip nodejs
    
  3. Deploy the layer:
    Use the AWS CLI or AWS Management Console to upload the .zip file and publish it as a new layer.

How large can a lambda layer be?

A Lambda layer can have a maximum size of 50 MB (zipped) or 250 MB (unzipped). This limit applies to the combined size of all files within the layer.


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved