Step-by-Step Guide to Creating an AWS Lambda Function Layer

Learn to create an AWS Lambda layer for easy code sharing across functions in this quick guide.

Published Mar 1, 2024
Are you looking to enhance your AWS Lambda functions with additional libraries or custom code? Creating a Python Lambda Layer is an efficient way to include these dependencies. In this blog post, we'll guide you through the process of creating and deploying a Python Lambda Layer.
What is a Lambda Layer?
A Lambda Layer is a .zip file archive that can contain additional code or libraries for AWS Lambda functions. It allows you to separate your function's logic from its dependencies, making your Lambda functions more organized and manageable.
Why Use Lambda Layers?
  • Reusability: Layers can be shared across multiple Lambda functions.
  • Simplicity: Keeps your Lambda deployment package small and straightforward.
  • Efficiency: Reduces the time required to package and deploy Lambda functions.
  • An AWS account
  • AWS CLI installed and configured
  • Basic knowledge of AWS Lambda
Step 1: Prepare Your Dependencies
First, you need to gather the Python packages or libraries you want to include in your layer. For this example, let's assume you want to add the `requests` library.
1. From your local PC, Create a directory for your layer:
mkdir python
cd python
2. Install the library inside this directory:
pip install requests -t .
3. Your directory structure should look like this:
└── requests/
Step 2: Package Your Layer
Zip the contents of the python directory. This step varies slightly depending on your operating system:
On Unix-like systems:
zip -r my_lambda_layer.zip .
On Windows:
Compress the python folder to a zip file named my_lambda_layer.zip .
Ensure that the zip command is run inside the python directory.
Step 3: Deploy Your Layer
Now, upload the layer to AWS Lambda:
1. Open the AWS Lambda console.
2. Choose Layers in the left navigation pane.
3. Click "Create layer".
4. Provide a name for your layer (e.g., `my-python-requests-layer`).
5. Upload your my_lambda_layer.zip file.
6. Choose the compatible runtime (e.g., Python 3.8).
7. Click Create.
Step 4: Attach the Layer to Your Lambda Function
Finally, attach the newly created layer to your Lambda function:
1. Open your Lambda function in the AWS console.
2. Scroll to the "Layers" section in the function's designer.
3. Click "Add a layer".
4. Select "Custom layers" and choose the layer you created.
5. Click "Add".
Happy coding, and may your serverless journey be efficient and enjoyable!

1 Comment