# Day 52: Your CI/CD pipeline on AWS - Part 3 🚀 ☁(Jan 17, 2024)

## **🙏 Introduction:**

In this blog, we will deploy a CI/CD pipeline on AWS using tools such as CodeCommit, CodeBuild, CodeDeploy, CodePipeline, and S3. This is Part 3.

## **🔶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. It can also deploy a serverless Lambda function. There is no need to make changes to your existing code before using CodeDeploy.
    

## **🎯Task: 1**

1. ### **Read about Appspec.yaml file for CodeDeploy**
    

The application specification file (AppSpec file) is a YAML -formatted or JSON-formatted file used by CodeDeploy to manage a deployment. The AppSpec file for an EC2/On-Premises deployment must be named appspec. yml or appspec. yaml

1. ### **Deploy index.html file on EC2 machine using nginx**
    

**Create Application**

* Navigate to CodeDeploy and click on **Create Application**.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703679622427/47b6e6ca-4c7b-4195-8bf4-cacefbe83cbb.png?auto=compress,format&format=webp align="left")

* Provide an application name, select the compute platform as EC2 instance, and click on **Create application**
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703679767782/46424e78-195b-454c-8270-f3bc29fecb34.png?auto=compress,format&format=webp align="left")

* Application created
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703679870752/b7e2d672-2a0e-4bc9-bef6-6ec40a0a5236.png?auto=compress,format&format=webp align="left")

**Create Deployment group in application of CodeDeploy**

* Go to IAM role and create one role with below permission policies
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703685147974/ea383592-dbd1-4121-86c1-c4775cb0f620.png?auto=compress,format&format=webp align="left")
    
* Go to EC2 instance and create an instance
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703685584768/fc938520-9cb6-41ae-a225-fd398a10459b.png?auto=compress,format&format=webp align="left")

* Now go to codedeploy application and click on create deployment group
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703685712229/524f8314-3bef-432b-84e9-83e1ae3aadcd.png?auto=compress,format&format=webp align="left")

* Enter group name and select previously created role
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703686305241/6d5b04f8-ccba-4e2e-8ccb-9c68972a8b19.png?auto=compress,format&format=webp align="left")

* Select In place and enter name of EC2 instance which we created
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703694649605/89828083-01f3-4971-960d-c7e954ed56b5.png?auto=compress,format&format=webp align="left")

* Deployment group created
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703696394686/f3a98e2b-04d6-43a5-9898-86bfe88e7e43.png?auto=compress,format&format=webp align="left")

1. ### **Setup a CodeDeploy agent in order to deploy code on EC2**
    

**Manually Set Up a CodeDeploy Agent**

* you have to setup a CodeDeploy agent in order to deploy code on EC2
    
* Connect to EC2 instance and create [install.sh](https://sutish.hashnode.dev/day-52-your-cicd-pipeline-on-aws-part-3#heading-what-is-codedeploy) file and enter below script and run it
    

* ```plaintext
        #!/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-west-2.s3.us-west-2.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/v1703697204713/15c485e7-6f92-47d3-856a-cb0de26c9129.png?auto=compress,format&format=webp align="left")

* EC2 doesn't have a role policy that allows it to retrieve data from S3 for CodeDeploy. Therefore, create a new service role to enable communication between EC2 and S3, as well as CodeDeploy.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703729000823/18182953-e4f6-48a6-8b7f-e5dafb047462.png?auto=compress,format&format=webp align="left")

**Attach that service role to EC2 instance**

* Select the EC2 instance, then in the Actions menu, navigate to Security and click on **Modify IAM role**.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703730023580/9982b509-3fa0-44e0-9fd3-0814d700b550.png?auto=compress,format&format=webp align="left")

* Select the role that we created in above step & click on Update IAM role
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703731074802/7dc0da64-99e9-4d66-b527-cbcbcd2c4e80.png?auto=compress,format&format=webp align="left")

* Restart code-deploy agent
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703731500100/81e3d19e-fa30-4fc3-a596-db032e51792a.png?auto=compress,format&format=webp align="left")

## **🎯Task: 2**

1. ### Add appspec.yaml file to CodeCommit Repository and complete the deployment process
    

* Create appspec.yml file
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703733395792/edbfa8ee-8550-4aff-aa4f-2dd62f0e428f.png?auto=compress,format&format=webp align="left")

* Create a script folder. Inside the script folder, create **install\_**[**nginx.sh**](https://sutish.hashnode.dev/day-52-your-cicd-pipeline-on-aws-part-3#heading-what-is-codedeploy) and **start\_**[**nginx.sh**](https://sutish.hashnode.dev/day-52-your-cicd-pipeline-on-aws-part-3#heading-what-is-codedeploy).
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703733940954/12f607ae-69eb-4553-bf37-4959ce43a282.png?auto=compress,format&format=webp align="left")

* Push this file to code-commit repository
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703734320006/45a274e3-bc4b-4acf-b93b-521780ded558.png?auto=compress,format&format=webp align="left")

* Click on **Strat build**
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703740965089/2aad1a68-4b09-4af3-aba2-9499a18bc5e8.png?auto=compress,format&format=webp align="left")

**Deployment process**

* Navigate to CodeDeploy and click on the application we created, then proceed to the Deployments section & Click on **Create deployment.**
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703742213723/94856878-62b9-4296-a2a2-5a0f127b2a3c.png?auto=compress,format&format=webp align="left")

* Select the Deployment group name, enter the path, and click on **Create deployment**.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703744365235/a9cc28cd-24ad-4af0-bb46-bdc4f4412313.png?auto=compress,format&format=webp align="left")

* Deployment completed successfully
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703747016107/c3583be8-0964-4324-9fea-bfd712ce3e46.png?auto=compress,format&format=webp align="left")

* Copy the public IP address of the instance and paste it into your browser. This will display the output of the index.html file.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703747222790/57588946-20ce-4bf1-b67d-121e39ca0ab9.png?auto=compress,format&format=webp align="left")
