Skip to main content

AWS Lambda Layers: Size Limits, Best Practices & Setup Guide

Lambda Layers let you share libraries, binaries, and utilities across functions without bloating every deployment package. This guide covers size limits, creating layers, sharing them across functions and accounts, and when Container Images are the better choice.

โœ“ 250MB unzipped / 5 layer limits โœ“ Creating and publishing layers โœ“ Sharing across accounts โœ“ Layers vs Container Images

Lambda Size Limits Summary

Limit Type Limit Notes
Deployment package (unzipped)250MBIncludes function code + all layers combined
Deployment package (zipped, direct)50MBFor direct upload via CLI/console
Deployment package (via S3)250MB zippedUse S3 for large packages
Layers per function5All layers share the 250MB unzipped budget
Layer ZIP (direct upload)50MBUpload larger layers via S3
Container image10GBBypasses all ZIP limits

Creating a Lambda Layer

# Python layer: package dependencies correctly
# Layer structure: python/lib/python3.12/site-packages/
mkdir -p layer/python/lib/python3.12/site-packages
pip install requests boto3 pydantic \
  --target layer/python/lib/python3.12/site-packages \
  --platform manylinux2014_x86_64 \
  --only-binary=:all:

# Create ZIP
cd layer && zip -r ../my-deps-layer.zip python/
cd ..

# Check size
ls -lh my-deps-layer.zip  # Must be under 50MB for direct upload

# Publish layer
aws lambda publish-layer-version \
  --layer-name my-python-deps \
  --zip-file fileb://my-deps-layer.zip \
  --compatible-runtimes python3.12 python3.11 \
  --description "Common Python dependencies"

# Get layer ARN from output:
# "LayerVersionArn": "arn:aws:lambda:us-east-1:123456789:layer:my-python-deps:1"

Attaching Layers to Functions

# Serverless Framework โ€” attach layers
functions:
  api:
    handler: src/handler.handler
    layers:
      - arn:aws:lambda:us-east-1:123456789:layer:my-python-deps:1
      - arn:aws:lambda:us-east-1:123456789:layer:chromium:14

# AWS CDK โ€” attach layers
const depsLayer = lambda.LayerVersion.fromLayerVersionArn(
  this,
  'DepsLayer',
  'arn:aws:lambda:us-east-1:123456789:layer:my-python-deps:1'
);

const fn = new lambda.Function(this, 'ApiFunction', {
  runtime: lambda.Runtime.PYTHON_3_12,
  handler: 'handler.lambda_handler',
  code: lambda.Code.fromAsset('src'),
  layers: [depsLayer],
});

# Node.js layer structure (for NODE_PATH resolution):
# nodejs/node_modules/
# nodejs/node20/node_modules/  (version-specific)

Lambda Layers vs Container Images

Factor Lambda Layers Container Images
Max size250MB unzipped10GB
Cold startFaster (no image pull)Slower (image pull on first cold start)
Build workflowZIP-basedDocker-based CI/CD
Custom runtimeLimited (provided.al2)Any runtime
Best forShared libs, small binariesLarge deps, custom runtimes
Share this tool: X / Twitter LinkedIn
โœ“ 60,000+ developers use DevPlaybook โ˜…โ˜…โ˜…โ˜…โ˜… 4.8 / 5 on Gumroad โœ“ MIT licensed ยท Instant download โœ“ No login required

Related Products

on Gumroad

You Might Also Need

More Free Tools

Enjoying this tool? Get more like it.

Weekly developer tool guides, productivity playbooks & AI tricks. Free. No spam.

DevPlaybook Pro 7 days free

Unlock AI-Powered Dev Tools

  • โšก AI Code Review, Doc Generator & SQL Builder
  • โšก All premium templates & early access
  • โšก Member discounts on Gumroad products
Start Free Trial โ†’

$9/mo after trial ยท Cancel anytime

Want the full toolkit?

Get DevPlaybook Pro

Every template, guide, boilerplate, and automation script in one bundle. 13 premium products โ€” grab them all at once and save big.

$79
$241 separately Save $162 (67% off)
Get DevPlaybook Pro โ€” $79 โ†’

MIT licensed ยท Instant download ยท No subscription

See what's included โ†’ Browse all deals โ†’

Frequently Asked Questions

What are AWS Lambda Layers?▼

Lambda Layers are a way to share code and dependencies across multiple Lambda functions without including them in each function's deployment package. A layer is a ZIP archive containing libraries, runtime code, or binaries. You can attach up to 5 layers per function, and each layer is extracted to the /opt directory in the Lambda execution environment. Layers reduce deployment package sizes and enable sharing common code across functions.

What are the size limits for Lambda Layers?▼

The unzipped size of a layer cannot exceed 250MB (the combined total of the function deployment package plus all attached layers must not exceed 250MB unzipped). Each layer ZIP file can be up to 50MB when uploaded directly, or up to 250MB when uploaded via S3. You can attach up to 5 layers per function. The 250MB unzipped limit is shared between your function code and all attached layers.

When should I use Lambda Layers vs Container Images?▼

Use Lambda Layers for sharing libraries across many small functions (node_modules, Python packages, shared utilities), binary dependencies (ffmpeg, chromium), and when you need fast deployments for application code changes. Use Container Images when your dependencies exceed 250MB, you need custom runtimes, or you want to use Docker-based CI/CD workflows. Container images support up to 10GB but have longer cold starts.

How do Lambda Layers interact with deployment packages?▼

Lambda extracts all layers to /opt/ in the execution environment before your function runs. For Node.js, add /opt/nodejs/node_modules to NODE_PATH. For Python, Lambda automatically includes /opt/python and /opt/python/lib/python3.x/site-packages in PYTHONPATH. Your function code can import from layers as if the libraries were installed locally. Layer code is available at runtime, not at build time.

Can I share Lambda Layers across AWS accounts?▼

Yes, Lambda Layers support resource-based policies that allow sharing across AWS accounts or making a layer publicly available. Use the add-layer-version-permission command to grant specific account IDs or all accounts (* for public) the lambda:GetLayerVersion permission. AWS also publishes managed layers for runtimes and popular libraries. Shared layers are useful in multi-account organizations to maintain a single source of truth for common dependencies.

What is the difference between Lambda Layers and Lambda Extensions?▼

Lambda Layers provide shared code and libraries extracted to /opt/. Lambda Extensions are a special type of layer that integrates with the Lambda lifecycle โ€” they can intercept initialization, invocation, and shutdown events. Extensions run in the same execution environment as the function and are used for monitoring agents, secret managers, and configuration providers. Both use the layer mechanism but serve different purposes.