Day 52/53 -Complete CI/CD pipeline Project on AWS ๐Ÿš€ โ˜

Day 52/53 -Complete CI/CD pipeline Project on AWS ๐Ÿš€ โ˜

ยท

7 min read

On your journey of making a CI/CD pipeline on AWS with these tools, you completed AWS CodeCommit & CodeBuild.

Next few days you'll learn these tools/services:

  1. CodeCommit

  2. CodeBuild

  3. CodeDeploy

  4. CodePipeline

  5. S3

CodeCommit

CodeCommit is a managed source control service by AWS that allows users to store, manage, and version their source code and artefacts securely and at scale. It supports Git, integrates with other AWS services, enables collaboration through branch and merge workflows, and provides audit logs and compliance reports to meet regulatory requirements and track changes. Overall, CodeCommit provides developers with a reliable and efficient way to manage their codebase and set up a CI/CD pipeline for their software development projects.

  1. Navigate to CodeCommit and Click on Create repository

  2. Provide the repository name and click on create

  1. The repository is successfully created

  1. As a root user you cannot access the CodeCommit repo, so we need to create an IAM user with certain policies

Creating IAM user of CodeCommit repo

  1. Navigate to IAM > Users

  2. You can create a new user by clicking Add Users

  3. Give him an AWSCodeCommitFullAccess Policy permissions

  4. Click on Create user

Click on Next

Set permissions and select Attach policies directly

Apply AWSCodeCommitFullAccess and click on Next

  1. Review the user name, and permissions and click on Create user

  1. Download .csv file which contains our IAM user login and password details.

Generating GitCredentials for CodeCommit repo

  1. Select and open the IAM User that you have created and go to Security Credentials

  1. Under Security credentials > HTTPS Git credentials for AWS CodeCommit

Click on Generate credentials

  1. Download credentials and click on Close

  2. HTTPS GIT credentials are created now.

  1. These credentials are used to commit the source files to the CodeCommit repo

Configure AWS CLI in EC2 Instance.

$ curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
sudo apt install unzip
unzip awscliv2.zip
sudo ./aws/install
aws --version
aws configure

Configure the Access and Secret keys that we generated from the IAM user

Now clone the repository from CodeCommit to the i

Cloning the repository from CodeCommit to local

  1. Open your repository from the code commit

  2. Select the repository and click on the Clone URL > Clone HTTPS

  3. Once you do this repo URL will be copied and we will be cloning it in local

  1. Connect to the EC2 instance now and use the Git clone command to clone the repository of CodeCommit to local

  2. I will be using Ubuntu 22 version for EC2 instance here

$ git clone <your-codecommit-repo-clone-https-url>
  1. Use the HTTPS GIT Credentials that you created and downloaded from the IAM User

  1. We have successfully cloned the empty repo named myrepo from AWS CodeCommit

Note: Make sure you add proper policies for the IAM role

Adding Source files to the repository in CodeCommit

  1. Clone the git repo https://github.com/rjthapaa/AWS-DevOps-Project.git to the local. contains the sample app files like

  2. Push all the files from local to the CodeCommit

  1. Commits are done, check the CodeCommit repo

  1. appspec/buildspec.yml, index.html and script files are required during CodeBuild/Deploy

CodeBuild

AWS CodeBuild is a fully managed build service in the cloud. CodeBuild compiles your source code, runs unit tests, and produces artefacts that are ready to deploy. CodeBuild eliminates the need to provision, manage, and scale your build servers.

A Buildspec file is a YAML file that defines the build process for your CodeBuild project. It contains a series of commands that CodeBuild will execute to build and package your application.

  1. Codebuild will be built from the CodeCommit repo files only.

  2. Click on Build Projects> Create Build project

  1. Provide the source details

  1. Provide the Environment details

  1. Check on New service role and a default service will be created.

    Services are used to communicate by CodeBuild with CodeCommit, CodeDeploy, S3, artifact and other resources

  2. Also check on Use a buildspec file, because we are using a buildspec from repo

  1. Batch configuration be optional and check on Cloudwatch Logs

  1. Leave the rest to defaults and click on Create build project

  1. Build Project is Successfully created

Setting up CodeDeploy

  1. We have done CodeCommit and CodeBuild, and now is time to CodeDepoly

  2. In CodeDeploy, go to Applications and click on Create Application

  3. Provide the Application name and ComputePlatform.

  4. Click on Create Application

  1. An application is successfully created.

  1. To run or deploy one or more applications on multiple servers we need a deployment group

Create deployment group

  1. Click on create deployment group, give it a name

  2. Under service role , we need to create a new role and attach it here

  1. Let's navigate to IAM and create a role code-deploy-service-role with the below permissions

  1. Once the role is created get back to CodeDeploy you will be able to view a service role that we have created, select it

  1. Under Environment Configuration, the deployment will be based on AMAZON EC2 instances, so select it

  2. Select the Key as Name and Value will be the EC2 instance name that we are using for deployment.

  1. Uncheck on Enable the Load Balancer and click on Create deployment group

If you are getting any errors while creating a Deployment Group refer to this link

  1. Deployment group is successfully created.

Installing CodeDeploy agent on EC2 to deploy code

1. To deploy your app to EC2, CodeDeploy needs an agent which deploys the code on your EC2.

  1. Before installing the Agent, we need to create IAM role for EC2 instance and attach it.

  2. Create a role with the below polices

AmazonEC2FullAccess
AmazonS3FullAccess
AWSCodeDeployFullAccess
  1. The role is created and now we need to attach this role to the EC2 instance

  2. Go to the EC2 instance and select it. Click on> Actions > Security > Modify IAM role

  1. Update the role we created for this instance and click on Update IAM role

  1. Connect to the EC2 instance and write a shell script file to install the CodeDeployAgent

  2. vi agentinstall.sh with the below contents and run it.

#!/bin/bash 
# This installs the CodeDeploy agent and its prerequisites on Ubuntu 22.04.  
sudo apt-get update 
sudo apt-get install ruby-full ruby-webrick wget -y 
cd /tmp 
wget https://aws-codedeploy-us-east-1.s3.us-east-1.amazonaws.com/releases/codedeploy-agent_1.3.2-1902_all.deb 
mkdir codedeploy-agent_1.3.2-1902_ubuntu22 
dpkg-deb -R codedeploy-agent_1.3.2-1902_all.deb codedeploy-agent_1.3.2-1902_ubuntu22 
sed 's/Depends:.*/Depends:ruby3.0/' -i ./codedeploy-agent_1.3.2-1902_ubuntu22/DEBIAN/control 
dpkg-deb -b codedeploy-agent_1.3.2-1902_ubuntu22/ 
sudo dpkg -i codedeploy-agent_1.3.2-1902_ubuntu22.deb 
systemctl list-units --type=service | grep codedeploy 
sudo service codedeploy-agent status

Note : This script file is for Ubuntu 22.04 version

  1. Run this command to install the DeployAgent sh agentinstall.sh

  1. DeployAgent is installed in EC2 and Active running.

Creating a CI/CD Pipeline

  1. Navigate to CodePipeline > Create Pipeline

  2. Provide a name for Pipeline and

  3. Select the new service role and a new service will by created by the Pipeline name that we provided for Pipeline project.

  1. Click on Next

  2. Add details about Source provider, here we are taking all the source code from AWS CodeCommit repo name myrepo from master branch

  1. Click on Next

  2. Add details about Build. So the provider AWS CodeBuild the project name is CICD

  1. Add details about Deploy. So the provider AWS CodeDeploy the project name CICDDeploy and Deployment group is CICDdeploymentgroup

  1. Review all the details we provided and

Click on Create pipeline

Source, Build and Deploy all stages are successfull

Now you can access the application by using the PublicIP of Ec2instance


Try this sample app

https://github.com/rjthapaa/sample-CICD

https://github.com/rjthapaa/React-SourceCode-For-CodePipeline

in Amazon Linux for your self.

ย