Skip to content

Instantly share code, notes, and snippets.

@santiagoprieto
Last active April 11, 2023 19:57
Show Gist options
  • Save santiagoprieto/02e56b0c17593e81914d79a1a47fb7dd to your computer and use it in GitHub Desktop.
Save santiagoprieto/02e56b0c17593e81914d79a1a47fb7dd to your computer and use it in GitHub Desktop.
AWS: How to remove baked in CodeDeploy Agent from AMI and set as User Data

AWS How to:

Remove baked in CodeDeploy Agent from AMI and set as User Data

It is a best practice to not have the CodeDeploy Agent baked into the AMI because AWS will update to new versions quite often. Once they deprecate a version, the AMI will stop being able to deploy new instances, therefore breaking the whole pipeline.

Instead, AWS recommends this CodeDeploy Agent install to be set as User Data within the EC2 Launch Configuration. Because we use CodePipline, CodeDeploy, Auto Scaling Groups, and Load Balancers, we need to change many interlaced pieces.

Instructions:

  1. Log into an instance you want to use as base via terminal ssh.

Caution: Make sure this instance is not tied to a CodePipline deployment as it won't be able to terminate automatically.

To see log in details go to EC2 dashboard, select instance and click connect. Use the ssh code within terminal to log into instance.

  • You'll need to cd into the same folder your .pem file is located for permissions to work.
  • DO NOT UPDATE UBUNTU VERSION!! (even if server wants you to)
  1. Uninstall AWS CodeDeploy Agent with:
sudo dpkg --purge codedeploy-agent
  • details found here.
  1. Within the EC2 Dashboard, select instance without agent and create an AMI clone of it.

Select instance, go to Actions > Image > Create Image, and follow prompts.

  1. You'll need a new Launch Configuration. Copy the one you want to replace, and edit User Data.

Within EC2, select Launch Configuration you want to copy, go to Actions > Copy Launch Configurations. In Launch Configuration Details, select Edit Details. Update to newly created AMI via Edit AMI. Change User Data (within Advanced Details) so CodeDeploy Agent is installed every time:

#!/bin/bash
sudo apt-get -y update
sudo apt-get install ruby
sudo apt-get install wget
cd /home/ubuntu
wget https://aws-codedeploy-us-west-1.s3.amazonaws.com/latest/install
chmod +x ./install
sudo ./install auto
sudo service codedeploy-agent start

Select existing security group and match as needed.

  1. Switch existing Auto Scaling Group to new Launch Configuration that has new AMI as base.

Within EC2, select Auto Scaling Group, Actions > Edit > Launch Configuration.

  1. Revise CodeDeploy to make sure Auto Scaling Group is the newly edited one.

Within CodeDeploy, select desired App, Edit > see ASG.

  1. Try deployment in CodePipline (it should now work)!! :)

Testing

  1. Log into instance via terminal ssh to make sure instance is live and healthy.

if you want to see if an instance installed codedeploy-agent correctly, log in and use:

sudo service codedeploy-agent status
  1. Test server via web, postman, and/or iOS to make sure it's all fine.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment