The signature and its nearby causes#
An ECS service uses the CODE_DEPLOY deployment controller. A CodeDeploy blue/green deployment creates a replacement task set, routes test traffic to it if a test listener exists, then shifts production traffic. The deployment ends Failed, and if automatic rollback is configured, traffic returns to the original task set. CodeDeploy rolls back an ECS deployment by rerouting traffic from the replacement task set to the original one. Deployments on the ECS platform · Blue/green with CodeDeploy
Three causes leave that same outcome:
- The hook function reported
Failed. A Lambda function named in the AppSpecHookssection ran its checks and calledPutLifecycleEventHookExecutionStatuswithFailed. - The hook function never reported. CodeDeploy considers the deployment failed if the validation function does not notify it within one hour. A function that crashes, times out, lacks permission to call CodeDeploy or sends the wrong IDs ends the same way.
- The replacement task set never became healthy. No hook failed; the new tasks did not start or did not pass health checks, and CodeDeploy timed out waiting for them.
AppSpec hooks for ECS · PutLifecycleEventHookExecutionStatus
Read the ECS target's lifecycle events#
Use a read-only profile and replace the uppercase placeholders. Read the deployment first, then its single ECS target. DeploymentInfo · ECSTarget
aws deploy get-deployment \
--profile PROFILE --region REGION --deployment-id DEPLOYMENT_ID \
--query 'deploymentInfo.{status:status,error:errorInformation,messages:deploymentStatusMessages,rollback:rollbackInfo,autoRollback:autoRollbackConfiguration,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.ecsTarget.{status:status,events:lifecycleEvents[].{event:lifecycleEventName,status:status,start:startTime,end:endTime,code:diagnostics.errorCode,message:diagnostics.message},taskSets:taskSetsInfo[].{id:identifer,label:taskSetLabel,status:status,desired:desiredCount,running:runningCount,pending:pendingCount,weight:trafficWeight,targetGroup:targetGroup.name}}' \
--output json --no-cli-pager
The field really is spelled identifer in the API. The events run in this order: BeforeInstall, Install, AfterInstall, AllowTestTraffic, AfterAllowTestTraffic, BeforeAllowTraffic, AllowTraffic, AfterAllowTraffic. Only the hook events run your functions; Install, AllowTestTraffic and AllowTraffic are CodeDeploy's own steps. taskSetLabel marks the original task set BLUE and the replacement GREEN. ECSTaskSet
First event not Succeeded | Its duration | Most likely cause | Next read |
|---|---|---|---|
A hook such as AfterAllowTestTraffic | Minutes, close to your test's run time | The function reported Failed | The function's log for the failing check |
| A hook | About one hour | CodeDeploy never received a status | The function's log, errors and execution role |
Install | Long, with the GREEN task set below its desired count | The replacement tasks did not become healthy | ECS service events and the green target group |
The errorInformation.code enum includes HOOK_EXECUTION_FAILURE, ECS_UPDATE_ERROR and TIMEOUT, but the API reference does not describe the first two. Read the code together with the message and the lifecycle events, not on its own. ErrorInformation
When a hook failed: read the function#
CodeDeploy invokes the hook function with a DeploymentId and a LifecycleEventHookExecutionId in the event. The function must pass both back in PutLifecycleEventHookExecutionStatus with a status of Succeeded or Failed. Lambda writes to the log group /aws/lambda/FUNCTION_NAME by default, so search it for the deployment ID. Sample hook function · LoggingConfig
fields @timestamp, @logStream, @message
| filter @message like /DEPLOYMENT_ID/ or @message like /ERROR|Error|Task timed out/
| sort @timestamp asc
| limit 100
Run it over the hook event's start to end window. Each of three results sends you somewhere different:
- A test result and a call reporting
Failed. The function worked and the check it runs failed. The replacement task set answered on the test listener, but not the way the test expects. Fix the release, or the test if the test is wrong. - An error before any call to CodeDeploy. The function crashed or timed out, so CodeDeploy waited out the hour. Fix the function.
- An access denial on the status call. AWS lists a missing
codedeploy:PutLifecycleEventHookExecutionStatuspermission on the function's execution role as a cause of a deployment that times out waiting for the hook. Troubleshoot ECS deployments
No log at all for that window means the function was not invoked. Check that the AppSpec names the right function and that the CodeDeploy service role can invoke it.
When the task set failed: read ECS#
If Install never completed, no hook is at fault. For a deployment that times out waiting for the replacement task set, AWS's first step is the ECS service events: find out why the replacement tasks are not becoming healthy. The task set's externalId holds the CodeDeploy deployment ID, and stabilityStatus stays STABILIZING until the running count matches the desired count and every task reports healthy to its load balancer, service discovery and container health checks. Troubleshoot ECS deployments · TaskSet
aws ecs describe-services \
--profile PROFILE --region REGION --cluster CLUSTER --services SERVICE \
--query 'services[0].{controller:deploymentController.type,taskSets:taskSets[].{id:id,deployment:externalId,status:status,definition:taskDefinition,desired:computedDesiredCount,running:runningCount,pending:pendingCount,stability:stabilityStatus,targetGroups:loadBalancers[].targetGroupArn},events:events[:15].[createdAt,message]}' \
--output json --no-cli-pager
aws elbv2 describe-target-health \
--profile PROFILE --region REGION --target-group-arn GREEN_TARGET_GROUP_ARN \
--query 'TargetHealthDescriptions[].{target:Target.Id,port:Target.Port,state:TargetHealth.State,reason:TargetHealth.Reason}' \
--output table --no-cli-pager
From here the failure is an ordinary ECS startup or health check failure on the new task definition, and the stopped tasks' stop codes sort it; see the signature table. Service auto scaling during the deployment is a separate cause AWS documents: if a scaling event occurs during a blue/green deployment and the service does not reach steady state within 5 minutes, CodeDeploy stops the deployment and marks it failed. Blue/green considerations
An illustrative example: one service, two rollbacks#
These records are fictional. The service checkout moves from checkout:41 to checkout:42 through CodeDeploy with a test listener on port 8443 and an AfterAllowTestTraffic hook function, checkout-smoke-test.
| Event | First rollback | Second rollback |
|---|---|---|
Install | Succeeded, 3 minutes | Failed after a long wait |
AllowTestTraffic | Succeeded | Not reached |
AfterAllowTestTraffic | Failed, 2 minutes | Not reached |
GREEN task set | 4 running of 4 | 0 running of 4 |
In the first rollback the replacement tasks were healthy and served test traffic. The hook ran for two minutes, and its log shows the smoke test's checkout request returning a 500 from checkout:42, followed by a status call with Failed. The release is the cause.
In the second, no hook ran. The ECS service events show replacement tasks starting and stopping, and the stopped tasks carry TaskFailedToStart with a reason that names a missing log group in checkout:42. The fix is in the task definition, and the smoke test had nothing to do with it.
A third pattern would be AfterAllowTestTraffic failing after almost exactly one hour with no Failed status in the function's log. That points at the function or its permissions, not at checkout:42.
Where this reading stops#
- A hook proves only what its test checks. A passing
AfterAllowTestTraffichook says the replacement task set answered the test's requests on the test listener, not that it handles production traffic. - A CloudWatch alarm attached to the deployment group can also stop the deployment and trigger a rollback. The Lambda alias guide separates an alarm from a hook, and the same reads apply here.
- When validation fails and rollbacks are configured, CodeDeploy returns the components to their state when the deployment started, so the replacement task set does not stay for inspection. Read its stopped tasks and the service events promptly. AfterAllowTestTraffic outcomes
- This guide covers the
CODE_DEPLOYdeployment controller. The ECS developer guide also documents a native ECS blue/green deployment type, which uses its own configuration model and hook format.
Questions#
Why did my CodeDeploy ECS deployment fail after exactly one hour?
CodeDeploy treats a hook as failed if the validation function does not report a status within one hour. Check the function's log for a crash or timeout before the status call, and check that its execution role allows codedeploy:PutLifecycleEventHookExecutionStatus.
How do I tell a failed hook from unhealthy replacement tasks?
Read the ECS target's lifecycle events with get-deployment-target. A failed hook event such as AfterAllowTestTraffic points at the hook function, while a failed or stalled Install with the GREEN task set below its desired count points at the replacement tasks.
What does the AfterAllowTestTraffic hook run against?
It runs after the test listener serves traffic to the replacement task set, so a test there reaches the new tasks before production traffic moves. Its result can trigger a rollback.