Getting started with CI/CD on AWS [Hands On] - Part 2


AWS

Reviewer: Deepak Prasad

This is the second part of getting started with CI/CD on AWS. In the previous tutorial, we learned about AWS CodeCommit and AWS CodeBuild that can be used to set up CI/CD on AWS. In this tutorial, we will do a deep dive into AWS CodeDeploy and AWS CodePipeline to implement CI/CD on AWS.

First, we will have a quick overview of AWS CodeDeploy then we will do its hands-on that will be used in our CodePipeline. After that, we will jump to AWS CodePipeline and make a two-stage pipeline for our hands-on. We will also have a look at the CodeCommit Repository that will be used for this tutorial. In the end, we will add a third stage to our pipeline that will be executed on manual approval. Now let's head straight to the topic.

 

AWS CodeDeploy Overview

  • CodeDeploy is a deployment service that automates application deployments to Amazon EC2 instances, on-premises instances, serverless Lambda functions, or Amazon ECS services. It is a service that is used to set up CI/CD on AWS.
  • It makes it easier to rapidly release new features, helps avoid downtime during application deployment, and handles the complexity of updating applications. We can consistently deploy applications across our development, test, and production environments whether deploying to Amazon EC2, AWS Lambda, or on-premises servers.
  • It helps maximize application availability during the deployment process. It introduces changes incrementally and tracks application health according to configurable rules. If there are errors, software deployments can easily be stopped and rolled back.
  • It allows us to easily launch and track the status of application deployments through the AWS Management Console or the AWS CLI. CodeDeploy gives a detailed report allowing us to view when and to where each application revision was deployed. Through CodeDeploy, we can receive live updates about our deployments by creating push notifications.
  • AWS CodeDeploy is platform and language agnostic, works with any application, and provides the same experience whether you’re deploying to Amazon EC2 or AWS Lambda. We can easily reuse our existing setup code. CodeDeploy can also integrate with our existing software release process or continuous delivery toolchain.

Now let's get started with the implementation to set up CI/CD on AWS.

 

AWS CodeDeploy Hands-on

First, we will log in to our AWS account then type CodeDeploy in the Services search bar. After that from the left pane, we will click on Applications.

Getting started with CI/CD on AWS [Hands On] - Part 2

 

Step 1:Create Application

Currently, we do not have any applications. Here we will click on Create Application.

Getting started with CI/CD on AWS [Hands On] - Part 2

 

Now we will provide the Application name i.e. MyGoLinuxCloudApplication. Then in Compute platform, we will select EC2/On-premises. And after that, we will click on Create application.

Getting started with CI/CD on AWS [Hands On] - Part 2

 

Step 2:Create Role for CodeDeploy

Now we will create a role for CodeDeploy. For that we will type IAM in the Services search bar, then from the left pane, we will click on Roles. After that, we will click on Create Role. Then in Use cases for other AWS services we will select CodeDeploy. The following policy i.e. AWSCodeDeployRole will be automatically attached to the role. Then we will click on Next. And after that, we will provide a Role name and finally click on Create role.

Getting started with CI/CD on AWS [Hands On] - Part 2

 

Step 3:Create Role for EC2 instance

For our tutorial, we will be using EC2 instance to deploy our GoLinuxCloud web application. We will have to associate our EC2 instance with a role so that it can call AWS Services on our behalf. Now we will create a Role for our EC2 instance that will be used in the Deployment Group.

For that we will type IAM in the Services search bar, then from the left pane, we will click on Roles. After that, we will click on Create Role. Then in Use cases for other AWS services we will select EC2. Then we will attach the Policy named AmazonEC2RoleforAWSCodeDeploy to our role. After that, we will click on Next. And in the end, we will provide a Role name and then click on Create role.

Getting started with CI/CD on AWS [Hands On] - Part 2

 

Step 4:Create EC2 instance

Now we will create an EC2 instance that will be used in the Deployment Group. Our GoLinuxCloud web application will be deployed to this EC2 instance. For the IAM role, we will select the role for the EC2 instance that we just created above.

Getting started with CI/CD on AWS [Hands On] - Part 2

In the User data field of our EC2 instance, we will enter the following commands. This code installs the CodeDeploy agent on our instance as it is created. To use CodeDeploy on EC2 instances or on-premises servers, the CodeDeploy agent must be installed first.

#!/bin/bash  
sudo yum -y update
sudo yum install -y ruby
sudo yum install -y aws-cli
cd /home/ec2-user
sudo wget https://aws-codedeploy-us-east-2.s3.us-east-2.amazonaws.com/latest/install
sudo chmod +x ./install
sudo ./install auto

 

Let's have a look at the security group of our EC2 instance. While configuring the inbound rules, for Type SSH we will select the Source to My IP.  Similarly, we will also add a rule for Type HTTP and then select the Source to My IP. This way we will only be able to access the web page from our local computer. In order to make it accessible from anywhere, we can select the Source to Anywhere-IPV4.

Getting started with CI/CD on AWS [Hands On] - Part 2

 

We will also add a tag to our EC2 instance with Key Name and Value MyCodePipelineDemo. This tag will be later used in the tutorial.

Getting started with CI/CD on AWS [Hands On] - Part 2

 

Step 5:Create Deployment group

Now coming back to where we left in CodeDeploy, we will create a Deployment group. Note that in an EC2/On-Premises deployment, a deployment group is a set of individual instances targeted for deployment.

Getting started with CI/CD on AWS [Hands On] - Part 2

 

First, we will provide a deployment group name i.e. MyGoLinuxCloudDeploymentGroup. Now we will enter a service role with CodeDeploy permissions that grants AWS CodeDeploy access to target our instances. This is the role we created above for CodeDeploy. After that in deployment type, we will select In-place.

Note that if we do In-place deployment, it updates the instances in the deployment group with the latest application revisions. During a deployment, each instance will be briefly taken offline for its update. We can also choose Blue/green deployment during which the instances in the deployment group are replaced with new instances and the latest application revision is deployed on them. After instances in the replacement environment are registered with a load balancer, instances from the original environment are deregistered and can be terminated.

Getting started with CI/CD on AWS [Hands On] - Part 2

 

Now we will be doing environment configurations. Here we will select Amazon EC2 instances. And then provide a  tag so that deployment is done on all the EC2 instances with the given tag. Here for key, we will enter Name and for Value, we will enter MyCodePipelineDemo. Note that we just created an EC2 instance with the mentioned tag.

Getting started with CI/CD on AWS [Hands On] - Part 2

 

Now we will choose from a list of default and custom deployment configurations. Note that a deployment configuration is a set of rules that determines how fast an application will be deployed and the success or failure conditions for deployment. Here we select CodeDeployDefault.AllAtOnce.  Next, we will disable load balancing. And in the end, we will click on Create deployment group.

Getting started with CI/CD on AWS [Hands On] - Part 2

Now let's head to the CodePipeline overview!

 

AWS CodePipeline overview

  • AWS CodePipeline is a service to set up CI/CD on AWS for fast and reliable application and infrastructure updates. It builds, tests, and deploys our code every time there is a code change, based on the release process models defined.
  •  With CodePipeline, we can quickly iterate on feedback and get new features to customers faster as it automates our software release process, allowing us to rapidly release new features to users.
  • Automating our build, test, and release process with AWS Codepipeline allows us to easily test each code change and catch bugs while they are small and simple to fix. We can assure the quality of our application or infrastructure code by running each change through our standardized release process.
  • AWS CodePipeline allows modeling the different stages of the software release process through a graphical user interface. We can specify the tests to run and the steps to deploy our application and its dependencies.
  • With AWS CodePipeline, there are no servers to provision or set up and we can immediately begin to model our software release process. CodePipeline is a fully managed continuous delivery service that connects to your existing tools and systems.

Now let's get our hands dirty with the implementation of AWS CodePipeline to set up CI/CD on AWS.

 

AWS CodePipeline hands-on

First, we will log in to our AWS account then type CodePipeline in the Services search bar. After that from the left pane, we will click on Pipelines. Then we will click on Create pipeline.

Getting started with CI/CD on AWS [Hands On] - Part 2 Getting started with CI/CD on AWS [Hands On] - Part 2

 

Step 1:Choose pipeline settings

Here we will provide the Pipeline name i.e. MyGoLinuxCloudCodePipeline. Then for the Service role, we will select New Service role. Note that it will create a service role in our account so that it can be used with our CodePipeline. After that, we will click on Next.

Getting started with CI/CD on AWS [Hands On] - Part 2

 

Step 2:Add source stage

The next step is adding a source stage. Here we will select Source Provider which in our case will be CodeCommit.  This is where we have stored our input artifacts for our pipeline. Then we will provide the Repository name i.e. MyGoLinuxCloudRepo where we have pushed our code. Then we will select the Branch name i.e. main. Then for Change Detection options, we will select Amazon CloudWatch Events which is the recommended way and for Output artifact format we will select CodePipelineDefault.

Getting started with CI/CD on AWS [Hands On] - Part 2

 

Let's have a look at our CodeCommit repository as well. We have a single-page application for GoLinuxCloud.  There is a file named appspec.yml. If our application uses the EC2/On-Premises compute platform, the AppSpec file must be a YAML-formatted file named appspec.yml and it must be placed in the root of the directory structure of an application's source code. Otherwise, deployments fail. This file contains deployment instructions, such as where to copy the files onto each instance and when to run deployment scripts

Getting started with CI/CD on AWS [Hands On] - Part 2
Getting started with CI/CD on AWS [Hands On] - Part 2

 

Step 3:Add build stage-optional

Now we can select a Build provider which is optional. We will leave it for now as our project does not need to be built. Here we will select Skip build stage.

Getting started with CI/CD on AWS [Hands On] - Part 2

 

Step 4:Add deploy stage

Next, we will select the Deploy provider. Here we will select AWS CodeDeploy. Then we will select the CodeDeploy Application name i.e. MyGoLinuxCloudApplication that we just created above. After that, we will select the Deployment Group i.e. MyGoLinuxCloudDeploymentGroup. Then we will click on Next.

Getting started with CI/CD on AWS [Hands On] - Part 2

 

Step 5:Review

The final stage is the review stage where we can view all the configurations that we just did above. In the end, we will click on Create Pipeline.

Getting started with CI/CD on AWS [Hands On] - Part 2

 

Pipeline execution (CI/CD on AWS)

Now whenever there is a code change to our repository in CodeCommit, MyGoLinuxCloudCodePipeline will execute automatically. Currently, it is a two-stage pipeline with a CodeCommit stage and a Code Deploy stage.

CI/CD on AWS

 

Testing deployment

Now it's time to test our deployment on EC2 instance. For that, we will copy the Public IPV4 address of our ec2 instance in the browser.

Getting started with CI/CD on AWS [Hands On] - Part 2

 

Here we can see that the GoLinuxCloud website has been deployed successfully onto our EC2 instance.

Getting started with CI/CD on AWS [Hands On] - Part 2

 

Add Manual Approval

Let's say we want to deploy our web application to the production environment on manual approval. So let's add one more stage to our two-stage CodePipeline. First, we will click on Edit from the top right.  Then we will click on Add stage.

Getting started with CI/CD on AWS [Hands On] - Part 2

 

Here we will provide the Stage name i.e. DeployToProduction. Then we will click on Add stage.

Getting started with CI/CD on AWS [Hands On] - Part 2

 

Next, we will provide the Action name i.e. ManualApproval. In Action provider, we will select Manual approval.

Getting started with CI/CD on AWS [Hands On] - Part 2

 

Now we will add another action to our third stage. First, we will provide the Action name i.e. DeployToProdEC2. Then in Action provider, we will select AWS CodeDeploy. In Input artifacts, we will select SourceArtifact. Next, we will provide the Application name for CodeDeploy i.e. MyGoLinuxCloudApplication. After that, we will select our Deployment Group for the production environment i.e. MyGoLinuxCloudProdDeploymentGroup. Note that this deployment group will also be created the same way. The only difference is that this time EC2 instances will be different (production instances with a different tag).

Getting started with CI/CD on AWS [Hands On] - Part 2

 

Following are the two actions that will be carried out in our DeployToProduction stage.

Getting started with CI/CD on AWS [Hands On] - Part 2

 

When the pipeline starts to execute due to a change in the CodeCommit repository. The first and second stage executes automatically whereas in the third stage we can clearly see Waiting for approval message. Now we will click on Review and approve manually to deploy the web application to the production environment.

Getting started with CI/CD on AWS [Hands On] - Part 2

 

Here we can see that pipeline has been executed successfully after manual approval and our application has been deployed to our production EC2 instance.

Getting started with CI/CD on AWS [Hands On] - Part 2

With this, we have come to the end of our tutorial on getting started with CI/CD on AWS.

 

Conclusion

In this tutorial, we learned about AWS CodeDelpoy and AWS CodePipeline to set up CI/CD on AWS.  First, we created a two-stage pipeline consisting of CodeCommit and CodeDeploy then we added a third stage that comprised of two actions, the first manual approval and the second deployment to production. In this way, we learned to implement CI/CD on AWS.

Stay tuned for some more informative tutorials coming ahead. Feel free to leave any feedback in the comments section.

 

References

 

Mahnoor Malik

Mahnoor Malik

She is a dedicated professional with deep expertise in data science, machine learning, and software development. With a strong foundation in academic and industry practice, she excels in crafting innovative backend applications and deploying them on cloud platforms like AWS, ensuring scalable, reliable, and secure solutions for the modern digital landscape.

Certifications & Credentials:

  • Machine Learning Specialization
  • AWS Certified Cloud Practitioner
  • AWS Academy Accredited Educator
  • AWS Academy Graduate
  • AWS Cloud Practitioner Essentials

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment