CI/CD in AWS — Configure Auto Scaling for CodeDeploy

Elman Hasa
6 min readFeb 20, 2019

Creating an Auto Scaling Group as a deploy target

In order to deploy to an EC2 using CodeDeploy, some pre-configuration is required. In this scenario, I will configure an EC2 to host an ASP .Net Core project, which will later be deployed with CodeDeploy. I will show the CodeDeploy configuration in the next article. Here I am focusing on the deploy target configuration. Going one step further, I will create an Auto Scaling group with this EC2 as launch configuration. As a result, I may use this Auto Scaling group as a deploy target for CodeDeploy.

The example project is hosted in GitHub.This article is a part of a series where I implement and improve a CI/CD pipeline in AWS using CodeBuild ,CodeDeploy and Auto Scaling.

Preparing EC2 Instance for Deploy

“Give me six hours to chop down a tree and I will spend the first four sharpening the axe.”

First, let’s define the Amazon Machine Image (AMI) I will be using for the EC2. The target instance OS will be Linux with .Net Core installed. Conveniently, Amazon provides an Image of this. The AMI currently available is “.NET Core 2.1 with Amazon Linux 2”. Of course, you may also pick any other Linux AMI and setup .Net Core installation on startup.

The minimum requirements to have an EC2 as a deploy target in CodeDeploy are

  • Create and attach an IAM role, which allows access to specific S3 buckets.
  • Install CodeDeploy Agent on the EC2

Creating the new IAM Role

From AWS, go to IAM section (Identity Access Management) and select “Roles” and then “Create Role”

On the first section , select EC2.

Next on the policies select “AmazonEC2RoleForAWSCodeDeploy” which will give the EC2 permission to the bucket to download and install CodeDeploy agent.

Create Role

On the review screen, I will name the new role EC2CodeDeployRole and confirm role creation.

Installing CodeDeploy Agent

CodeDeploy Agent allows AWS CodeDeploy to interact with our instance in order to install the components.

On Amazon Linux can be installed using the following commands

sudo yum install -y httpdsudo yum install -y gitsudo yum install -y rubysudo yum install -y wgetcd /home/ec2-userwget https://bucket-name.s3.amazonaws.com/latest/installchmod +x ./installsudo ./install auto

Where bucket-name is the name of the Amazon S3 bucket that contains the CodeDeploy Resource Kit files for your region. For example, for the US East (Ohio) Region, replace bucket-name with aws-codedeploy-us-east-2. For a list of bucket names, see Resource Kit Bucket Names by Region.

Install and Configure HTTP Server

In my case the instance will be a web-server. For this I have to install an HTTP server and configure its reverse proxy to point to my website. As an HTTP server I will be using Apache.

In case of .Net Core, when we open the main project component (dll), the website is run by default on port 5000. So I can forward all traffic from port 80 to port 5000 in my reverse proxy configuration.

In Apache server for Linux this can be done by adding the following string to the configuration file etc/httpd/conf/httpd.conf

<VirtualHost *:80>ProxyPreserveHost OnProxyPass / http://127.0.0.1:5000/ProxyPassReverse / http://127.0.0.1:5000/ErrorLog ${APACHE_LOG_DIR}helloapp-error.logCustomLog ${APACHE_LOG_DIR}helloapp-access.log common</VirtualHost>

So to recap target EC2 steps during startup:

  • The selected AMI will be “.NET Core 2.1 with Amazon Linux 2” (so .Net Core installation not required in my case)
  • Install CodeDeploy Agent
  • Install Apache
  • Configure reverse proxy configuration to forward traffic to port 5000

Here is the resulting script, which will be run on EC2 startup.

Diving in Auto Scaling Group

Now that I have an AMI and configuration ready for my EC2, I can create an Auto Scaling group on AWS, which will be my deploy target.

Login to AWS Console and navigate to EC2. From there, on the left select “Auto Scaling Groups”, and then “Create Auto Scaling Group”.

On the first screen, select “Create a new launch configuration”.

On the next step, as an AMI for EC2 I select “ .NET Core 2.1 with Amazon Linux 2”

For instance size I will select t2.micro in order to stay within free tier.

Now on creating the new launch configuration, I will name it CodeDeployLC and attach the role EC2CodeDeployRole previously created.

Expand “Advanced Details” and paste in the the script of the previous step in the “User Data” text box. This configures the script to be executed on instance startup.

On the “Security Group” section create a new security group and configure it to open the ports 22 (SSH) and 80 (HTTP) to public.

Finally, select the security key-pair and confirm the launch group configuration. This Launch group is configured to create an EC2 with the Amazon Linux OS with .Net Core pre-installed, and run our configuration script on startup of each EC2 instance.

Next, following the Auto Scaling group creation, I will name it DeployAutoScaleGroup , select the default VPC and start with 1 instance.

The advantage of Auto Scaling group is that this instance will be automatically re-provisioned in case of failure.

On next steps, I will just keep the default settings which are “Keep this group at its initial size” as scaling policy and no notifications or tags configured.

Ok! So now I have an Auto Scale group that will automatically provision an EC2 instance. On each startup, an instance will

  • Download and install CodeDeploy agent
  • Install and run Apache server
  • Configure reverse proxy and restart Apache

Now, what is left is to deploy my application on this instance, which will be done through CodeDeploy.

Posts in this Series:

Introduction: CI/CD in AWS — A pipeline with CodeBuild and CodeDeploy

Part 1: CI/CD in AWS — Continuous Integration with CodeBuild

Part 2: CI/CD in AWS — Configure Auto Scaling for CodeDeploy

Part 3: CI/CD in AWS — Continuous Deployment with CodeDeploy

--

--

Elman Hasa

A passionate developer with experience in Java, .Net and Scala. Extensive user of AWS. DevOps culture practitioner. Connect with me linkedin.com/in/elman-hasa