StackPractices
intermediate By Mathias Paulenko

Package Python Dependencies for AWS Lambda with Layers

Package Python dependencies for AWS Lambda using Lambda Layers, Docker builds for native extensions, and SAM/Serverless Framework integration.

Overview

AWS Lambda limits deployment packages to 250MB unzipped, and Python packages with C extensions (NumPy, pandas, psycopg2) must be compiled for the OS Lambda actually runs — Amazon Linux 2 for python3.11, AL2023 for python3.12+. Pip-install them on your laptop and you get binaries that crash with cannot open shared object file the moment they hit Lambda.

Lambda Layers solve both sides: they split dependencies out of the function zip, and when built inside the official Lambda Docker image the wheels match the runtime exactly. This recipe covers manual builds, Docker builds for native code, an automated script, and SAM/Serverless integration — plus when a layer stops being the right answer and a container image wins.

When to Use This

  • Lambda functions that need third-party Python packages (requests, SQLAlchemy, pandas)
  • Packages with C extensions that must be compiled for the Lambda runtime OS
  • Sharing dependencies across multiple Lambda functions
  • Reducing deployment package size by moving dependencies to a layer
  • CI/CD pipelines that publish layer versions on dependency changes

Skip layers when the whole dependency set pushes past 250MB or when you need system packages pip can’t install — that’s container-image territory (up to 10GB). Also skip the layer for dependencies that change on every deploy; the version bump costs more than it saves.

Prerequisites

  • Python 3.11+
  • AWS CLI configured with appropriate permissions
  • Docker (for native extension builds)
  • AWS SAM CLI (optional, for SAM-based deployment)

Solution

1. Manual Layer Build (Pure Python Packages)

Pure-Python wheels are platform-independent, so this works anywhere:

# Create a directory structure matching Lambda's Python path
mkdir -p layer/python
pip install requests pydantic sqlalchemy --target layer/python

# Zip the layer
cd layer
zip -r ../my-deps-layer.zip python
cd ..

# Publish the layer
aws lambda publish-layer-version \
  --layer-name my-python-deps \
  --zip-file fileb://my-deps-layer.zip \
  --compatible-runtimes python3.11 python3.12 \
  --compatible-architectures x86_64 arm64

The python/ subdirectory isn’t cosmetic — Lambda mounts each layer at /opt, and /opt/python is what lands on PYTHONPATH. Zip without it and the packages are present but unimportable.

2. Docker Build for Native Extensions

Native packages (NumPy, pandas, psycopg2-binary) must be compiled for the Lambda runtime OS. Build inside the official image and the output is guaranteed compatible:

# Build in a container matching Lambda's runtime
docker run --rm -v "$PWD/layer":/var/task public.ecr.aws/lambda/python:3.11 \
  /bin/sh -c "pip install numpy pandas psycopg2-binary --target /var/task/python"

# Zip and publish
cd layer
zip -r ../native-deps-layer.zip python
cd ..

aws lambda publish-layer-version \
  --layer-name native-python-deps \
  --zip-file fileb://native-deps-layer.zip \
  --compatible-runtimes python3.11 \
  --compatible-architectures x86_64

On macOS and Windows this step is mandatory for anything with a .so inside — local builds produce binaries for the wrong platform and the failure only surfaces at cold start.

3. Automated Layer Build Script

Same flow, repeatable and safe to put in CI:

#!/bin/bash
# build-layer.sh
set -euo pipefail

LAYER_NAME="app-deps"
RUNTIME="python3.11"
ARCH="x86_64"
REQUIREMENTS_FILE="requirements-layer.txt"

# Clean previous build
rm -rf build && mkdir -p build/python

# Install dependencies in Docker (matching Lambda runtime).
# Mount the project root so the requirements file is visible inside the container.
docker run --rm \
  -v "$PWD":/var/task \
  public.ecr.aws/lambda/python:${RUNTIME} \
  /bin/sh -c "pip install -r /var/task/${REQUIREMENTS_FILE} --target /var/task/build/python"

# Zip
cd build
zip -r "../${LAYER_NAME}.zip" python
cd ..

# Publish
LAYER_ARN=$(aws lambda publish-layer-version \
  --layer-name "${LAYER_NAME}" \
  --zip-file "fileb://${LAYER_NAME}.zip" \
  --compatible-runtimes "${RUNTIME}" \
  --compatible-architectures "${ARCH}" \
  --query 'LayerVersionArn' \
  --output text)

echo "Layer published: ${LAYER_ARN}"
echo "${LAYER_ARN}" > layer-arn.txt

Writing the ARN to layer-arn.txt gives the deploy step a stable hand-off — the same file a create-function or update-function-configuration call reads from.

4. Using the Layer in a Lambda Function

# lambda_function.py
import json
import requests  # From the layer
from pydantic import BaseModel  # From the layer

class ApiResponse(BaseModel):
    status: str
    data: dict

def lambda_handler(event, context):
    response = requests.get("https://api.example.com/data")
    api_data = ApiResponse(status="ok", data=response.json())
    return {
        "statusCode": 200,
        "body": json.dumps(api_data.model_dump()),
    }

Deploy with the layer attached:

# Zip only the function code (no dependencies)
zip function.zip lambda_function.py

# Create or update function with layer
aws lambda create-function \
  --function-name my-api \
  --runtime python3.11 \
  --handler lambda_function.lambda_handler \
  --zip-file fileb://function.zip \
  --role arn:aws:iam::123456789012:role/lambda-role \
  --layers $(cat layer-arn.txt)

5. SAM Template with Layers

# template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31

Resources:
  DepsLayer:
    Type: AWS::Serverless::LayerVersion
    Properties:
      LayerName: app-deps
      ContentUri: layer/
      CompatibleRuntimes:
        - python3.11
      CompatibleArchitectures:
        - x86_64

  ApiFunction:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: my-api
      Handler: lambda_function.lambda_handler
      Runtime: python3.11
      CodeUri: src/
      Layers:
        - !Ref DepsLayer
      Events:
        ApiEvent:
          Type: Api
          Properties:
            Path: /data
            Method: GET
      Environment:
        Variables:
          API_URL: https://api.example.com

Build and deploy:

# Build the layer content
sam build

# Deploy
sam deploy --guided

6. Serverless Framework with Layers

# serverless.yml
service: my-api

provider:
  name: aws
  runtime: python3.11
  architecture: x86_64

layers:
  deps:
    path: layer
    name: app-deps
    compatibleRuntimes:
      - python3.11

functions:
  api:
    handler: lambda_function.lambda_handler
    layers:
      - { Ref: DepsLambdaLayer }
    events:
      - http:
          path: /data
          method: get

7. requirements.txt Split Strategy

Separate function code dependencies from layer dependencies — the split tracks how often each side changes:

# requirements.txt — function-level (small, changes often)
boto3>=1.34.0
aws-xray-sdk>=2.12.0

# requirements-layer.txt — layer-level (large, changes rarely)
requests>=2.31.0
pydantic>=2.5.0
sqlalchemy>=2.0.0
psycopg2-binary>=2.9.0
numpy>=1.26.0
pandas>=2.1.0

Keeping volatile packages out of the layer means dependency updates don’t force a new layer version and a function re-attach on every deploy.

8. Multiple Layers for Different Needs

# SAM template with multiple layers
Resources:
  DataLayer:
    Type: AWS::Serverless::LayerVersion
    Properties:
      LayerName: data-deps
      ContentUri: layers/data/
      CompatibleRuntimes: [python3.11]

  HttpLayer:
    Type: AWS::Serverless::LayerVersion
    Properties:
      LayerName: http-deps
      ContentUri: layers/http/
      CompatibleRuntimes: [python3.11]

  ApiFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: lambda_function.lambda_handler
      Runtime: python3.11
      CodeUri: src/
      Layers:
        - !Ref DataLayer
        - !Ref HttpLayer

How It Works

  1. Layer structure: Lambda expects Python packages in /opt/python/ inside the layer zip. When a layer is attached, Lambda mounts it at /opt, and /opt/python is added to PYTHONPATH.
  2. Native extensions: Packages like NumPy compile C code during installation. The compiled binary must match the runtime OS (Amazon Linux 2 for 3.11, AL2023 for 3.12+) and architecture (x86_64 or arm64). Docker builds ensure compatibility.
  3. Layer limits: Up to 5 layers per function. Total unzipped size (function + layers) must be under 250MB. Each layer can be up to 50MB compressed.
  4. Layer versioning: Each publish creates a new immutable version. Functions reference a specific version ARN. Updating a layer requires updating the function to point to the new version — immutability is what makes rollbacks safe, and also what makes people forget the second step.
  5. Cold start: Layers are cached on the execution environment. First invocation downloads the layer; subsequent invocations reuse it. Larger layers increase cold start time — see Reduce AWS Lambda Cold Start for the mitigation side. Layers help cold starts in one specific way: the download is a separate, cacheable artifact, so iterating on function code doesn’t re-pull the dependency set.
flowchart diagram: REQ[

Variants

ARM64 (Graviton) Build

# Build for ARM64 (Graviton2 — lower cost, better performance)
docker run --rm --platform linux/arm64 \
  -v "$PWD/layer":/var/task \
  public.ecr.aws/lambda/python:3.11-arm64 \
  /bin/sh -c "pip install numpy --target /var/task/python"

aws lambda publish-layer-version \
  --layer-name native-deps-arm64 \
  --zip-file fileb://native-deps-layer.zip \
  --compatible-runtimes python3.11 \
  --compatible-architectures arm64

Custom Runtime with Compiled Binary

# Dockerfile for a custom Lambda layer with compiled extensions
FROM public.ecr.aws/lambda/python:3.11

COPY requirements-layer.txt /tmp/
RUN pip install --target /opt/python -r /tmp/requirements-layer.txt

# Compile any native code
COPY src/ /opt/native/
RUN cd /opt/native && make install

Using AWS-Provided Layers

# List AWS-provided layers
aws lambda list-layers

# Use AWS's Pillow layer (pre-built for Lambda)
aws lambda update-function-configuration \
  --function-name my-image-processor \
  --layers arn:aws:lambda:us-east-1:770693414926:layer:Klayers-p311-Pillow:1

Layer vs Container Image

When the dependency set outgrows layers, switch to packaging the function as a container image (up to 10GB). The trade: you lose the shared-layer model and gain a real Dockerfile — and cold starts stay comparable as long as the image lives in ECR in the same region.

The 250MB unzipped ceiling is the hard line. A realistic check: a pandas+numpy layer lands around 60-80MB compressed and roughly double that unzipped, so one function can carry two heavy layers plus a light one before hitting the wall. If your math already crosses it — scipy plus torch plus spacy, for instance — don’t fight it, go straight to the image route. The other breaking point is system packages: libpq, poppler, or any yum-level dependency can’t come from pip at all, which makes the Dockerfile the only door.

Best Practices

  • Split layers by change frequency: Put stable dependencies (pandas, numpy) in one layer and frequently changing ones in another. This maximizes layer cache reuse.
  • Use Docker for any package with C extensions: Installing locally on macOS/Windows produces binaries that won’t work on Lambda’s Amazon Linux 2/AL2023.
  • Pin dependency versions: Use == in requirements.txt to ensure reproducible builds. A new version of a dependency shouldn’t break your layer.
  • Keep function code small: Only include the handler and business logic in the function zip. Move all dependencies to layers.
  • Use ARM64 when possible: Graviton2 processors are cheaper and faster for many workloads. Build separate layers for x86_64 and arm64.
  • Minimize layer count: You can attach up to 5 layers. Consolidate related packages into one layer to stay under the limit.

Common Mistakes

  • Installing packages locally on macOS/Windows: Native extensions compiled for your OS won’t work on Lambda. Always use Docker with the Lambda runtime image.
  • Exceeding 250MB unzipped limit: Large dependencies (pandas, scipy) can push you over the limit. Use lighter alternatives or strip unnecessary files.
  • Not pinning versions: A pip install without version pins can pull a newer, incompatible package. Always pin.
  • Including .pyc and __pycache__: These add unnecessary size. Add --no-compile to pip or exclude them in the zip — the bytecode also isn’t portable across architectures anyway.
  • Forgetting to update the function after a layer update: Publishing a new layer version doesn’t automatically update functions. You must call update-function-configuration with the new ARN.
  • Zipping without the python/ top-level dir: The layer mounts at /opt, and only /opt/python joins PYTHONPATH — packages outside it are dead weight.

Further Reading

Frequently Asked Questions

What is the maximum size for a Lambda layer?

50MB compressed, up to 250MB unzipped (combined with function code). For larger dependencies, consider container-based Lambda functions (up to 10GB image size).

Can I share layers across functions?

Yes. Layers are regional resources. Any function in the same region and account can attach a layer. You can also share layers across accounts via resource-based policies.

How do I strip unnecessary files from a layer?

After pip install, remove __pycache__, .pyc, tests, docs, and examples:

find layer/python -type d -name "__pycache__" -exec rm -rf {} +
find layer/python -type d -name "tests" -exec rm -rf {} +
find layer/python -type f -name "*.pyc" -delete
Can I use conda packages in Lambda?

Not directly. Lambda runs Amazon Linux 2/AL2023 with the runtime's Python. Use pip with Docker builds instead. For scientific packages, check whether AWS ships a pre-built layer.

Layers or a container image — which should I pick?

Layers while the unzipped total stays under 250MB and you want dependency sharing across functions. Container image when you need system packages, larger sets, or a fully controlled filesystem.

What is the difference between layers and container Lambda?

Layers are zip-based and limited to 250MB. Container Lambda packages the function as a Docker image (up to 10GB), giving more flexibility for large dependencies and custom runtimes.