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

This is Day 52 of #90daysofdevops challenge

ยท

5 min read

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

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:

  • CodeDeploy

  • CodePipeline

  • S3

What is CodeDeploy?

AWS CodeDeploy is a deployment service that automates application deployments to Amazon EC2 instances, on-premises instances, serverless Lambda functions, or Amazon ECS services.

CodeDeploy can deploy application content that runs on a server and is stored in Amazon S3 buckets, GitHub repositories, or Bitbucket repositories. CodeDeploy can also deploy a serverless Lambda function. You do not need to make changes to your existing code before you can use CodeDeploy.

Task 1:

-> Read about Appspec.yaml file for CodeDeploy.

The AppSpec file provides an easy way to specify deployment instructions for AWS CodeDeploy in a declarative YAML format. It allows you to customize the deployment process to meet the specific needs of your application.

-> Deploy index.html file on EC2 machine using nginx

Please refer day 50 and day 51 for better understandings. Here is the link for Day 50 and Part 51.

  • Create an instance or restart the previous one.

  • Now, let's create a CodeDeploy application. In AWS Console navigate to the CodeDeploy service.

  • Click on "Create application" give it a name and select EC2/On-Premises as a compute platform.

  • Before creating a deployment group, we have to create an IAM role and give all the necessary permissions to it.

  • Update the Trust relationship in order to grant AWS CodeDeploy access to your target instances.

    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Effect": "Allow",
                "Principal": {
                    "Service": "codedeploy.amazonaws.com"
                },
                "Action": "sts:AssumeRole"
            }
        ]
    }
  • Now, let's create a deployment group, in the CodeDeply application click on "Create deployment group" and give it a name.

  • Click on "Create deployment group".

  • Now we need to install the CodeDeploy agent on the server for which we need to write a script file with all the dependencies.
#!/bin/bash 

# This script installs the CodeDeploy agent and its prerequisites on Ubuntu 22.04.  

# Update package list
sudo apt-get update 

# Install Ruby and its dependencies
sudo apt-get install ruby-full ruby-webrick wget -y 

# Change to temporary directory
cd /tmp 

# Download the CodeDeploy agent package
wget https://aws-codedeploy-us-east-1.s3.us-east-1.amazonaws.com/releases/codedeploy-agent_1.3.2-1902_all.deb 

# Create a directory to extract the package contents
mkdir codedeploy-agent_1.3.2-1902_ubuntu22 

# Extract the package contents
dpkg-deb -R codedeploy-agent_1.3.2-1902_all.deb codedeploy-agent_1.3.2-1902_ubuntu22 

# Update the package dependencies to use Ruby 3.0
sed 's/Depends:.*/Depends:ruby3.0/' -i ./codedeploy-agent_1.3.2-1902_ubuntu22/DEBIAN/control 

# Repackage the updated package
dpkg-deb -b codedeploy-agent_1.3.2-1902_ubuntu22/ 

# Install the updated package
sudo dpkg -i codedeploy-agent_1.3.2-1902_ubuntu22.deb 

# List all systemd units that contain "codedeploy" in their name
systemctl list-units --type=service | grep codedeploy 

# Check the status of the CodeDeploy agent service
sudo service codedeploy-agent status

  • Let's run this script.

Task 2: Add appspec.yaml file to CodeCommit Repository and complete the deployment process.

  • AppSpec file is required to create a bridge between the AWS CodeDeploy and EC2 instance. Let's create one...
version: 0.0               #specifies the version number of the file format.
os: linux                  #specifies the operating system to be used.
files:                     #is an array of files to be copied from the source to the destination directory.
  - source: /               #specifies the source directory.
destination: /var/www/html #specifies the destination directory where the files will be copied to.
  hooks:                   #is an array of scripts to be executed at different points during the deployment process.
    AfterInstall:          #specifies a script to be executed after the installation of the application.
      - location: scripts/install_nginx.sh #specifies the location of the script file to be executed
        timeout: 300       #specifies the maximum amount of time in seconds that the script can run for.
        runas: root        #specifies the user that the script should be run as.
    ApplicationStart:      #specifies a script to be executed after the application has started.
      - location: scripts/start_nginx.sh
        timeout: 300
        runas: root

  • Now we have to create 2 scripts for installing nginx and starting nginx.

  • install_nginx.sh

#!/bin/bash

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

#!/bin/bash

sudo systemctl start nginx
sudo systemctl enable nginx

  • We also have to change the buildspec.yaml file so that the CodeBuild will build the appspec.yml file and transfer the artifact to S3 bucket.
version: 0.2

phases:
  install:
    commands:
      - echo Installing NGINX - echo apt-get install NGINX
      - sudo apt-get update
      - sudo apt-get install nginx -y
  build:
    commands:
      - echo Build started on date
      - cp index.html /var/www/html
  post_build:
    commands:
      - echo Configuring NGINX
artifacts:
    files:
      - "**/*"

  • Let's push all the changes to the CodeCommit repository.

  • You can view the new code files in the CodeCommit repository.

  • Let's edit the build, select the artifacts and update it after that start the build.

  • Now, We have to Create a role for giving access to the EC2 instance with all the necessary permission policies as shown below.

  • Now, navigate to the instance and modify the IAM role. Select the IAM role created above.

  • Now create a deployment in the deployment group that we made previously.

  • Click on "Create deployment"

  • Provide the S3 artifactory details. This will pull the code at the time of CodeDeploy. Then create the deployment.

  • Click on "Create deployment"

  • Restart the codedeploy-agent in the EC2 instance.

  • Once the codedeploy-agent restarted, the deployment will get completed successfully.

"Thank you for enjoying my DevOps blog! Your positive response fuels my passion to dive deeper into technology and innovation.

Stay tuned for more captivating DevOps articles, where we'll explore this dynamic field together. Follow me on Hashnode and connect on LinkedIn (https://www.linkedin.com/in/som-shanker-pandey/) for the latest updates and discussions.

Did you find this article valuable?

Support Som Pandey's blog by becoming a sponsor. Any amount is appreciated!

ย