Learning center

Lambda canary rolled back by CodeDeploy: alarm or traffic hook

When CodeDeploy rolls back a Lambda canary or linear deployment, read the failed deployment's status and its target's lifecycle events. Stopped with an alarm in ALARM points at the alarm; Failed with a failed BeforeAllowTraffic or AfterAllowTraffic event points at a hook function.

The signature and its nearby causes#

CodeDeploy shifts traffic on a Lambda alias from the version production uses to the new version, in two increments for a canary configuration such as CodeDeployDefault.LambdaCanary10Percent5Minutes, or in equal steps for a linear one. A rollback redeploys the previous version as a new deployment with its own ID, so the alias ends up pointing at the original version again. Deployment configurations · Lambda deployments and rollbacks

Three causes end that way:

  • A CloudWatch alarm. A deployment group can name up to ten alarms. If any of them is activated, the deployment stops and its status becomes Stopped; with rollback on alarm configured, CodeDeploy then redeploys the last known good version. CloudWatch alarms in CodeDeploy
  • A traffic hook. The AppSpec file can name a BeforeAllowTraffic function, which runs before traffic shifts to the new version, and an AfterAllowTraffic function, which runs after all traffic has shifted. A hook that reports Failed, or reports nothing within one hour, fails the deployment. AppSpec hooks for Lambda
  • A person or tool stopped it. A manual stop also ends as Stopped, and it rolls back only if the group is set to roll back on stop requests.

Read the failed deployment#

Use a read-only profile and replace the uppercase placeholders. Start with the deployment that was rolled back, not the rollback deployment. DeploymentInfo · RollbackInfo

aws deploy get-deployment \
  --profile PROFILE --region REGION --deployment-id DEPLOYMENT_ID \
  --query 'deploymentInfo.{status:status,error:errorInformation,messages:deploymentStatusMessages,rollback:rollbackInfo,autoRollback:autoRollbackConfiguration,alarmOverride:overrideAlarmConfiguration,group:deploymentGroupName,config:deploymentConfigName,created:createTime,completed:completeTime}' \
  --output json --no-cli-pager

aws deploy list-deployment-targets \
  --profile PROFILE --region REGION --deployment-id DEPLOYMENT_ID \
  --query 'targetIds' --output json --no-cli-pager

aws deploy get-deployment-target \
  --profile PROFILE --region REGION --deployment-id DEPLOYMENT_ID --target-id TARGET_ID \
  --query 'deploymentTarget.lambdaTarget.{status:status,function:lambdaFunctionInfo,events:lifecycleEvents[].{event:lifecycleEventName,status:status,start:startTime,end:endTime,message:diagnostics.message}}' \
  --output json --no-cli-pager

rollbackInfo.rollbackDeploymentId names the rollback deployment, and rollbackMessage says whether it succeeded. autoRollbackConfiguration.events lists which triggers were armed: DEPLOYMENT_FAILURE, DEPLOYMENT_STOP_ON_ALARM and DEPLOYMENT_STOP_ON_REQUEST. lambdaFunctionInfo gives the alias, currentVersion, targetVersion and targetVersionWeight, the share of traffic the new version had reached. AutoRollbackConfiguration · LambdaFunctionInfo

StatusLifecycle eventsMost likely causeNext read
StoppedAllowTraffic not Succeeded; no hook FailedAn alarmThe group's alarms and their history
FailedBeforeAllowTraffic FailedThe pre-traffic hook, before any traffic reached the new versionThe hook function's log
FailedAfterAllowTraffic FailedThe post-traffic hook, after all traffic had shiftedThe hook function's log
StoppedNo alarm changed state in the windowA manual or tool stopCloudTrail for StopDeployment

errorInformation.code also has enum values named ALARM_ACTIVE, HOOK_EXECUTION_FAILURE and MANUAL_STOP. The API reference does not describe them, so use them as a hint next to the status and events. ErrorInformation

When an alarm stopped it#

Read which alarms the group watches, then what each one did during the deployment. AlarmConfiguration · MetricAlarm · DescribeAlarmHistory

aws deploy get-deployment-group \
  --profile PROFILE --region REGION \
  --application-name APPLICATION --deployment-group-name DEPLOYMENT_GROUP \
  --query 'deploymentGroupInfo.{alarms:alarmConfiguration,autoRollback:autoRollbackConfiguration,config:deploymentConfigName}' \
  --output json --no-cli-pager

aws cloudwatch describe-alarms \
  --profile PROFILE --region REGION --alarm-names ALARM_NAME \
  --query 'MetricAlarms[].{name:AlarmName,state:StateValue,reason:StateReason,changed:StateTransitionedTimestamp,namespace:Namespace,metric:MetricName,dimensions:Dimensions,statistic:Statistic,period:Period,periods:EvaluationPeriods,threshold:Threshold,missing:TreatMissingData}' \
  --output json --no-cli-pager

aws cloudwatch describe-alarm-history \
  --profile PROFILE --region REGION --alarm-name ALARM_NAME \
  --history-item-type StateUpdate --start-date START_TIME --end-date END_TIME \
  --query 'AlarmHistoryItems[].{time:Timestamp,summary:HistorySummary}' \
  --output json --no-cli-pager

A transition to ALARM inside the deployment's window, before its completeTime, is the alarm that stopped it. Then read the alarm's dimensions, because they decide what the alarm measured. In the AWS/Lambda namespace, FunctionName aggregates every version and alias of the function, Resource narrows to one version or alias, and ExecutedVersion separates the versions behind a weighted alias. An alarm on FunctionName or on the alias's Resource counts errors from the old version too, so it can fire during a canary for a reason the new version did not cause. Viewing Lambda metrics

To see which version produced the errors, compare Errors for the two versions over the deployment window. List the dimension combinations the function publishes, then query the ones that carry ExecutedVersion, copying each dimension value exactly as list-metrics prints it.

aws cloudwatch list-metrics \
  --profile PROFILE --region REGION --namespace AWS/Lambda --metric-name Errors \
  --dimensions Name=FunctionName,Value=FUNCTION_NAME \
  --query 'Metrics[].Dimensions' --output json --no-cli-pager

aws cloudwatch get-metric-statistics \
  --profile PROFILE --region REGION --namespace AWS/Lambda --metric-name Errors \
  --dimensions Name=FunctionName,Value=FUNCTION_NAME Name=Resource,Value=RESOURCE_VALUE Name=ExecutedVersion,Value=NEW_VERSION \
  --start-time START_TIME --end-time END_TIME --period 60 --statistics Sum \
  --query 'sort_by(Datapoints,&Timestamp)[].[Timestamp,Sum]' --output text --no-cli-pager

Run the second command again with the old version number. View invocation metrics such as Errors with the Sum statistic. Lambda metric types

When a hook failed#

A hook function receives a DeploymentId and a LifecycleEventHookExecutionId and must report Succeeded or Failed through PutLifecycleEventHookExecutionStatus. Its log is in /aws/lambda/HOOK_FUNCTION_NAME by default. Sample Lambda hook · PutLifecycleEventHookExecutionStatus

The event that failed narrows it. BeforeAllowTraffic runs before any traffic moves, so a failure there means production traffic never reached the new version. AfterAllowTraffic runs once all traffic has shifted, so the new version served everything for a while before the rollback. A hook event that ran for about an hour means the function never reported a status: check its log for a crash or timeout and its execution role for permission to call CodeDeploy.

The alias after the rollback#

Read the alias to confirm where traffic goes now. get-alias · AliasRoutingConfiguration

aws lambda get-alias \
  --profile PROFILE --region REGION --function-name FUNCTION_NAME --name ALIAS \
  --query '{version:FunctionVersion,weights:RoutingConfig.AdditionalVersionWeights,revision:RevisionId}' \
  --output json --no-cli-pager

After a completed rollback, version is the original version and weights is empty. A non-empty weights means the alias still splits traffic between two versions. AWS documents that state after a manual stop with no rollback configured, and it makes later deployments of the function fail until you set the weighted alias back to none. Lambda deployment troubleshooting · Weighted aliases

An illustrative example: two rollbacks of version 58#

These records are fictional. The alias live on checkout-api moves from version 57 to 58 with CodeDeployDefault.LambdaCanary10Percent5Minutes and the alarm checkout-api-errors.

ReadFirst rollbackSecond rollback
Deployment statusStoppedFailed
targetVersionWeight100
Failed lifecycle eventNoneBeforeAllowTraffic
Alarm historyOK to ALARM at 14:03No change

In the first, the alarm fired three minutes into the canary. Its only dimension is FunctionName, so it counted both versions. Errors by ExecutedVersion shows version 57 with 40 errors in those minutes and version 58 with none: a downstream payment API was failing for both, and version 57 carried 90 percent of the traffic. The rollback removed nothing that was wrong. Fix the alarm to measure the new version, and redeploy once the dependency recovers.

In the second, BeforeAllowTraffic failed in 20 seconds. The hook's log shows its test invocation of version 58 returning a validation error for a field the new code renamed. No production request reached version 58. The cause is in the release.

Where the reading stops#

  • An alarm says a metric crossed a threshold for its evaluation periods. It does not say which version caused it unless its dimensions or metric math separate the versions.
  • A 10 percent canary over five minutes may produce too few errors to cross a threshold set for full traffic, so a quiet canary is not evidence that the new version is healthy.
  • If the deployment group sets ignorePollAlarmFailure, deployments continue when CodeDeploy cannot read alarm status, so a missing alarm stop does not mean the alarms were healthy. AlarmConfiguration
  • The same alarm and hook reads apply to ECS blue/green deployments through CodeDeploy; the ECS hook guide adds the task set checks, and the signature table covers other AWS deployment services. If the new version failed at startup, the init errors guide sorts the error types.

Questions#

How do I know whether an alarm or a hook rolled back my Lambda deployment?

Read the failed deployment with get-deployment and its target with get-deployment-target. A Stopped status with an alarm transition to ALARM in the window points at the alarm; a Failed status with a failed BeforeAllowTraffic or AfterAllowTraffic event points at a hook.

Why did my canary alarm fire when the new version had no errors?

An alarm on the FunctionName dimension, or on the alias, counts errors from every version behind the alias. During a canary most traffic still goes to the old version. Compare Errors by ExecutedVersion to see which version produced them.

Why do my Lambda deployments fail after I stopped one manually?

If the stopped deployment had no rollback configured, the alias can be left pointing at two versions, and later deployments fail. Set the alias back to a single version, then deploy again.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading