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

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](https://hashnode.com/post/clj57f9u0000209jn4xcgajo6).

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".
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687359405625/43d18f13-2b80-4ce2-8857-d5d5cb75d425.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687359476987/4d80a15a-4604-4a01-8182-8c41e9645214.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687359554288/b106bfef-a394-46a9-9fde-114ec1717e08.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687360169141/c07ee5b8-64fb-45ed-8fb9-700ef3a5ef31.png align="center")

1. Choose the deployment type that suits your requirements.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687360195420/78b853f0-a20f-4508-b4eb-ac61691a99f5.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687360206895/727973e3-98a6-43ee-9b38-6ab9ce47b444.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687360378895/5e1d4cfd-9c5e-4d24-8df0-5dcbab7fdb95.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687360217044/007f1a23-013d-4520-a127-2a16460c62be.png align="center")

1. Create the deployment group.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687360436359/9df05f6b-57b8-427a-9e5f-e8b4422f890b.png align="center")

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:
    

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687361026413/c7161fba-e9d9-48b1-aab3-3867a42d9c52.png align="center")

1. Execute the created script on your EC2 instance.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687361277131/db715ada-34b2-4228-aba8-ab13265a747a.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687361340833/fd2a9b64-3e80-4be2-8ae9-d3ad1c4fe333.png align="center")

**<mark>Task-02: Add appspec.yml File to CodeCommit Repository and Complete the Deployment Process</mark>**

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:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687361938027/ca8597bc-877f-42eb-8ee2-f7410ae4fea5.png align="center")

<mark>appspec.yml</mark>

```bash
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`:
    

```bash
#!/bin/bash

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

1. `scripts/start_nginx.sh`:
    

```bash
#!/bin/bash

sudo service nginx start
```

To ensure a smooth deployment process, <mark>let's update our </mark> `buildspec.yml` <mark>file as well:</mark>

```bash
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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687362179617/ca3ef806-562d-41ae-a4cd-d09f4a700459.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687362268828/d2400c53-9333-42e9-b671-86434d1febba.png align="center")

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".

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687362512339/911eea05-13da-462e-9707-f45e37088827.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687362587483/266a5208-65ea-4c4e-b2ac-6fba98d5f143.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687362691533/27bd688e-e5cd-4f5b-90b8-3b8d3577d67b.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687362988554/2e3988a0-01c0-443f-ad03-5e594a302a99.png align="center")

Then, attach the created role to your EC2 instance.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687403993409/28ead9fb-c269-4ab3-9a4c-288429367829.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687363456339/f3b41709-b7a5-4801-88ed-016302f9a0aa.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687370254047/47616e5d-c7f2-4ccb-9f7d-de8f6de74e2e.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687370366833/e94756de-9793-437b-9976-d11c13b547d8.png align="center")

Congratulations on completing Part 3 of our CI/CD pipeline on AWS series! See you in [part 4.](https://hashnode.com/post/clj77pgs7000c09l32usdcltd)
