Configuring the CodeDeploy Agent, Rollbacks and Troubleshooting
Most failed deployments to EC2 and on-premises servers trace back to the agent, permissions, scripts or health checks. This lesson explains how to install and manage the CodeDeploy agent at scale, how automatic and manual rollbacks work, and a systematic method for troubleshooting failed deployments using events, logs and error codes.
Learning outcomes
After completing this lesson, you will be able to:
- Describe how the CodeDeploy agent is installed, updated and configured on EC2 and on-premises servers.
- Explain how automatic rollback works and what it deploys.
- Explain file-exists behaviour and its effect on redeployments.
- Diagnose common deployment failures from their symptoms and logs.
The CodeDeploy Agent
The CodeDeploy agent is software that must run on every EC2 and on-premises target. It polls CodeDeploy over HTTPS (port 443, outbound only) for work, downloads revisions, runs lifecycle scripts and reports results. Lambda and ECS deployments do not use the agent. The agent needs outbound access to the CodeDeploy and S3 endpoints, through the internet, a NAT gateway or VPC endpoints.
Installing the agent manually does not scale. The recommended method is AWS Systems Manager: the AWSCodeDeployAgent package in Systems Manager Distributor installs the agent, and a State Manager association keeps it installed and updated on a schedule, including on newly launched instances. The CodeDeploy console can create this association when a deployment group is configured. Alternatives are installing the agent in the golden AMI or in user data, but these do not keep it up to date.
The agent's configuration file on Linux is /etc/codedeploy-agent/conf/codedeployagent.yml. It sets values such as :max_revisions:, the number of revisions kept on disk (default 5), and proxy settings. Revisions and logs are stored under /opt/codedeploy-agent/deployment-root.
| Log | Linux location | Contents |
|---|---|---|
| Agent log | /var/log/aws/codedeploy-agent/codedeploy-agent.log | Agent activity, polling and errors such as credentials or connectivity problems |
| Deployment script log | /opt/codedeploy-agent/deployment-root/deployment-logs/codedeploy-agent-deployments.log | Output of all lifecycle hook scripts |
| Per-deployment logs | /opt/codedeploy-agent/deployment-root/<group-id>/<deployment-id>/logs/scripts.log | Script output for one deployment |
The CloudWatch agent can ship these logs to CloudWatch Logs so that failures can be investigated without connecting to instances.
Automatic and Manual Rollback
A deployment group can roll back automatically when a deployment fails, when one of its CloudWatch alarms enters the ALARM state, or both. A rollback is a new deployment of the last known good revision, with its own deployment ID; CodeDeploy does not restore files from the failed deployment. For EC2 in-place deployments, the rollback reinstalls the previous revision on the instances; for blue/green, it reroutes traffic to the original instances if they still exist; for Lambda and ECS, it shifts traffic back to the original version or task set.
Rollback can also be performed manually by deploying an earlier revision or by stopping a deployment with the option to roll back. If there is no previous successful revision, such as on the first deployment, there is nothing to roll back to.
File-exists behaviour
Because a rollback or redeployment installs files to locations where files already exist, the file_exists_behavior setting in the AppSpec or the deployment decides what happens when a destination file exists that was not placed by the previous deployment: DISALLOW (the default) fails the deployment, OVERWRITE replaces the file and RETAIN keeps the existing file. Files placed by the previous successful deployment are removed by the agent before installation, so the setting matters mainly for files created outside CodeDeploy.
A Method for Troubleshooting
When a deployment fails, work from the outside in. First, read the deployment's error code and the failed instances in the console or with get-deployment and list-deployment-instances. Next, read the lifecycle events for a failed instance with get-deployment-target to find the failing event and its diagnostic message. Then read the agent and script logs on the instance, through CloudWatch Logs or Session Manager. The table below links frequent symptoms to their causes.
| Symptom | Likely cause | Resolution |
|---|---|---|
| Instances stay Pending; deployment times out with no lifecycle events | Agent not installed or not running, no outbound access, or instance profile missing | Install or restart the agent with Systems Manager; check network and profile |
| DownloadBundle fails with access denied | Instance profile cannot read the S3 object or decrypt it | Grant s3:GetObject and kms:Decrypt to the instance profile |
| ApplicationStop fails on every deployment | Broken stop script from the previous revision | Redeploy with ignore ApplicationStop failures |
| Script fails with permission denied | Script not executable or wrong runas user | Set file mode in the revision or permissions section; correct runas |
| ScriptTimedOut | Script exceeds its timeout or waits for input, or a started process keeps the script attached | Increase the timeout; redirect output and background long-running processes |
| HEALTH_CONSTRAINTS error | Too many instances failed for the minimum healthy hosts | Fix the underlying failures; review the deployment configuration |
| AllowTraffic fails or takes very long | Instances fail target group health checks | Check the health check path, port and security groups |
| Deployment fails with IAM or Auto Scaling errors | CodeDeploy service role lacks permissions, or iam:PassRole for the launch template | Attach the managed service role policy and PassRole |
| Destination file already exists | file_exists_behavior is DISALLOW | Set OVERWRITE or RETAIN, or clean up the files |
Troubleshoot CodeDeploy
A Deployment that Never Starts
A new Auto Scaling group is added to a deployment group. Deployments to the existing instances succeed, but the new instances stay in the Pending state until the deployment fails. The new group uses a different launch template.
Because no lifecycle events start on the new instances, the agent is not communicating with CodeDeploy. Session Manager shows that the agent is not installed: the old launch template's AMI included it, but the new one does not. Creating a State Manager association that installs the AWSCodeDeployAgent package on instances with the application tag fixes the instances and protects future launch template changes. The team also confirms that the new instance profile includes S3 read access to the artifact bucket, which the next stage of the deployment would otherwise fail on.
After an alarm-based automatic rollback, an engineer looks for the failed deployment's ID in the rollback's details and finds a different deployment ID. Why?
Show answer
A CodeDeploy rollback is a new deployment of the last known good revision, so it has its own deployment ID. The rollback information references the failed deployment.
Exam Tips
Remember for the exam
- Install and update the CodeDeploy agent at scale with Systems Manager Distributor (AWSCodeDeployAgent) and a State Manager association.
- The agent needs outbound HTTPS to CodeDeploy and S3; instances stuck in Pending with no lifecycle events point to the agent, network or instance profile.
- Rollback deploys the last known good revision as a new deployment; blue/green rollback reroutes traffic to the original environment.
- file_exists_behavior (DISALLOW, OVERWRITE, RETAIN) controls conflicts with files not placed by CodeDeploy.
- Script output is in codedeploy-agent-deployments.log; agent problems are in codedeploy-agent.log.