Learning center

CodeDeploy health constraints: failed instances or an impossible setting?

A HEALTH_CONSTRAINTS failure means instances failed during the rollout. A HEALTH_CONSTRAINTS_INVALID failure means the minimum healthy hosts setting could never be met. Compare the deployment configuration's minimum, rounded up, with the number of instances in the deployment: that one calculation tells you whether to fix instances or the configuration.

Two codes, two different failures#

Both codes appear in a deployment's errorInformation.code on the EC2/on-premises compute platform. The API reference describes them in one line each, and the user guide's error-code table adds the remedy. ErrorInformation · Error codes

CodeAPI referenceUser guide
HEALTH_CONSTRAINTSThe deployment failed on too many instances to be successfully deployed within the instance health constraints specified.The overall deployment failed because too many individual instances failed deployment, too few healthy instances are available for deployment, or some instances in your deployment group are experiencing problems.
HEALTH_CONSTRAINTS_INVALIDThe revision cannot be successfully deployed within the instance health constraints specified.The deployment can't start because the minimum number of healthy instances is not available; reduce the required number or add instances to the deployment group.

The first is a symptom with causes on the instances: failing hook scripts, an agent that is not running, stopped instances. The second is arithmetic. No instance failure is needed, and redeploying the same revision to the same group will fail the same way.

Read the configuration and the instance count together#

Use a read-only profile and replace the uppercase placeholders. The deployment names its configuration and summarizes its instances. DeploymentInfo · DeploymentOverview

aws deploy get-deployment \
  --profile PROFILE --region REGION --deployment-id DEPLOYMENT_ID \
  --query 'deploymentInfo.{status:status,error:errorInformation,config:deploymentConfigName,style:deploymentStyle,overview:deploymentOverview,created:createTime,completed:completeTime}' \
  --output json --no-cli-pager

aws deploy get-deployment-config \
  --profile PROFILE --region REGION --deployment-config-name DEPLOYMENT_CONFIG \
  --query 'deploymentConfigInfo.{name:deploymentConfigName,platform:computePlatform,minimumHealthy:minimumHealthyHosts,zonal:zonalConfig}' \
  --output json --no-cli-pager

minimumHealthyHosts.type is HOST_COUNT, an absolute number, or FLEET_PERCENT, a percentage of the instances in the deployment. For the predefined CodeDeployDefault.OneAtATime, get-deployment-config returns the type MOST_CONCURRENCY with a value of 1, which you cannot set yourself. A configuration with a zonalConfig can also set minimumHealthyHostsPerZone, the number or percentage of instances that must remain available in each Availability Zone, alongside the overall minimum; it defaults to 0 percent. MinimumHealthyHosts · ZonalConfig

Add the counts in deploymentOverview (Pending, InProgress, Succeeded, Failed, Skipped, Ready) to get the number of instances the deployment covered. That count is the denominator for the next section.

A worked calculation#

CodeDeploy applies four rules, all from its instance health documentation. CodeDeploy instance health

  1. At the start, a percentage is converted to a number of instances, rounding up.
  2. If the number of healthy instances is already less than or equal to the minimum when the deployment starts, CodeDeploy fails the overall deployment immediately. That is the condition the error-code table describes for HEALTH_CONSTRAINTS_INVALID.
  3. Otherwise CodeDeploy deploys to as many instances at a time as it can without the healthy count falling below the minimum, and stops the deployment when the healthy count falls to the minimum.
  4. At least one instance must succeed, even with a minimum of 0. With OneAtATime, a failure on the last instance still leaves the deployment successful.

For a deployment group of 10 instances:

Minimum healthy hostsConverted minimumInstances at a timeWhat fails it
FLEET_PERCENT 9510 (9.5 rounded up)NoneIt fails before deploying anywhere: 10 healthy is not more than 10
FLEET_PERCENT 859 (8.5 rounded up)1The first failed instance
HOST_COUNT 991Any failure, except on the last instance
HOST_COUNT 882Two failures
HOST_COUNT 0010All 10 failing

The API reference gives a nine-instance example with the same rules: a HOST_COUNT of 6 deploys to three instances at a time, and a FLEET_PERCENT of 40 (3.6, rounded up to 4) deploys to five at a time. CodeDeployDefault.HalfAtATime deploys to up to four of nine at a time and needs five to succeed. Predefined configurations

The rounding is what makes a configuration that looks reasonable impossible. 90 percent of 10 instances leaves room for one instance at a time. 90 percent of 6 instances is 5.4, which rounds up to 6, so a six-instance group can never run that configuration.

What changes the count#

The denominator is the deployment group's instances as CodeDeploy sees them, which is not always the number you expect.

  • Stopped instances count as failed. CodeDeploy attempts to deploy to instances in the Stopped state, and in the minimum healthy host calculation a stopped instance has the same impact as a failed one. Restart them or change their tags to take them out of the group. Instance health
  • Newly tagged instances may be missing. CodeDeploy can take several minutes to read new tags, and AWS recommends waiting at least five minutes after tagging before deploying. Tags
  • Blue/green counts the replacement environment. The minimum applies to the replacement instances, not the original ones. Separately, the deployment fails if even one original instance fails to deregister from the load balancer. Instance health

An illustrative pair of failures#

These records are fictional. The deployment group orders-prod runs on six instances.

The first deployment uses a custom configuration, orders-90pct, with FLEET_PERCENT 90. It ends in seconds with HEALTH_CONSTRAINTS_INVALID, and deploymentOverview shows no instance in Succeeded or Failed. The calculation is 5.4 rounded up to 6, which equals the number of healthy instances, so CodeDeploy could never take one offline. Nothing on the instances needs fixing. Lower the minimum to 5 hosts or fewer, or add instances.

The second deployment uses HOST_COUNT 5 on the same six instances. CodeDeploy deploys to one instance at a time. The first instance fails, leaving five healthy instances, which equals the minimum, so the deployment stops with HEALTH_CONSTRAINTS. The deployment target shows AfterInstall failed with ScriptFailed. The configuration did its job: it stopped a revision that failed on its first instance. The next read is that instance's diagnostics, covered in the ScriptFailed guide.

What to change#

For HEALTH_CONSTRAINTS_INVALID, change the arithmetic: a lower minimum, a HOST_COUNT instead of a percentage, or more instances. Check the result against the rounding rule before you deploy again.

For HEALTH_CONSTRAINTS, change the instances or the revision. Lowering the minimum lets more instances fail before CodeDeploy stops, which spreads a broken revision further. Read the failed targets first:

aws deploy list-deployment-targets \
  --profile PROFILE --region REGION --deployment-id DEPLOYMENT_ID \
  --target-filters '{"TargetStatus":["Failed","Skipped"]}' \
  --query 'targetIds' --output json --no-cli-pager

A failed lifecycle event with a script error points at the revision or the instance. If every lifecycle event was skipped, AWS lists the agent not running, no route to the CodeDeploy or S3 endpoint on port 443, missing service role permissions, clock skew and a missing instance profile among the causes; see why CodeDeploy events stay pending. Skipped lifecycle events

Where the calculation stops#

The rules above are CodeDeploy's own description of in-place EC2/on-premises deployments. They do not cover ECS or Lambda deployments, which shift traffic instead of taking hosts offline. An instance's health in CodeDeploy means only that the last deployment to it succeeded; it says nothing about load balancer health or whether the application serves requests. For the matching fields in other AWS deployment services, see the signature table.

Questions#

Why did my CodeDeploy deployment fail without deploying to any instance?

If the minimum healthy hosts value, rounded up, is greater than or equal to the number of healthy instances at the start, CodeDeploy fails the deployment immediately, which the error-code table reports as HEALTH_CONSTRAINTS_INVALID. Lower the minimum or add instances.

Does a minimum healthy hosts value of 0 mean a deployment always succeeds?

No. CodeDeploy deploys to every instance at once, and at least one instance must succeed for the overall deployment to succeed.

Why does one failed instance fail my whole deployment?

With a high minimum, such as 85 percent of 10 instances, the converted minimum is 9, so CodeDeploy deploys one instance at a time and stops when the healthy count reaches 9. One failure is enough to reach it.

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