Sitemap

CICD for high availability using AWS CodePipeline, AWS autoscaling group, AWS load balancer.

8 min readJun 29, 2023

Here we are creating an application incorporates a highly available architecture with auto-scaling and load balancing on EC2 instances. The continuous integration and continuous deployment (CI/CD) process is orchestrated by AWS CodeCommit, CodeDeploy, and CodePipeline. This setup ensures scalability, fault tolerance, and streamlined software delivery.

ARCHITECTURE

STEPS

  1. Setting up the Production Environment

a. Create a load balancer

b. Create an autoscaling group

2. Set up the source of Code from CodeCommit

3. Deploy using CodeDeploy

4. Orchestrate CICD Pipeline using CodePipeline

STEP-1 Setting up the Production Environment

Here we will set up our production environment which is a fleet of EC2 instances managed by Auto Scaling group and a load balancer for scalability and high avaibility.

Steps to follow:-

  1. Create a Load Balancer
  2. Create a Auto Scaling group

STEP — 1 Create a Load Balancer

Here we are creating an application load balancer which is in public subnet and facing the internet which will balance the traffic load to our destination EC2 instances.

Go to Aws dashboard and search Load balancer then you will see the below given screen. Select Application load Balancer.

Change the configurations as the below given screenshot as we have given the name of our load balancer as MyELB and scheme as internet-facing as our load balancer need to be public and accessible by internet. Choose Ip address type IPv4 and listener as Http and port 80.

In availablity zones select two availablity zone from the default VPC you have our you can create your own VPC.

We are not adding any Additional services so click on Next.

Now creating the new security group for our laod balancer with the name MyElbSG and rule to allow traffic from anywhere.

Now creating Load Balancer target group, as our target group is instances then select instance and in path mention index.html .

In register target page we did not need to register any target as we will be using auto scaling group for managing and scaling our target instances.

Then Review and Create your Load Balancer.

STEP -2 Create a Auto Scaling group

Here we will be creating a auto scaling group which scale the EC2 instances in two AZ’s within the private subnet.

Go to the AutoScaling Group Dashboard and click on Create Auto Scaling Group.

Name your Auto Scaling Group as MyASG and click on create a new launch template.

On creating new launch template dashboard add name and description to your template.

Now in AMI’s click on browse more AMI and select free ubuntu AMI for your EC2's.

In Instance type add t2 micro.

Add the key Pair because if in case we need to ssh the instance to our local pc then we will need key pair.

For Security group create a new one and add the source of traffic as the security group of Load balancer.

Now in Advanced Settings , in the IAM instance profile click on create new IAM instance profile.

Click on create a new role and select the resources as per the below image.

Add all the permissions listed below.

Now add the User Data as below code and create a launch template.

#!/bin/bash
sudo su
apt update
apt upgrade -y
apt install nginx -y
sudo apt update
sudo apt-get install ruby wget -y
cd /home/ubuntu
wget https://aws-codedeploy-ap-south-1.s3.ap-south-1.amazonaws.com/latest/install
chmod +x install
sudo ./install auto

Now click on create launch template and then go back to auto scaling dashboard and select the template you have created and click Next.

In network select two AZ’s in which you need to deploy your website and keep the rest information as it is and click on Next.

In load balancing choose attach existing ELB and select the application load balancer which we had created.

In group size make desired value as 2 , minimun capacity as 2 and maximum capacity as 4.

In scaling policies select target tracking policy and make the changes as shown in the below image.

Click on Next and Review and create your auto scaling group.

Now to check that your auto scaling group is working properly go on EC2 dashboard and you will see two new instance been created.

Here your autoscaling group is created successfully and your instances are running great.

To check that your load balancer and auto scaling is running perfectly you can search for DNS of loadbalancer and IP address of instance on browser and you will see the below given page.

STEP-2 Set up the source of Code from CodeCommit

For the source of code we will create a CodeCommit repository .

Go to CodeCommit Dashboard and create a repository with name as MyRepo.

Clone the repo to you remote server to add the files to the repo.

To clone your file we need a user with the specific permissions.

So attach the below given permission the user.

Now in that user go to Https Git Cretendials for CodeCommit.

Now generate Credentials.

It will generate credentials like this.

Now in your local pc open th eterminal and line git clone command.

git clone <HTTPS-LINK-OF-CODECOMMIT-REPO>

When you will write the above command the prompt will open as shown below add your credentials in it.

Now add the index.html file in your repo by the following command and push it to repo master branch.

git add .
git commit -m "add index.html file"
git push origin master

Now this is our CodeCommit Repository with the index.html file.

Also add appspec.yml , before_script.sh and after_script.sh to the repository for the deployment.

Code for appspec.yml

version: 0.0
os: linux
files:
- source: /index.html
destination: /var/www/html/
file_exists_behavior: OVERWRITE
BeforeInstall:
- location: /before_script.sh
timeout: 300
runas: root
ApplicationStop:
- location: /after_script.sh
timeout: 300
runas: root

We after_script.sh as blank file and the below given is the cod efor before_script.sh.

rm /var/www/html/index.html

Now we will create a deployment stage.

STEP-3 Deploy using CodeDeploy

Go to CodeDeploy dashboard and click on Create application.

Give application name and select the compute platform as EC2/on-premises.

In application go to create deployment group and give your deployment group name

Add the service role which give the CodeDeploy access to your target group.

Select deployment type as In-place.

In environment select your auto scaling group.

In loadbalancer make select target group as the one which you had created.

Finally create your deployment group.

STEP-4 Orchestrate CICD Pipeline using CodePipeline

Go to CodePipeline dashboard and click on create pipeline.

Give your pipeline name and in service role select new service role.

Then Go to next and select the source of your application as CodeCommit.

Then select your repository and branch in which your code is there.

In the add build stage click on skip the buils stage or can add your own build stages that you want.

Now in the deploy stage select the Code deploy as the deploy provider and then select the application and deployment group which you had created.

Then Review and create your pipeline.

Then you will see that your pipeline is created successfully.

Now lets check that our deployment is been successful by revisiting elb dns . Just check that it is showing the index.html which you had uploded in codecommit.

In my case it is showing the below page.

Now our deployment have worked successfully so lets check if our pipeline is working successfully or not. So make some changes to your index.html file and commit the changes then check your pipeline.

Now see your DNS of load balancer and see that the changes have taken place or not.

Hurray!!!!! Our pipeline is successfully configured

Things we had learned:-

  1. Creating launch tempaltes of auto scaling group
  2. Creating auto scaling group with target tracking policy
  3. Creating application load balancer
  4. Creating CodeCommit Repository.
  5. Deploying with CodeDeploy.
  6. Working with Codepipeline.

Happy Learning :)))

--

--

No responses yet