AWS Lambda Layers Example: A Complete Guide
AWS Lambda Layers allow developers to share common code and dependencies across multiple Lambda functions, simplifying management and reducing deployment package size. This article provides a step-by-step example of creating and using an AWS Lambda Layer, focusing on adding a shared library to multiple Lambda functions.
What Are AWS Lambda Layers?
AWS Lambda Layers are an architectural feature that helps:
- Reuse Code: Share libraries and dependencies across functions.
- Streamline Maintenance: Update shared code in one place.
- Reduce Deployment Package Size: Keep the function code lean by separating dependencies.
AWS Lambda Layers Example
Objective
We’ll create a Lambda Layer containing a Python library (requests
) and use it in multiple Lambda functions.
Step 1: Prepare the Lambda Layer
1. Create the Layer Directory
On your local machine, create the directory structure:
mkdir lambda-layer
cd lambda-layer
mkdir -p python/lib/python3.9/site-packages
Replace python3.9
with the appropriate version for your Lambda runtime.
2. Install Dependencies
Install the required library into the directory:
pip install requests -t python/lib/python3.9/site-packages
This will download the requests
library and its dependencies into the specified folder.
3. Package the Layer
Zip the layer files:
zip -r layer.zip python
Step 2: Create the Lambda Layer in AWS
- Navigate to the AWS Lambda Console:
- Go to Layers → Create Layer.
- Upload the Layer:
- Name: Enter a name (e.g.,
requests-layer
). - Description: Optionally, provide a description.
- Upload: Choose the
layer.zip
file. - Compatible Runtimes: Select Python 3.9 (or the runtime you used).
- Name: Enter a name (e.g.,
- Create the Layer:
- Click Create to finalize the layer.
Step 3: Use the Layer in a Lambda Function
1. Create a Lambda Function
- In the AWS Lambda Console, create a new function:
- Runtime: Select Python 3.9.
- Function Name: Enter a name (e.g.,
use-layer-function
).
- Attach the Layer:
- In the function’s configuration, go to Layers → Add a layer.
- Choose Custom Layers → Select the
requests-layer
you created. - Click Add.
2. Write the Function Code
Update the function code to use the requests
library from the layer:
import json
import requests
def lambda_handler(event, context):
url = "https://api.github.com"
response = requests.get(url)
return {
'statusCode': 200,
'body': json.dumps({
'message': 'Layer test successful!',
'data': response.json()
})
}
Step 4: Test the Function
- Deploy and Test:
- Deploy the function and test it with any input.
- Check the Logs:
- View the logs in CloudWatch to ensure the
requests
library was successfully loaded and used.
- View the logs in CloudWatch to ensure the
Benefits of Using Lambda Layers
- Code Reusability: Share common code across multiple Lambda functions.
- Reduced Maintenance: Update shared libraries in one layer rather than across functions.
- Optimized Deployment: Smaller function packages lead to faster deployment and execution.
Best Practices for AWS Lambda Layers
- Version Control:
- Manage versions of your layer carefully, as updates are not automatically propagated.
- Optimize Layer Size:
- Keep layers small by including only the necessary dependencies.
- Use Compatible Runtimes:
- Ensure the layer runtime matches the Lambda function runtime.
- Monitor Usage:
- Regularly check which functions use each layer to manage dependencies efficiently.
Conclusion
AWS Lambda Layers simplify code management by enabling the reuse of shared libraries and dependencies across functions. This example demonstrated how to create a Python-based Lambda Layer, attach it to a function, and use it effectively. By leveraging Lambda Layers, you can optimize function deployment, improve maintainability, and enhance development productivity.