CodeDeploy Agent will not install via User Data

New to EC2 Instance User Data scripts and I’m struggling here. I can create an EC2 Instance from one of my Launch Templates without issue. I can then SSH into that instance and run the following commands manually, which (collectively) install the CodeDeploy Agent (I’m on Ubuntu 20.04):

sudo apt update
sudo apt -y install ruby-full
sudo apt install wget
cd /home/ubuntu
wget https://aws-codedeploy-us-east-1.s3.us-east-1.amazonaws.com/latest/install
chmod +x ./install
sudo ./install auto > /tmp/logfile

I can then test to see that CodeDeploy Agent is running via: sudo service codedeploy-agent status, and I see it is clearly running + working properly.

Now I’m trying to create a new EC2 Instance from the same Launch Template, except this time, I’m trying to define the exact same script as above in User Data. So at the very bottom of the Advanced details section, it presents you with a huge text area labeled User data. I copy and paste the exact same script from above (as-is), and click Launch instance.

It typically takes about 2 mins for my instances to become healthy + initialized, and the script above takes about 1 minute to install CodeDeploy Agent. I waited 10 minutes, SSHed onto the instance, ran my sudo service codedeploy-agent and…nothing:

sudo service codedeploy-agent
Unit codedeploy-agent.service could not be found.

What am I doing wrong here? I’m definitely waiting long enough. Same exact script, same exact launch template. The only difference is running the commands manually vs. through the User Data section of the Launch Template UI.

I figured this out on my own but I will post the solution here for good measure.

You need to specify #!/usr/bin/bash (or the path to bash on your system) and because AWS runs every command in the script as sudo, you need to remove sudo from your commands. So my final, working implementation that successfully installs CodeDeploy Agent from a User Data script is this:

#!/usr/bin/bash
apt update
apt -y install ruby-full
apt install wget
cd /home/ubuntu
wget https://aws-codedeploy-us-east-1.s3.us-east-1.amazonaws.com/latest/install
chmod +x ./install
./install auto > /tmp/logfile

And boom – it works!

You should probably do -y on the wget install too just to be safe.

For future reference you can get the cloud init logs by doing sudo journalctl -u cloud-final to see what’s failed

AWS also provides a script with a little more logic if you’re interested.

https://aws.amazon.com/premiumsupport/knowledge-center/codedeploy-agent-launch-configuration/

Yep use this.

Fun story, not all regions use the same codedeploy version. So if you use a newer version in a region where it’s not yet supported, it’ll try to downgrade.

If you’re using ASG lifecycle hooks with it, that upgrade process (which restarts the agent) often triggers a failed lifecycle hook, and termination of the EC2 instance. You can end up in quite the failure scenario.

Quick 30s check of this script appears to handle that case.

That’s not the only CodeDeploy issue, and I haven’t used it in years so maybe that’s better now.