Home / AWS

AWSRunbookAdvanced

Sev1: Bad Lambda deployment breaking production, roll back with versions and aliases

Errors spike right after a Lambda release. Point the alias back to the last good version in one command, or redeploy old code if callers use $LATEST, then drain the failed events.

Deepak Bhardwaj6 min read

Applies toAWS Lambda (zip and container image), aliases and versions, CodeDeploy, AWS SAM, AWS CLI v2

Symptoms

  • Errors or Throttles for one function jump within minutes of a deploy.
  • API Gateway returns 502 or 500; Step Functions executions fail on a Lambda task.
  • SQS queues back up and the DLQ starts filling; stream consumers show rising IteratorAge.
  • Logs show new exceptions such as KeyError, Runtime.ImportModuleError, Task timed out, or a missing environment variable.

The rollback path depends on what the caller invokes. Find that first.

Step 1: see how the function is invoked (3 minutes)

FN=orders-api
aws lambda list-aliases --function-name $FN \
  --query "Aliases[].[Name,FunctionVersion,RoutingConfig]" --output table
aws lambda list-versions-by-function --function-name $FN \
  --query "Versions[-5:].[Version,LastModified,Description,CodeSha256]" --output table
aws lambda list-event-source-mappings --function-name $FN \
  --query "EventSourceMappings[].[UUID,EventSourceArn,FunctionArn,State]" --output table

Check the API Gateway integration, EventBridge target or Step Functions state definition too. The ARN tells you the path:

Caller invokes ARN ends with Rollback
An alias :function:orders-api:live Step 2A, one command
A specific version :function:orders-api:42 Change the caller to the previous version number
Unqualified or $LATEST :function:orders-api Step 2B, redeploy the old code and config
CodeDeploy-managed alias alias with weighted routing during deploy Step 2C, stop the deployment

Confirm the errors line up with the new version. The Resource dimension holds function:alias or function:version:

aws cloudwatch get-metric-statistics --namespace AWS/Lambda --metric-name Errors \
  --dimensions Name=FunctionName,Value=$FN Name=Resource,Value=$FN:live \
  --statistics Sum --period 60 \
  --start-time $(date -u -d '-1 hour' +%FT%TZ) --end-time $(date -u +%FT%TZ) --output table

Step 2: roll back

2A: alias points to the bad version

Point the alias at the last good version. This is atomic and takes effect for new invocations right away:

aws lambda update-alias --function-name $FN --name live --function-version 41

If the alias had weighted routing (canary), clear it so 100% goes to the good version:

aws lambda update-alias --function-name $FN --name live --function-version 41 \
  --routing-config '{"AdditionalVersionWeights":{}}'

A published version is immutable and includes its code, layers, environment variables, memory and timeout. Rolling the alias back reverts all of them together.

Tip

If provisioned concurrency is configured on the alias, Lambda allocates it to the version the alias now points to. Until that finishes, some requests can run on on-demand environments with cold starts. Watch ProvisionedConcurrencySpilloverInvocations.

2B: callers use $LATEST

$LATEST is mutable, so you have to put the old code and configuration back. If you published versions, copy from the last good one.

# Code: zip package
URL=$(aws lambda get-function --function-name $FN --qualifier 41 --query "Code.Location" --output text)
curl -s -o good.zip "$URL"
aws lambda update-function-code --function-name $FN --zip-file fileb://good.zip
aws lambda wait function-updated --function-name $FN

# Code: container image
aws lambda get-function --function-name $FN --qualifier 41 --query "Code.ImageUri" --output text
aws lambda update-function-code --function-name $FN --image-uri <image-uri-from-above>

Then compare and restore configuration that changed in the release:

aws lambda get-function-configuration --function-name $FN --qualifier 41 \
  --query "{env:Environment,layers:Layers[].Arn,handler:Handler,runtime:Runtime,mem:MemorySize,timeout:Timeout}" > good.json
aws lambda get-function-configuration --function-name $FN \
  --query "{env:Environment,layers:Layers[].Arn,handler:Handler,runtime:Runtime,mem:MemorySize,timeout:Timeout}" > bad.json
diff good.json bad.json

Apply only what differs, for example aws lambda update-function-configuration --function-name $FN --layers <arn> --handler app.handler.

Warning

update-function-configuration --environment replaces the whole variable set. Pass every variable from good.json, not just the one that changed, or you will remove the rest.

If no versions were ever published, redeploy the previous artifact from your CI system or the previous git tag.

2C: CodeDeploy deployment in progress

aws deploy list-deployments --application-name orders-api-app --include-only-statuses InProgress
aws deploy stop-deployment --deployment-id d-ABCDEF123 --auto-rollback-enabled

CodeDeploy moves the alias back to the original version.

Step 3: protect queues and streams while you roll back

For SQS, Kinesis or DynamoDB stream triggers, a bad version keeps pulling and failing records. If the rollback will take more than a few minutes, pause the mapping:

aws lambda update-event-source-mapping --uuid <uuid> --no-enabled
# after rollback
aws lambda update-event-source-mapping --uuid <uuid> --enabled

Messages stay in SQS while paused, subject to the queue's retention period. Stream records stay until the stream retention expires, so do not leave a stream mapping paused for long.

Step 4: verify and recover failed events

  • Watch Errors, Duration and Throttles return to their baseline for 15 minutes.
  • Redrive the DLQ once you are sure the good version handles the messages:
aws sqs start-message-move-task \
  --source-arn arn:aws:sqs:eu-west-1:111122223333:orders-dlq \
  --destination-arn arn:aws:sqs:eu-west-1:111122223333:orders-queue
  • For asynchronous invocations with an on-failure destination, replay from that destination.
  • Check whether the bad version wrote partial data (half-written records, duplicate side effects) that needs cleanup.

Step 5: find the cause before redeploying

Common causes after a release:

Log line Likely cause
Runtime.ImportModuleError Dependency missing from package or layer, or built for the wrong architecture
Task timed out after N seconds New downstream call, or VPC change that blocks egress
KeyError / undefined on an env var Variable not set in this environment
AccessDeniedException Execution role missing a permission the new code needs
Runtime.ExitError with memory messages Memory setting too low for new code

Search logs for the new version only. Lambda writes the version into the log stream name, for example 2026/11/13/[42]abcdef:

fields @timestamp, @message
| filter @logStream like /\[42\]/ and (@message like /ERROR/ or @message like /Task timed out/)
| sort @timestamp desc
| limit 50

Prevent it next time

  • Always invoke through an alias, never $LATEST, from every caller and event source mapping.
  • Publish a version on every deploy. In AWS SAM, AutoPublishAlias: live does this.
  • Use gradual deployments with automatic rollback on alarms:
OrdersApi:
  Type: AWS::Serverless::Function
  Properties:
    AutoPublishAlias: live
    DeploymentPreference:
      Type: Canary10Percent5Minutes
      Alarms:
        - !Ref OrdersApiAliasErrorsAlarm
  • Alarm on the alias-level Errors metric, not just the function total, so the canary trips it.
  • Keep at least the last few versions; do not let a cleanup job delete the version the alias points to or the one before it.
  • Run a smoke test against the new version (aws lambda invoke --qualifier <version>) before shifting any traffic.