Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Servers, nginx and Docker

CodeDeploy reports that the overall deployment failed on too many instances

Your error

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. (Error code: HEALTH_CONSTRAINTS)2 more lines it can show

Fix it fast

Most common cause, 1 of 4

The CodeDeploy agent is not installed, not running, or cannot reach AWS

Install the 2.x agent from the resource kit bucket of your Region and start it.

Fix
sudo yum install wget
cd /home/ec2-user
wget https://aws-codedeploy-us-east-2.s3.us-east-2.amazonaws.com/latestv2/install
chmod +x ./install
sudo ./install auto
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

HEALTH_CONSTRAINTS means CodeDeploy stopped because too many instance deployments failed for the minimum healthy hosts rule to hold. The fault sits on the instances: a missing or stopped CodeDeploy agent, an instance profile without access, or a failing hook script. List the failed instances, read their lifecycle events and fix that. Applies to EC2 with agent 2.x.

  • Applies to AWS CodeDeploy, EC2/On-Premises compute platform, CodeDeploy agent 2.1.4 on Amazon Linux 2023, AWS CLI 2.42
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

The deployment-level message is a summary. Ask which instances failed, then read the lifecycle events of one of them: all events Skipped points to the agent or its access, one Failed event points to that hook.

Run this first
aws deploy list-deployment-instances --deployment-id d-EXAMPLE111 \
  --instance-status-filter Failed Skipped
aws deploy get-deployment-instance --deployment-id d-EXAMPLE111 \
  --instance-id i-0123456789abcdef0 \
  --query 'instanceSummary.lifecycleEvents[].[lifecycleEventName,status,diagnostics.errorCode,diagnostics.message]' \
  --output table

Then find the line you see in this list and open its fix:

  1. 1The CodeDeploy agent is not installed, not running, or cannot reach AWSEvery lifecycle event on the instance shows Skipped, and nothing about this deployment appears in the agent log
  2. 2The instance has no IAM instance profile, or one without the needed permissionsThe agent log says Missing credentials - please check if this instance was started with an IAM instance profile
  3. 3A lifecycle hook script fails or runs past its timeoutOne event (often BeforeInstall, AfterInstall or ApplicationStart) is Failed, with diagnostics errorCode ScriptFailed, ScriptTimedOut, ScriptMissing or ScriptNotExecutable
  4. 4The minimum healthy hosts value cannot be met by this deployment groupThe deployment fails almost at once and few or no instances were even attempted, or stopped instances sit in the group

What does the error look like?

  • 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. (Error code: HEALTH_CONSTRAINTS)
  • InstanceAgent::Plugins::CodeDeployPlugin::CommandPoller: Missing credentials - please check if this instance was started with an IAM instance profile
  • Script at specified location: after-install.sh failed to complete in 60 seconds

Cause 1 of 4

The CodeDeploy agent is not installed, not running, or cannot reach AWS

You seeEvery lifecycle event on the instance shows Skipped, and nothing about this deployment appears in the agent log

Check

CodeDeploy reports HEALTH_CONSTRAINTS with all lifecycle events skipped when the agent on the instance never picks up the work. Either the agent is absent or stopped, or it cannot open HTTPS (port 443) to the CodeDeploy and Amazon S3 endpoints. Low memory or a full disk can also stop the agent.

Check
systemctl status codedeploy-agent
df -h /opt/codedeploy-agent

Fix

Install the 2.x agent from the resource kit bucket of your Region and start it. If the agent runs but stays silent, give the instance a way out on 443: a public IP and an internet gateway route, a NAT gateway for a private subnet, or the :proxy_uri: setting when you use a proxy. On a full disk, lower max_revisions in the agent configuration.

Fix
sudo yum install wget
cd /home/ec2-user
wget https://aws-codedeploy-us-east-2.s3.us-east-2.amazonaws.com/latestv2/install
chmod +x ./install
sudo ./install auto

Verify

systemctl status codedeploy-agent shows the agent active, and a new deployment moves past ApplicationStop on that instance.

Verify
systemctl status codedeploy-agent

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 2 of 4

The instance has no IAM instance profile, or one without the needed permissions

You seeThe agent log says Missing credentials - please check if this instance was started with an IAM instance profile

Check

The deployment fails with HEALTH_CONSTRAINTS when the agent has no credentials to poll CodeDeploy or to download the revision from Amazon S3. That happens when no instance profile is attached, or when the attached role lacks the CodeDeploy and S3 read permissions. Look at the agent log and at the profile on the instance.

Check
sudo grep -i "credentials" /var/log/aws/codedeploy-agent/codedeploy-agent.log | tail -n 5
aws ec2 describe-instances --instance-ids i-0123456789abcdef0 \
  --query 'Reservations[].Instances[].IamInstanceProfile.Arn'

Fix

Attach an instance profile whose role can read the revision bucket and talk to CodeDeploy, or add those permissions to the existing role. AWS then advises restarting the instance so the agent starts with the new credentials.

Fix
aws ec2 associate-iam-instance-profile --instance-id i-0123456789abcdef0 \
  --iam-instance-profile Name=CodeDeployEC2InstanceProfile
aws ec2 reboot-instances --instance-ids i-0123456789abcdef0

Verify

After the reboot the agent log shows no credential errors, and the next deployment downloads the bundle on that instance.

Verify
sudo tail -n 20 /var/log/aws/codedeploy-agent/codedeploy-agent.log

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 3 of 4

A lifecycle hook script fails or runs past its timeout

You seeOne event (often BeforeInstall, AfterInstall or ApplicationStart) is Failed, with diagnostics errorCode ScriptFailed, ScriptTimedOut, ScriptMissing or ScriptNotExecutable

Check

A hook that exits non-zero, cannot be found, is not executable or runs longer than its appspec timeout fails that instance, and enough failed instances end in HEALTH_CONSTRAINTS. AWS's own example is a hook that launches a long-running process without detaching it. The scripts log holds the script's own output.

Check
sudo less /opt/codedeploy-agent/deployment-root/<deployment-group-ID>/<deployment-ID>/logs/scripts.log
sudo tail -n 50 /opt/codedeploy-agent/deployment-root/deployment-logs/codedeploy-agent-deployments.log

Fix

Fix the error the log shows and make every script exit on its own. To start a background process from a hook, redirect its input and output so CodeDeploy can move on. If the broken script is the ApplicationStop hook of the last good revision, deploy once with --ignore-application-stop-failures.

Fix
#!/bin/bash
# in after-install.sh: start the long-running process detached from the hook
/tmp/sleep.sh > /dev/null 2> /dev/null < /dev/null &

aws deploy create-deployment --application-name my-app \
  --deployment-group-name my-group \
  --s3-location bucket=my-bucket,key=my-app.zip,bundleType=zip \
  --ignore-application-stop-failures

Verify

get-deployment-instance lists every lifecycle event as Succeeded for that instance.

Verify
aws deploy get-deployment-instance --deployment-id d-EXAMPLE222 --instance-id i-0123456789abcdef0 --query 'instanceSummary.status'

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 4 of 4

The minimum healthy hosts value cannot be met by this deployment group

You seeThe deployment fails almost at once and few or no instances were even attempted, or stopped instances sit in the group

Check

CodeDeploy fails the whole deployment as soon as the healthy count would drop to the minimum. A percentage is rounded up, so 95 percent of 10 instances means 10 and the deployment fails before any instance is touched. Stopped instances that still match the group's tags count as failed ones.

Check
aws deploy get-deployment-group --application-name my-app --deployment-group-name my-group \
  --query 'deploymentGroupInfo.deploymentConfigName'
aws deploy get-deployment-config --deployment-config-name MyConfig \
  --query 'deploymentConfigInfo.minimumHealthyHosts'

Fix

Keep the minimum below the number of instances in the group, and start or untag the stopped instances so they leave the calculation. Create a custom deployment configuration when none of the defaults fits.

Fix
aws deploy create-deployment-config --deployment-config-name NineOfTen \
  --minimum-healthy-hosts type=HOST_COUNT,value=9

Verify

A new deployment works through the instances in batches instead of failing at the start.

Verify
aws deploy get-deployment --deployment-id d-EXAMPLE333 --query 'deploymentInfo.[status,deploymentOverview]'

Confirmed by docs.aws.amazon.com

Did this fix it?

How do I stop it coming back?

  • Install and start the CodeDeploy agent in the launch template user data or the AMI, so every new instance in the group can take a deployment.
  • Ship the agent and scripts logs to CloudWatch Logs, as AWS recommends, so a failed hook is readable without logging in to each instance.
  • Give each hook an appspec timeout a little above its real run time, and run the scripts once on a test instance before they go into a revision.
  • Review the deployment group's tags when instances are stopped or retired, so a stopped instance does not count against the healthy minimum.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: Dispatch Pro, Raheeb, Loade Driver.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.