Blue/Green Deployment for Autoscaling Groups with CodePipeline, CodeBuild and CodeDeploy (Part 3/7)

Leon Jalfon
RhinOps by Sela
Published in
6 min readApr 30, 2020

In this 7-parts tutorial we will create an initial environment composed by an autoscaling group with two ubuntu machines behind a load balancer, then we will create a CI/CD pipeline to provide blue/green deployments using CodeBuild, CodeDeploy and CodePipeline.

Part 3: Setup the initial environment

In this section we will setup our initial environment by configuring:

  1. Launch Configuration
  2. Autoscaling Group
  3. Target Group
  4. Load Balancer
  5. Configure Servers

Create a Launch Configuration

Browse to “Launch Configurations” (under Auto Scaling) in the EC2 portal

Then click in “Create launch configuration”

Select “Ubuntu 18.04” as the AMI template for the instances

Select “t2.micro” as the instance type

Configure the launch configuration with the following details and click on “Next: Add storage”:

  • Name: bluegreen-launchconfig
  • IAM Role: bluegreen-ec2-role
  • Advanced -> User data:
  • Set a startup script for the instances to install the codedeploy agent
#!/bin/bash 
apt-get -y update
apt-get -y install ruby
apt-get -y install wget
cd /home/ubuntu
wget https://aws-codedeploy-ap-southeast-1.s3.amazonaws.com/latest/install
chmod +x ./install
./install auto
systemctl enable codedeploy-agent
systemctl start codedeploy-agent
systemctl restart codedeploy-agent

Then you will be asked for the storage configuration, keep the defaults and click “Next: Configure Security Group”

Choose the security group that you created before (bluegreen-sg) and click “Review”

Review the details and click on “Create launch configuration”

Choose the SSH key pair created before (bluegreen-key), check the confirmation box and click on “Create launch configuration”

Finally in the confirmation page click on “Create an Auto Scaling group using this launch configuration”

Configure an Autoscaling Group

Configure the autoscaling group with the parameters below and click on “Next: Configure scaling policies”

  • Group Name: bluegreen-asg
  • Group Size: 2
  • Network: <your-vpc>
  • Subnets: <your-subnets>

Choose “Keep this group at its initial size” (default) and click on “Next: Configure notifications”

We don’t need any notification so just click on “Next: Configure Tags”

Set the following tags and click on “Review” (those tags will be added to the instances created by the autos calling group)

  • Name=bluegreen
  • App=bluegreen

Review the details and click on “Create Auto Scaling group”

Browse to “Instances” under “EC2” to see how your servers are being created by the auto scaling group

Create a Target Group

Browse to “Target Groups” (under Load Balancing) in the EC2 portal

Click on “Create target group”

Configure the target group with the details below:

  • Name: bluegreen-tg
  • Target Type: Instance
  • Protocol: HTTP
  • Port: 80
  • VPC: <your-pvc>

Select the created target group, open the tab “Targets” and click on “Edit” to add the instances to the target group

Search the instances created by the auto scaling group (“bluegreen”) and register them in the target group

Setup a Load Balancer

Browse to “Load Balancers” under “Load Balancing” in the EC2 portal

Click on “Create Load Balancer”

Let’s create an Application Load Balancer by choose “HTTP/HTTPS”

Configure the load balancer with the details below, then click on “Next: Configure Security Settings”

  • Name: bluegreen-lb
  • Scheme (default): internet-facing
  • IP adress type (default): ipv4
  • Listeners (default): HTTP-80
  • Availability Zones
  • VPC: <your-vpc>
  • Subnets: <your-subnets>

Click on “Next: Configure Security Groups”

Select the security group “bluegreen-sg” and click on “Next: Configure Routing”

Configure routing with the parameters below, then click on “Next: Register Targets”

  • Target Group: Existing target group
  • Name: bluegreen-tg
  • Target Type: Instance
  • Protocol: HTTP
  • Port: 80

Ensure that the instances are registered and click on “Next: Review”

Review the load balancer details and click on “Create”

Configure Servers

Get the public ip of your instances in the EC2 portal

Ensure that the downloaded ssh key have the right permissions by run

chmod 400 ~/Downloads/bluegreen-key.pem

Access the first instance by using the command

ssh ubuntu@<instance1-public-ip> -i ~/Downloads/bluegreen-key.pem

Ensure that the CodeDeploy agent is up and running

systemctl status codedeploy-agent

Use the command below to install Apache

sudo apt-get install -y apache2

Browse to the first server, you should see the Apache default page

Exit from the first instance and access to the second one

ssh ubuntu@<instance2-public-ip> -i ~/Downloads/bluegreen-key.pem

Use the command below to install Apache

sudo apt-get install -y apache2

Browse to the second server, you should see the Apache default page

Browse to the load balancer to ensure that it’s working as expected

--

--