Learning center

CodeDeploy ScriptFailed: which hook, which script, which revision

When a CodeDeploy lifecycle event fails with ScriptFailed, read that event's diagnostics on the instance target for the hook, the script and the log tail. The hook name then tells you whether the failing script came from the new revision or from the last revision that succeeded, which decides what you fix.

The signature and its nearby causes#

An EC2/on-premises deployment fails, and the console or pipeline shows a deployment-level error such as HEALTH_CONSTRAINTS. That code counts instances: the deployment failed on too many instances for the deployment configuration's minimum healthy hosts. The cause sits one level down, on each failed instance's lifecycle events. Every lifecycle event carries a diagnostics object whose errorCode is one of six values. ErrorInformation · Diagnostics

diagnostics.errorCodeAPI descriptionWhere to go
SuccessThe specified script ran.Not this event
ScriptMissingThe specified script was not found in the specified location.The location in the AppSpec hooks section, relative to the bundle root
ScriptNotExecutableThe specified script is not a recognized executable file type.File type and line endings in the bundle
ScriptTimedOutThe specified script did not finish running in the specified time period.The hook's timeout; see the agent and timeout guide
ScriptFailedThe specified script failed to run as expected.This guide
UnknownErrorThe specified script did not run for an unknown reason.The message field and the agent log

ScriptFailed has three nearby causes that look the same in the console. The script belongs to the old revision and runs before the new one is installed. The script belongs to the new revision and fails against the instance. Or the script is fine and the instance state it expects, such as a file or service left behind by an earlier deployment, is not there. The hook name separates the first two, and the log tail usually separates the third.

Read the failed event's diagnostics#

Use a read-only profile and replace the uppercase placeholders. Start with the deployment: its revision, the revision before it, and whether it was told to ignore stop failures. DeploymentInfo

aws deploy get-deployment \
  --profile PROFILE --region REGION --deployment-id DEPLOYMENT_ID \
  --query 'deploymentInfo.{status:status,error:errorInformation,group:deploymentGroupName,config:deploymentConfigName,creator:creator,revision:revision,previous:previousRevision,ignoreStopFailures:ignoreApplicationStopFailures,overview:deploymentOverview}' \
  --output json --no-cli-pager

Then list the instance targets that failed and read one. get-deployment-instance still works for EC2/on-premises, but the API model marks it deprecated in favor of the target operations. ListDeploymentTargets · get-deployment-target · InstanceTarget

aws deploy list-deployment-targets \
  --profile PROFILE --region REGION --deployment-id DEPLOYMENT_ID \
  --target-filters '{"TargetStatus":["Failed"]}' \
  --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.instanceTarget.{target:targetId,status:status,label:instanceLabel,events:lifecycleEvents[?status==`Failed`].{event:lifecycleEventName,start:startTime,end:endTime,code:diagnostics.errorCode,script:diagnostics.scriptName,message:diagnostics.message,log:diagnostics.logTail}}' \
  --output json --no-cli-pager

scriptName names the script, message carries CodeDeploy's summary, and logTail returns up to the last 4 KB of the diagnostic log. instanceLabel is BLUE or GREEN in a blue/green deployment. Read two or three failed targets before deciding: the same hook, script and tail on every instance points at the revision, while a failure on one instance only points at that instance's state. LifecycleEvent

The hook name says which revision ran the script#

During a deployment, the CodeDeploy agent runs the ApplicationStop, BeforeBlockTraffic and AfterBlockTraffic scripts from the AppSpec file of the previous successful deployment. All other scripts come from the AppSpec file in the current deployment. The agent finds the previous revision through the deployment-group-id_last_successful_install file under /opt/codedeploy-agent/deployment-root/deployment-instructions on Linux. ApplicationStop does not run the first time you deploy to an instance, because no earlier AppSpec file exists there. AppSpec hooks · Failed ApplicationStop, BeforeBlockTraffic or AfterBlockTraffic

Failed eventScript comes fromWhat a fix in the new revision does
ApplicationStop, BeforeBlockTraffic, AfterBlockTrafficThe last successful revision on that instanceNothing for this deployment, because the old script runs first
BeforeInstall, AfterInstall, ApplicationStart, ValidateService, BeforeAllowTraffic, AfterAllowTrafficThe revision being deployedFixes the next attempt
DownloadBundle, Install, BlockTraffic, AllowTrafficReserved for the agent; no scripts runRead message, not a script

AWS lists three reasons an old-revision hook fails: the location in the last-successful-install file no longer exists, the AppSpec file or scripts at that location are invalid, or the script has an error that can never succeed. For the last case, AWS documents one way through: create a new deployment that ignores those three events' failures. This is a change, not a read. It continues to DownloadBundle after a failed ApplicationStop, so the old script's work, such as stopping a process, may not have happened. Ignore stop failures · create-deployment

# Change: starts a new deployment that continues past ApplicationStop,
# BeforeBlockTraffic and AfterBlockTraffic failures.
aws deploy create-deployment \
  --profile PROFILE --region REGION \
  --application-name APPLICATION --deployment-group-name DEPLOYMENT_GROUP \
  --s3-location bucket=BUCKET,key=KEY,bundleType=zip \
  --ignore-application-stop-failures --no-cli-pager

Once that deployment succeeds, the new revision becomes the last successful one, and its own ApplicationStop script runs next time.

Read the log tail for the cause#

The tail holds the script's own output. A few patterns change the next step:

  • The command or file does not exist. AWS names invalid character, command not found and file not found as signs of non-printing characters, such as carriage returns, that some editors add to shell scripts. Check the script's line endings in the bundle. Text editor problems
  • The script's own command failed. A service that would not start, a migration that returned an error, or a permission denial is a defect in the revision or its configuration. Fix it in the revision.
  • The tail is cut off. 4 KB is often not enough. The full output of every script is in scripts.log under /opt/codedeploy-agent/deployment-root/DEPLOYMENT_GROUP_ID/DEPLOYMENT_ID/logs/, and codedeploy-agent-deployments.log under deployment-root/deployment-logs/ collects it per deployment ID. Files the agent installs

An illustrative example: two ScriptFailed deployments#

These records are fictional. The application orders deploys revision orders-58.zip to the deployment group orders-prod, replacing orders-57.zip. Both deployments end with HEALTH_CONSTRAINTS.

In the first, every failed target shows the same event:

FieldValue
lifecycleEventNameApplicationStop
diagnostics.errorCodeScriptFailed
diagnostics.scriptNamescripts/stop_orders.sh
logTailThe script's systemctl stop reports that the orders unit is not loaded

ApplicationStop ran the stop script from orders-57.zip. Revision 58 renamed the systemd unit, but an operator had already applied the rename by hand on these hosts, so the old script stops a unit that is gone. Changing orders-58.zip would not reach this script. Redeploy revision 58 with --ignore-application-stop-failures, then check that no old orders process is still running before traffic returns.

In the second, the event is AfterInstall, the script is scripts/migrate.sh and the tail ends with the migration tool's error about a missing column. That script came from orders-58.zip, so the fix belongs in revision 58. A rollback redeploys revision 57, but CodeDeploy does not revert what earlier scripts did, so check the database state the migration left before you roll back. Rollback behavior

Where the diagnostics mislead#

  • The tail is the last 4 KB of output. An error printed early, followed by cleanup noise, is not in it.
  • Diagnostics describes scripts. When DownloadBundle fails, CodeDeploy reports UnknownError with messages such as not opened for reading, which AWS ties to S3 errors, instance profile permissions or a bucket in another Region. DownloadBundle failures
  • If every lifecycle event is Skipped or stays Pending, no script ran. The agent, its credentials or its network path are the cause; see why CodeDeploy events stay pending.
  • The agent keeps archives of five revisions by default, and it rotates its own Linux log daily and deletes it after seven days. Copy the logs you need early. Log cleanup

A failure on one instance in a large group may not fail the deployment at all, depending on the minimum healthy hosts setting; the health constraints guide works through the arithmetic. The signature table lists the fields other AWS deployment services use for the same question.

Questions#

Why does fixing my ApplicationStop script not fix the failed deployment?

ApplicationStop runs the script from the last revision that deployed successfully on that instance, not the one you are deploying. Deploy with --ignore-application-stop-failures once, and the fixed script becomes the one that runs next time.

Where is the full output of a CodeDeploy hook script?

get-deployment-target returns only the last 4 KB in diagnostics.logTail. On a Linux instance, the full output is in scripts.log under the deployment's folder in /opt/codedeploy-agent/deployment-root/.

What is the difference between ScriptFailed and ScriptTimedOut?

ScriptFailed means the script ran and failed to run as expected. ScriptTimedOut means it was still running when the hook's timeout expired. The default and maximum is 3,600 seconds per lifecycle event.

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