How to Use AWS Lambda Layers: Syntax, Example, and Tips
Use
Lambda layers to package and share libraries or code across multiple Lambda functions. Create a layer ZIP archive, publish it in AWS Lambda, then add the layer to your function's configuration to access its content during execution.Syntax
A Lambda layer is created by packaging your code or libraries into a ZIP file and publishing it as a layer version. Then, you add the layer to your Lambda function configuration using the Layers property. This makes the layer's content available in the /opt directory during function runtime.
- Layer ZIP file: Contains your shared code or libraries.
- Publish Layer: Upload ZIP to AWS Lambda as a layer version.
- Add Layer to Function: Reference the layer ARN in your function's
Layerslist.
bash
aws lambda publish-layer-version --layer-name my-layer --description "My shared code" --zip-file fileb://layer.zip --compatible-runtimes python3.9 aws lambda update-function-configuration --function-name my-function --layers arn:aws:lambda:region:account-id:layer:my-layer:1
Example
This example shows how to create a Lambda layer with a Python library, publish it, and use it in a Lambda function.
bash and python
mkdir python pip install requests -t python/ zip -r layer.zip python aws lambda publish-layer-version --layer-name requests-layer --description "Requests library" --zip-file fileb://layer.zip --compatible-runtimes python3.9 # Update your Lambda function to use the layer aws lambda update-function-configuration --function-name my-function --layers arn:aws:lambda:region:account-id:layer:requests-layer:1 # Lambda function code example (handler.py): import requests def lambda_handler(event, context): response = requests.get('https://example.com') return {'statusCode': response.status_code, 'body': response.text[:100]}
Output
{"statusCode": 200, "body": "<!doctype html><html>..."}
Common Pitfalls
- Not packaging the layer content in the correct folder structure (e.g., Python libraries must be inside a
python/folder). - Forgetting to specify compatible runtimes when publishing the layer.
- Not updating the Lambda function configuration to include the new layer version ARN.
- Using layers that exceed the size limit (50 MB compressed).
- Trying to use layers with incompatible runtimes or architectures.
bash
## Wrong: Packaging Python files at root instead of inside 'python/' folder zip -r layer.zip mylib.py ## Right: Packaging inside 'python/' folder mkdir python mv mylib.py python/ zip -r layer.zip python
Quick Reference
- Package shared code in a ZIP file with correct folder structure.
- Publish the ZIP as a Lambda layer with compatible runtimes.
- Add the layer ARN to your Lambda function configuration.
- Access layer content in
/optduring function execution. - Keep layer size under 50 MB compressed.
Key Takeaways
Package shared code in a ZIP file with the correct folder structure before publishing as a layer.
Publish Lambda layers specifying compatible runtimes to ensure function compatibility.
Add the layer ARN to your Lambda function configuration to use the layer content.
Access layer files inside the function at the /opt directory during runtime.
Avoid common mistakes like wrong folder structure or missing layer updates in function config.