Lambda
03 / 08

Deployment & Configuration

Lambda Deployment & Configuration

Lambda can be deployed as a .zip package or a container image. Layers enable code sharing. Versions and aliases enable safe production deployments. AWS SAM and the Serverless Framework simplify infrastructure management.

Deployment Packages & Layers

# Zip deployment package — Node.js
npm ci --production
zip -r function.zip index.js node_modules/
aws lambda update-function-code   --function-name my-function   --zip-file fileb://function.zip

# Deploy from S3 (preferred for large packages > 50 MB)
aws s3 cp function.zip s3://my-deploy-bucket/function.zip
aws lambda update-function-code   --function-name my-function   --s3-bucket my-deploy-bucket   --s3-key function.zip

# Container image deployment (up to 10 GB — for ML models, large dependencies)
# Dockerfile
# FROM public.ecr.aws/lambda/nodejs:20
# COPY index.js package*.json ./
# RUN npm ci --production
# CMD ["index.handler"]

# Build and push to ECR
aws ecr create-repository --repository-name my-lambda-repo
aws ecr get-login-password | docker login --username AWS --password-stdin <account>.dkr.ecr.us-east-1.amazonaws.com
docker build -t my-lambda-repo .
docker tag my-lambda-repo:latest <account>.dkr.ecr.us-east-1.amazonaws.com/my-lambda-repo:latest
docker push <account>.dkr.ecr.us-east-1.amazonaws.com/my-lambda-repo:latest

aws lambda update-function-code   --function-name my-function   --image-uri <account>.dkr.ecr.us-east-1.amazonaws.com/my-lambda-repo:latest

# Lambda Layers — shared code/dependencies across functions (up to 5 layers)
# Create a layer
mkdir -p nodejs/node_modules && npm install --prefix nodejs sharp
zip -r sharp-layer.zip nodejs/
aws lambda publish-layer-version   --layer-name sharp-image-layer   --zip-file fileb://sharp-layer.zip   --compatible-runtimes nodejs20.x

# Attach layer to function
aws lambda update-function-configuration   --function-name my-function   --layers arn:aws:lambda:us-east-1:123:layer:sharp-image-layer:1

Configuration & Concurrency

# Memory (128 MB – 10 GB) — also controls CPU allocation proportionally
# Timeout (1 sec – 15 min)
aws lambda update-function-configuration   --function-name my-function   --memory-size 1024   --timeout 30

# Environment variables (not for secrets — use Secrets Manager or SSM)
aws lambda update-function-configuration   --function-name my-function   --environment "Variables={NODE_ENV=production,LOG_LEVEL=info,TABLE_NAME=users-prod}"

# Concurrency:
# Default: unreserved (all functions share account limit of 1000)
# Reserved concurrency: guarantee N concurrent executions for a function (also acts as throttle)
# Provisioned concurrency: pre-warm N execution environments (eliminates cold starts)

# Set reserved concurrency
aws lambda put-function-concurrency   --function-name my-function   --reserved-concurrent-executions 50

# Provisioned concurrency (on an alias or version)
aws lambda put-provisioned-concurrency-config   --function-name my-function   --qualifier prod   --provisioned-concurrent-executions 10

# Versions & Aliases
# Publish a version (immutable snapshot of current $LATEST)
aws lambda publish-version --function-name my-function

# Create alias pointing to version (stable ARN for callers)
aws lambda create-alias   --function-name my-function   --name prod   --function-version 5

# Weighted alias for canary deployment (10% to new version, 90% to old)
aws lambda update-alias   --function-name my-function   --name prod   --routing-config "AdditionalVersionWeights={6=0.1}"  # version 6 gets 10%

AWS SAM & Serverless Framework

# AWS SAM (Serverless Application Model) — template.yaml
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31

Globals:
  Function:
    Runtime: nodejs20.x
    Timeout: 30
    MemorySize: 512
    Environment:
      Variables:
        TABLE_NAME: !Ref UsersTable

Resources:
  GetUserFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: src/handlers/getUser.handler
      Policies:
        - DynamoDBReadPolicy:
            TableName: !Ref UsersTable
      Events:
        Api:
          Type: HttpApi
          Properties:
            Path: /users/{userId}
            Method: GET

  CreateUserFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: src/handlers/createUser.handler
      Policies:
        - DynamoDBWritePolicy:
            TableName: !Ref UsersTable
      Events:
        Api:
          Type: HttpApi
          Properties:
            Path: /users
            Method: POST

  UsersTable:
    Type: AWS::DynamoDB::Table
    Properties:
      BillingMode: PAY_PER_REQUEST
      AttributeDefinitions:
        - AttributeName: pk
          AttributeType: S
      KeySchema:
        - AttributeName: pk
          KeyType: HASH

# SAM CLI commands
# sam build                     — build + package
# sam local invoke GetUserFunction --event event.json
# sam local start-api           — local API GW emulation
# sam deploy --guided           — interactive first deploy
# sam deploy                    — subsequent deploys (uses samconfig.toml)

Keep your own version of these notes — editable, searchable, and organised by your stack.

Start free