Working with layers for Python Lambda functions (original) (raw)
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.
This topic explains how to create a Python layer and attach it to a Lambda function.
Topics
Package your layer content
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.
To create a layer using pip packages
- Choose one of the following methods to install
pip
packages into the required top-level directory (python/
):
pip install
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.
requirements.txt
Using a requirements.txt
file helps you manage package versions and ensure consistent installations.
Example requirements.txt
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.
2. Zip the contents of the python
directory.
zip -r layer.zip python/
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.`x`/site-packages
). As long as your .zip file includes the python
directory at the root level, Lambda can locate and import your packages.
To create a layer using your own code
- 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.
Example custom module: validator.py
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/
The directory structure of your .zip file should look like this:python/
# Required top-level directory
└── validator.py
4. 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\"}}"
}
Create the layer in Lambda
You can publish your layer using either the AWS CLI or the Lambda console.
AWS CLI
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.
Console
To create a layer (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.
Add the layer to your function
AWS CLI
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:`1`
). For more information, see Layers and layer versions.
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"
Console
To add a layer to a function
- 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.
Note
If 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. 5. Choose Add.
Sample app
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.