0
0
AwsHow-ToBeginner · 4 min read

How to Create AWS Lambda Layer: Step-by-Step Guide

To create a AWS Lambda layer, package your libraries or code into a ZIP file with the correct folder structure, then use the AWS CLI or Console to publish the layer. Attach the layer to your Lambda function to share code across multiple functions easily.
📐

Syntax

Creating a Lambda layer involves packaging your code or libraries in a ZIP file with a specific folder structure, then publishing it using AWS CLI or Console.

  • Folder structure: Place your files inside a folder named python (for Python) or nodejs (for Node.js) at the root of the ZIP.
  • Publish command: Use aws lambda publish-layer-version with parameters for layer name, description, and ZIP file.
  • Attach layer: Add the layer ARN to your Lambda function configuration.
bash
aws lambda publish-layer-version --layer-name my-layer --description "My Lambda layer" --zip-file fileb://layer.zip --compatible-runtimes python3.9 nodejs18.x
💻

Example

This example shows how to create a Lambda layer with a Python package, publish it, and attach it to a Lambda function.

bash
# Step 1: Create folder structure
mkdir -p python/lib/python3.9/site-packages

# Step 2: Install package locally into folder
pip install requests -t python/lib/python3.9/site-packages/

# Step 3: Zip the folder
zip -r layer.zip python

# Step 4: Publish the layer
aws lambda publish-layer-version --layer-name requests-layer --description "Requests library" --zip-file fileb://layer.zip --compatible-runtimes python3.9

# Step 5: Attach layer to Lambda function (replace ARNs and names)
aws lambda update-function-configuration --function-name my-function --layers arn:aws:lambda:region:account-id:layer:requests-layer:1
Output
LayerVersionArn: arn:aws:lambda:region:account-id:layer:requests-layer:1 Function updated successfully
⚠️

Common Pitfalls

  • Wrong folder structure: The ZIP must have the correct folder like python or nodejs at root, or Lambda won't find your code.
  • Incorrect runtime compatibility: Specify the correct runtimes when publishing, or the layer won't attach.
  • Large layer size: Keep ZIP size under 50 MB compressed to avoid deployment errors.
  • Not updating function configuration: After publishing, you must update your Lambda function to use the new layer version.
bash
Wrong ZIP structure example:
zip -r layer.zip lib/python3.9/site-packages

Right ZIP structure example:
zip -r layer.zip python

Wrong publish command:
aws lambda publish-layer-version --layer-name my-layer --zip-file fileb://layer.zip

Right publish command:
aws lambda publish-layer-version --layer-name my-layer --zip-file fileb://layer.zip --compatible-runtimes python3.9
📊

Quick Reference

Remember these key points when creating Lambda layers:

  • Use correct folder structure inside ZIP (python or nodejs).
  • Specify compatible runtimes during publishing.
  • Keep layer size under AWS limits.
  • Update Lambda function configuration to use the new layer version.

Key Takeaways

Package your code in a ZIP with the correct folder structure like 'python' or 'nodejs' at root.
Publish the layer using AWS CLI with compatible runtimes specified.
Attach the published layer ARN to your Lambda function configuration.
Keep the layer size within AWS limits to avoid deployment failures.
Always update your Lambda function after publishing a new layer version.