Streamlining App Deployment Using AWS: CodeCommit, CodeBuild, CodeDeploy, and CodePipeline

Vijesh VijayanVijesh Vijayan
4 min read

Objective:

This is a demonstration of creating a simple, automated deployment pipeline using AWS Devops services like CodeCommit, CodeBuild, CodeDeploy, and CodePipeline.

Prerequisites:

Need AWS Account.
Familiar with concepts of AWS CodeCommit, CodeBuild, CodeDeploy and CodePipeline.
Familiar with CICD.

Source Code (Github):

https://github.com/vijeshnair89/blog-aws.git

Concepts:

AWS CodeCommit:

CodeCommit is AWS’s version control system, similar to GitHub. It securely stores your source code and keeps track of changes. In this demonstration we build a repository in codecommit and use Git to push the source code to the repository.

AWS CodeBuild:

This is an aws managed build service which compiles the code, runs test and creates build artifacts i.e. packages or executables. CodeBuild relies on the specifications defined in buildspec.yml file to perform the build. The source code is fetched from Codecommit repository.

AWS CodeDeploy:

CodeDeploy automates the deployment of the application to EC2 instances, ECS, AWS Lambda, EKS, etc. In this demonstration the deployment is done to AWS EC2 instance. We need to define the appspec.yml file in the root path of the repository which defines the deployment instructions to CodeDeploy.

AWS CodePipeline:

CodePipeline integrates the CodeCommit, CodeBuild, CodeDeploy as one unit and creates a connection between each process to automate the whole process, thus creating a full deployment pipeline.

Steps:

→ Create a CodeCommit repository and push the code using Git to the CodeCommit Repository.

→ Create a CodeBuild service and configure the source as CodeCommit Repository. The codebuild role should have access to S3 and SSM (explained below).

→ Create an EC2 Instance and configure CodeDeploy Agent which will allow CodeDeploy to deploy applications on the instances.

→ Create IAM roles so that both the EC2 instance and CodeDeploy service should be able to access each other. Also provide S3 access to the CodeDeploy roles so that it will get the access to fetch the build artifact from S3 for deployment.

→ Create CodeDeploy Service and execute the deployment and verify if the container is getting created in the EC2 instance.

→ Finally create the CodePipeline and configure stages as CodeCommit - CodeBuild - CodeDeploy and verify end to end deployment.

S3 Setup

The artifacts created using the build process is stored on S3 buckets.

Systems Manager

Systems manager is a service which can be used to store the docker credentials for the docker images to be build and published to docker-hub.

CodeCommit Repository

CodeBuild Setup

Before triggering code build need to give the codebuild service role the below access:
S3 → build artifact needs to be stored on s3
SSM → since the code build will be getting the docker credentials information from systems manager.

Start the build…

Hence Code Build is successfully able to build the code and create the build artifactory.

Code Deploy Setup

Setup an EC2 instance and install docker and Code Deploy agent.

To install code deploy agent either we can provide the below script in the user-data section of the instance while creating it or follow the documentation to install code deploy through the command line once the instance is up and running. Here i have used below script to install docker and code deploy agent:

#!/bin/bash
sudo apt update
sudo apt install ruby-full -y
sudo apt install wget -y
wget https://aws-codedeploy-us-east-1.s3.us-east-1.amazonaws.com/latest/install
chmod +x ./install
sudo ./install auto
sudo apt install docker.io -y
usermod -aG docker ubuntu
systemctl status codedeploy-agent

To setup CodeDeploy agent using command line refer the below link:
https://docs.aws.amazon.com/codedeploy/latest/userguide/codedeploy-agent-operations-install.html

Create 2 IAM roles each for ec2 instance and codedeploy service respectively to communicate with each other:

ec2-codedeploy-role → To enable ec2 to codedeploy access

codedeploy-ec2-role → To enable codedeploy service to ec2 access.

Code Deploy Service

1) Create Application
2) Create Deployment Group
3) Create Deployments

Create Deployment Group…

Since we installed code deploy agent so no need to install agent again…

Create deployment…

The above revision location can be fetched from s3 as below:

Hence the deployment is succeeded, this means the docker container should be up and running in the ec2 instance.

Now we will integrate the whole deployment process using code pipeline.

Code Pipeline Setup

Once reviewed and submitted the pipeline will trigger. This is only for the first time the pipeline is created, afterwards whenever a commit happens in the repository the pipeline will trigger automatically.

Lets open the application using the instance ip and port as 8080 where the application is exposed to.

Hence the pipeline is working as expected. Next time whenever a commit happens in the codecommit repo the pipeline triggers and code is deployed as container in the instance.

Thanks for reading……

0
Subscribe to my newsletter

Read articles from Vijesh Vijayan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vijesh Vijayan
Vijesh Vijayan

An Experienced IT Professional with a curiosity in learning and sharing knowledge.