Select Language:
If you’re having trouble with the CodeDeploy agent not starting properly on your EC2 instance, there is a simple fix that involves updating your user data script. When the CodeDeploy agent installs, it sometimes creates a service file that isn’t automatically located by systemd, especially on Ubuntu systems. To fix this, you can adjust your script to ensure the agent is registered correctly and starts without issues.
Start by updating your user data commands to include these steps. First, run system updates and install the necessary packages. Then, download and run the CodeDeploy installer. After installation, add a short delay to ensure everything finishes properly. Instead of starting the agent with the default command, use the service management commands suited for Ubuntu. Specifically, use the command to restart the service, which also starts it if needed. You should also verify the service is running with a check in a loop to confirm it’s active before proceeding.
Here is an example of what your updated script could look like:
user_data = ec2.UserData.for_linux()
user_data.add_commands(
“sudo apt update -y”,
“sudo apt install ruby wget -y”,
“cd /tmp”,
“wget https://aws-codedeploy-us-east-1.s3.amazonaws.com/latest/install“,
“chmod +x ./install”,
“sudo ./install auto”,
Wait for the installation to finish
“sleep 10”,
Restart the CodeDeploy agent using service command
“sudo service codedeploy-agent restart”,
Confirm that the agent is running
“until sudo service codedeploy-agent status | grep -q ‘running’; do”,
” echo ‘Waiting for CodeDeploy agent to start…'”,
” sleep 5″,
“done”,
“echo ‘CodeDeploy agent is now running'”
)
Instead of using systemctl, which is common on newer Linux distributions, this script uses service commands that work well on Amazon Linux and Ubuntu. The script also includes a loop that keeps checking if the agent is active, helping to prevent deployment issues caused by a non-running agent.
Make sure your EC2 instance has the right permissions by attaching the proper IAM role for CodeDeploy. Also, remember to tag your instance appropriately so CodeDeploy knows where to deploy.
Following these steps should solve the issue, ensuring your CodeDeploy agent is installed, started, and ready for deployments smoothly.



