Day 52: Your CI/CD pipeline on AWS - Part 3 ๐Ÿš€ โ˜

Day 52: Your CI/CD pipeline on AWS - Part 3 ๐Ÿš€ โ˜

ยท

4 min read

This is #90DaysofDevops challenge under the guidance of Shubham Londhe sir.

Welcome to Day 52 of the #90DaysOfDevOps Challenge. Today, we'll explore the mighty realm of AWS CodeDeploy and uncover its potential to transform your CI/CD pipeline on AWS. Let's dive in!

Introduction:

In the world of DevOps, automating application deployments is crucial for efficiency and reliability. AWS CodeDeploy is a powerful tool that streamlines this process, allowing you to deploy your applications seamlessly to various environments. In this guide, we'll walk through the basics of CodeDeploy and demonstrate how to deploy a simple HTML file to an EC2 instance using Nginx.

What is CodeDeploy?

AWS CodeDeploy is a fully managed deployment service offered by Amazon Web Services (AWS). It streamlines the application deployment process across various computing services like Amazon EC2 instances, AWS Fargate, and on-premises servers. CodeDeploy ensures swift, dependable, and uniform deployments.

CodeDeploy empowers you to define deployment configurations and employ different deployment strategies such as rolling updates, blue/green deployments, and in-place deployments. Its seamless integration with other AWS services makes it a pivotal element of your CI/CD pipeline.

With CodeDeploy, managing deployments for applications built in diverse programming languages and frameworks becomes effortless. It provides flexibility in defining pre- and post-deployment hooks, allowing for custom actions like running scripts or executing tests before or after each deployment.

Moreover, when utilizing AWS CodeDeploy, you can incorporate an appspec.yaml file. This file outlines deployment instructions and lifecycle events for your application. Here's a sample appspec.yaml file:

version: 0.0
os: linux

files:
  - source: /
    destination: /var/www/html/myapp

permissions:
  - object: /var/www/html/myapp
    pattern: "**"
    owner: nginx
    group: nginx
    mode: 755

hooks:
  BeforeInstall:
    - location: scripts/install_nginx.sh
      timeout: 300
      runas: root

  AfterInstall:
    - location: scripts/start_nginx.sh
      timeout: 300
      runas: root

Task-01: Deploying an HTML File to EC2 Using Nginx

Create a CodeDeploy Application:

Navigate to the AWS Management Console and select CodeDeploy.

Click on "Applications" and then "Create application".

Choose "EC2/on premises" as the compute platform and proceed with creating the application.

The application has been created successfully.

Create a Deployment Group:

After creating the application, click on "Create a Deployment Group".

Provide a name for the deployment group and create a new IAM role for CodeDeploy if necessary.

Once created, go to your EC2 instance and assign the IAM role:

Specify the details of your EC2 instance in the environment configuration.

Install the CodeDeploy Agent:

SSH into your Ubuntu EC2 instance.

vim install.sh
#!/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

Create and execute a script to install the CodeDeploy agent. This script will download and install the necessary packages.

bash install.sh

Edit the HTML File:

If needed, modify the HTML file that you want to deploy.

Task-02: Completing the Deployment Process

Create an appspec.yaml File:

Ensure the appspec.yaml file includes instructions for installing and starting Nginx. Here's an example:

version: 0.0
os: linux

files:
  - source: /
    destination: /var/www/html/myapp

permissions:
  - object: /var/www/html/myapp
    pattern: "**"
    owner: nginx
    group: nginx
    mode: 755

hooks:
  BeforeInstall:
    - location: scripts/install_nginx.sh
      timeout: 300
      runas: root

This appspec.yaml file defines deployment instructions for CodeDeploy, including copying files and executing a script to install Nginx before deployment.

Build the Application with CodeBuild:

Ensure the appspec.yaml file is included in the artifacts for CodeBuild. Here's an example configuration in your buildspec.yaml:

version: 0.2

artifacts:
  files:
    - appspec.yaml
    - myapp/*  # Assuming myapp is the directory containing your application files

This configuration ensures that the appspec.yaml file is included in the artifacts generated by CodeBuild.

Create a Deployment in CodeDeploy:

Go to the CodeDeploy console and select your application.

Create a new deployment, specifying the revision location from CodeBuild.

Provide the S3 URL for the deployment configuration and create the deployment.

Update EC2 IAM Role:

Create a new IAM role that allows EC2 instances to retrieve data from S3. Example IAM policy allowing S3 access:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::your-bucket/*"
        }
    ]
}

Attach this policy to a new IAM role or update the existing EC2 instance's IAM role.

Restart CodeDeploy Agent:

SSH into your EC2 instance and restart the CodeDeploy agent:

sudo service codedeploy-agent restart

This ensures that the agent picks up the new IAM permissions.

Conclusion:

AWS CodeDeploy simplifies the deployment process, allowing you to deploy applications with ease and reliability. By following the steps outlined in this guide, you can deploy your applications efficiently and ensure a smooth deployment process.

๐Ÿ’ก
If you found this helpful, a thumbs up๐Ÿ‘ and a follow would mean a lot!

Thank you for taking the time to read! ๐Ÿ’š

Did you find this article valuable?

Support Akash Dhengale by becoming a sponsor. Any amount is appreciated!

ย