Use Lambda layers to package code and dependencies that you want to reuse across multiple functions. Layers usually contain library dependencies, a custom runtime, or configuration files. Creating a layer involves three general steps:
Package your layer content. This means creating a .zip file archive that contains the dependencies you want to use in your functions.
Create the layer in Lambda.
Add the layer to your functions.
To create a layer, bundle your packages into a .zip file archive that meets the following requirements:
Build the layer using the same Python version that you plan to use for the Lambda function. For example, if you build your layer using Python 3.13, use the Python 3.13 runtime for your function.
Your .zip file must include a python
directory at the root level.
The packages in your layer must be compatible with Linux. Lambda functions run on Amazon Linux.
You can create layers that contain either third-party Python libraries installed with pip
(such as requests
or pandas
) or your own Python modules and packages.
Choose one of the following methods to install pip
packages into the required top-level directory (python/
):
For pure Python packages (like requests or boto3):
pip install requests -t python/
Some Python packages, such as NumPy and Pandas, include compiled C components. If you're building a layer with these packages on macOS or Windows, you might need to use this command to install a Linux-compatible wheel:
pip install numpy --platform manylinux2014_x86_64 --only-binary=:all: -t python/
For more information about working with Python packages that contain compiled components, see Creating .zip deployment packages with native libraries.
Using a requirements.txt
file helps you manage package versions and ensure consistent installations.
requests==2.31.0
boto3==1.37.34
numpy==1.26.4
If your requirements.txt
file includes only pure Python packages (like requests or boto3):
pip install -r requirements.txt -t python/
Some Python packages, such as NumPy and Pandas, include compiled C components. If you're building a layer with these packages on macOS or Windows, you might need to use this command to install a Linux-compatible wheel:
pip install -r requirements.txt --platform manylinux2014_x86_64 --only-binary=:all: -t python/
For more information about working with Python packages that contain compiled components, see Creating .zip deployment packages with native libraries.
Zip the contents of the python
directory.
zip -r layer.zip python/
Compress-Archive -Path .\python -DestinationPath .\layer.zip
The directory structure of your .zip file should look like this:
python/
# Required top-level directory
âââ requests/
âââ boto3/
âââ numpy/
âââ (dependencies of the other packages)
Note
If you use a Python virtual environment (venv) to install packages, your directory structure will be different (for example, python/lib/python3.
). As long as your .zip file includes the x
/site-packagespython
directory at the root level, Lambda can locate and import your packages.
Create the required top-level directory for your layer:
mkdir python
Create your Python modules in the python
directory. The following example module validates orders by confirming that they contain the required information.
import json
def validate_order(order_data):
"""Validates an order and returns formatted data."""
required_fields = ['product_id', 'quantity']
# Check required fields
missing_fields = [field for field in required_fields if field not in order_data]
if missing_fields:
raise ValueError(f"Missing required fields: {', '.join(missing_fields)}")
# Validate quantity
quantity = order_data['quantity']
if not isinstance(quantity, int) or quantity < 1:
raise ValueError("Quantity must be a positive integer")
# Format and return the validated data
return {
'product_id': str(order_data['product_id']),
'quantity': quantity,
'shipping_priority': order_data.get('priority', 'standard')
}
def format_response(status_code, body):
"""Formats the API response."""
return {
'statusCode': status_code,
'body': json.dumps(body)
}
Zip the contents of the python
directory.
zip -r layer.zip python/
Compress-Archive -Path .\python -DestinationPath .\layer.zip
The directory structure of your .zip file should look like this:
python/
# Required top-level directory
âââ validator.py
In your function, import and use the modules as you would with any Python package. Example:
from validator import validate_order, format_response
import json
def lambda_handler(event, context):
try:
# Parse the order data from the event body
order_data = json.loads(event.get('body', '{}'))
# Validate and format the order
validated_order = validate_order(order_data)
return format_response(200, {
'message': 'Order validated successfully',
'order': validated_order
})
except ValueError as e:
return format_response(400, {
'error': str(e)
})
except Exception as e:
return format_response(500, {
'error': 'Internal server error'
})
You can use the following test event to invoke the function:
{
"body": "{\"product_id\": \"ABC123\", \"quantity\": 2, \"priority\": \"express\"}"
}
Expected response:
{
"statusCode": 200,
"body": "{\"message\": \"Order validated successfully\", \"order\": {\"product_id\": \"ABC123\", \"quantity\": 2, \"shipping_priority\": \"express\"}}"
}
You can publish your layer using either the AWS CLI or the Lambda console.
Run the publish-layer-version AWS CLI command to create the Lambda layer:
aws lambda publish-layer-version --layer-name my-layer
--zip-file fileb://layer.zip --compatible-runtimes python3.13
The compatible runtimes parameter is optional. When specified, Lambda uses this parameter to filter layers in the Lambda console.
Open the Layers page of the Lambda console.
Choose Create layer.
Choose Upload a .zip file, and then upload the .zip archive that you created earlier.
(Optional) For Compatible runtimes, choose the Python runtime that corresponds to the Python version you used to build your layer.
Choose Create.
To attach the layer to your function, run the update-function-configuration AWS CLI command. For the --layers
parameter, use the layer ARN. The ARN must specify the version (for example, arn:aws:lambda:us-east-1:123456789012:layer:my-layer:
). For more information, see Layers and layer versions.1
aws lambda update-function-configuration --function-name my-function
--cli-binary-format raw-in-base64-out --layers "arn:aws:lambda:us-east-1:123456789012:layer:my-layer:1
"
The cli-binary-format option is required if you're using AWS CLI version 2. To make this the default setting, run aws configure set cli-binary-format raw-in-base64-out
. For more information, see AWS CLI supported global command line options in the AWS Command Line Interface User Guide for Version 2.
Open the Functions page of the Lambda console.
Choose the function.
Scroll down to the Layers section, and then choose Add a layer.
Under Choose a layer, select Custom layers, and then choose your layer.
NoteIf you didn't add a compatible runtime when you created the layer, your layer won't be listed here. You can specify the layer ARN instead.
Choose Add.
For more examples of how to use Lambda layers, see the layer-python sample application in the AWS Lambda Developer Guide GitHub repository. This application includes two layers that contain Python libraries. After creating the layers, you can deploy and invoke the corresponding functions to confirm that the layers work as expected.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4