Skip to main content

Command Palette

Search for a command to run...

Day 52: CI/CD pipeline on AWS - Part 3 🚀 ☁

Updated
3 min read

Today, we will continue our journey towards building a robust CI/CD pipeline on AWS. In Part 3, we have two important tasks to complete. Let's dive in!

We will continue from Part-2.

Task-01: Deploy index.html File on EC2 Using Nginx

To deploy our index.html file on an EC2 instance using Nginx, we need to set up the CodeDeploy agent to facilitate the deployment process.

Here are the steps to follow:

  1. Go to CodeDeploy in your AWS Management Console and select "Create application".

  1. Provide a name for your application and choose "EC2/On-premises" as the compute platform.

3.Create a deployment group by specifying a group name and selecting a service role.

  1. Choose the deployment type that suits your requirements.

  1. Select "EC2 instances" as the environment configuration and provide the name of your EC2 instance.

(Make sure you have an EC2 instance available for this step.)

  1. Choose "Never" for the AWS CodeDeploy agent option.

  1. Create the deployment group.

Now, let's set up the CodeDeploy agent on your EC2 instance by following these steps:

  1. Create a bash file on your EC2 instance and add the following code:
#!/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

  1. Execute the created script on your EC2 instance.

With the CodeDeploy agent now up and running on your EC2 instance, we can proceed to the next task.

Task-02: Add appspec.yml File to CodeCommit Repository and Complete the Deployment Process

In this task, we will add an appspec.yml file to our CodeCommit Repository to define the deployment process.

Here's an example of an appspec.yml file:

appspec.yml

version: 1
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

Additionally, we need two scripts for installation and starting Nginx:

  1. scripts/install_nginx.sh:
#!/bin/bash

sudo apt-get update
sudo apt-get install nginx -y
  1. scripts/start_nginx.sh:
#!/bin/bash

sudo service nginx start

To ensure a smooth deployment process, let's update our buildspec.yml file as well:

version: 0.1

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

artifacts: 
  files:
    - index.html
    - appspec.yml
    - scripts/install_nginx.sh
    - scripts/start_nginx.sh

Don't forget to push your code to CodeCommit and trigger the build process in CodeBuild.

Once the build is successful, it's time to create the deployment. Go to your CodeDeploy application, navigate to the application group, and click on "Create deployment".

Provide the artifacts URL for the revision location and proceed with the deployment.

To grant the EC2 instance access to the S3 bucket, create a new IAM role called "ec2-code-deploy" with the necessary permissions.

Then, attach the created role to your EC2 instance.

Finally, restart the CodeDeploy agent on your EC2 instance using the following command:

If everything is configured correctly, you should see a success message indicating that your website is now running on the EC2 instance.

Congratulations on completing Part 3 of our CI/CD pipeline on AWS series! See you in part 4.

More from this blog

Ajay Patel

116 posts