16

I want to autoscale the infrastructure when load gets high. I am running my infrastructure on AWS. I have a requirement where I need to pull the application code from Github when autoscaling happens. As the code changes frequently, we can't take an AMI and launch an instance from that AMI. So I want to pull the latest code from repositories. AWS just launched a service called AWS CodeDeploy. How can I use this service to automate the process of pulling the code when the instances start?

P.S. I have written an init script to automatically attach an EIP, whitelist that IP on different Security Groups and put the instance under a load-balancer when the instance boots and revoking everything when instance is terminated in autoscaling.

EmptyArsenal
  • 6,518
  • 3
  • 28
  • 51
Nikunj Shukla
  • 320
  • 3
  • 11

1 Answers1

26

CodeDeploy is a great solution to your problem. If configured correctly, it can automatically deploy to new EC2 instances that are spun up by Autoscaling. To get this working you'll need three things:

  1. An Auto Scaling group that launches instances with the latest host agent installed and running.
  2. A Code Deploy deployment group configured to deploy to that Auto Scaling group (as a Auto Scaling group not by tags).
  3. A target revision to deploy automatically to new instances. If there is no target revision, Code Deploy will see the instance launch, but won't deploy anything.

Here's a tutorial you can use to help get started: Tutorial: Using AWS CodeDeploy to Deploy an Application to an Auto Scaling Group. That tutorial will walk you through baking an AMI with the agent installed and setting up the deployment group to deploy your code to new instances.

If you do bake a AMI with the agent pre-installed, you would need to update that image regularly with agent releases. Once the agent is installed it will update itself, but Auto Scaling might fail your instance launches if the agent version is no longer supported by Code Deploy. For actual production use, I would recommend not baking an AMI and instead installing the latest agent when your instances are launched. (The tutorial should be updated to use this method soon.)

You can setup your instances to automatically download and run the latest installer on boot. Essentially, you paste in a shell script as user data when creating the Auto Scaling group.

For example, I tested the following script on Amazon Linux (taken from Set Up a New Amazon EC2 Instance to Work with AWS CodeDeploy):

#!/bin/bash
yum -y update
yum install -y aws-cli
cd /home/ec2-user
aws s3 cp s3://aws-codedeploy-us-east-1/latest/install . --region us-east-1
chmod +x ./install
./install auto

You should be able to paste this in as user data when you are creating the Auto Scaling group. For Auto Scaling, you set that up when creating the launch configuration under configure details -> advanced details.

To set up the deployment group and set the target revision:

  1. Create or edit a deployment group to include the Auto Scaling group. (Note: the Auto Scaling group must exist first for this to work.)
  2. Make sure there is at least 1 instance in the Auto Scaling group.
  3. Deploy the artifact bundle you want to be automatically deployed to the deployment group.
  4. Assuming step 3 was successful, the deployment group will now have that bundle set as it's target revision. For automatic deployments, this becomes the known good revision to deploy.

New instances that are launched by Auto Scaling in that Auto Scaling group will have the target revision of the deployment group automatically deployed to them. Revisions from failed manual deployments won't be automatically deployed automatically.

Jonathan Turpie
  • 1,247
  • 10
  • 15
  • How does this deployment strategy work when deploying a new version of an application? For example, say I write a user data script which pulls the 'latest' version of the packaged application from S3. Now, when I run a build job (e.g. via Travis-CI) to deploy my app, it will package and upload the app to S3, and then I want each instance in the group to be shutdown and replaced (preferably one-by-one). How can that be achieved? – Josh Sep 16 '15 at 20:25
  • This should be it's own questions: How do I trigger deployments in CodeDeploy from TravisCI? How do I do a blue-green deployment with CodeDeploy? It would be a bit lengthy do go into in the comments. – Jonathan Turpie Sep 17 '15 at 15:16
  • I've created a new question here, in case you want to answer ;) http://stackoverflow.com/questions/32635428/what-is-a-good-way-to-deploy-a-distributed-application-using-codedeploy-and-a-ci – Josh Sep 17 '15 at 16:29
  • Thanks! I was missing that I needed to install the CodeDeploy agent (via the UserData script in the Launch configuration! If I git push to codeCommit and that kicks of a deployment via a codePipeline, will subsequent newly spun-up instances get the revision from codeCommit's repository, or still the old revision? (I know this question pre-dates CodePipeline's release but thought someone may know) – NULL pointer May 17 '19 at 06:04