AWS DevOps Project Part-2

AWS DevOps Project Part-2

AWS CodeDeploy + AWS CodePipeLine

AWS CodeDeploy:

is a fully managed deployment service that automates software deployments to various compute services, such as Amazon Elastic Compute Cloud (EC2), Amazon Elastic Container Service (ECS), AWS Lambda, and your on-premises servers. Use CodeDeploy to automate software deployments, eliminating the need for error-prone manual operations.

CodeDeploy Process:

Developer Tools-->CodeDeploy-->Application-->Create application

Application name: "demo-app-application"

IAM role for CodeDeploy:

Role creation:

Role name: "code-deploy-role" & attached the following AWS managed policies-

Deployment Group:

A deployment group is a set of EC2 instances/ ECS/Lambda where an application can be deployed.

Create a deployment group:

EC2 Instance creation for adding to the deployment group:

Environment configuration:

Here we add EC2 instance in the deployment group, where the app will run.

Agent configuration:

Not configure agent at this time.

CodeDeploy agent configuration:

The CodeDeploy agent is a software package that runs on your instance and interacts with CodeDeploy to deploy your application.

This agent needs to be configured on the deployment group.

create an "install.sh" file on "demo-app-ec2".

#!/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

run this CodeDeploy agent on ec2 instance.

bash install.sh

The status of the CodeDeploy agent is : active (running)

appspec.yml:

is the configuration file for app deployment (here index.html). This app needed nginx web server, for which 2 scripts install_nginx.sh & start_nginx.sh created for installing & starting the nginx service.

version: 0.0
os: linux
files:
  - source: /
    destination: /var/www/html
hooks:
  AfterInstall:
    - location: scripts/install_nginx.sh
      timeout: 300
      runas: root
  ApplicationStart:
    - location: scripts/start_nginx.sh
      timeout: 300
      runas: root

#!/bin/bash

sudo apt-get update
sudo apt-get install -y nginx

#!/bin/bash

sudo service nginx start

Git operation for adding these files to CodeCommit:

Again build the app so that the updated source code of the application can be reached to artifact(S3).

Here, the codebuild process succeeded.

Create Deployment:

Copy the URL of artifact(where the app code is stored after CodeBuild process) of the build code.

Paste the artifact URL in Revision location

IAM Role creation for instances of deployment group:

During the deployment process, all the deployment events are in a pending state because EC2 doesn’t have any IAM role policy to retrieve the data from S3 to CodeDeploy & communication between EC2 instance and CodeDeploy. We need to defile an IAM Role with the following permissions-

Attach this IAM Role with EC2 instance:

select EC2 Instance-->actions-->security-->modify IAM role.

Restart the codedeploy agent service:

sudo service codedeploy-agent restart
sudo service codedeploy-agent status

As we restarted the Codedeploy agent service, all the events of deployment succeeded.

Browse the Public IP of EC2 instance.

Congratulations! Your application is now running on an EC2 through a public IP address. But, wait, it's not over yet. We have automated this deployment through AWS CodePipeline.

CICD Pipeline:

If we want to automate the whole CI: Continuous Integration(CodeCommit + CodeBuild) & CD: Continuous Deployment(CodeDeploy), then we need to create a CICD pipeline.

Developer Tools-->CodePipeline-->Pipelines-->Create pipeline

Pipeline name: "demo-app-pipeline"

Parameters for CodeCommit process:

Parameters for CodeBuild process:

Parameters for CodeBuild process:

Review the whole pipeline process:

After reviewing Create the pipeline.

Make some changes in the source code of app:

All the pipeline processes will be automated, and no manual intervention required.

If we refresh the public IP of EC2 instance, all the changes reflected here.

Congratulations, Your application is now running on an EC2 IP address. We have automated this deployment through AWS Code Pipeline with AWS Code Commit, Code Build & Code Deploy.

For Part-1 follow this link : https://devopscsant.hashnode.dev/aws-devops-project-part-1

Thank you for reading! Happy Learning!!

Santosh Chauhan

Did you find this article valuable?

Support Santosh Chauhan's blog by becoming a sponsor. Any amount is appreciated!