AWS CodeDeploy
Tags: aws, awsdevops, devops, cloud
Published: 2024-01-31 08:55:34

Hi, guys! I hope you are practicing AWS DevOps and enhancing your skills. Last week, we covered the AWS CodeBuild service, and today we are moving on to the next part, which is CodeDeploy. After building your code, we need to deploy it, right? With the help of the CodeDeploy service, we can automate our deployment process as well. 

AWS CodeDeploy is a service that automates code deployments to any instance, including Amazon EC2 instances and instances running on-premises.

-1-. Let's deploy our code with the AWS CodeDeploy service. To deploy an application using CodeDeploy, follow these steps:

1. Navigate to CodeDeploy.
2. Click on "Create Application".

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nxmrsyx9uickvm0xnti1.png)

-2-. Then, provide an "application name" and select the compute platform as "EC2." Finally, click on "Create."

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uoprylf161p0h5nhw333.png)

-3-. So, since we are using EC2 for our study, once your application is built, it will run on that system or be deployed on the server. This concept is referred to as a "deployment group." Click on "Create Deployment Group." A deployment group typically consists of more than one server. If you want to deploy the application on multiple servers simultaneously, you'll need to create a deployment group for that purpose.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3389g9rd1iem6umv8815.png)

-4-. Then, provide a deployment group name, create a service role, and select the deployment type. In the service role, attach the necessary access policies. If needed, you can attach those policies to your role to ensure the deployment is done successfully.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pwco4y7upy524syxj96v.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5eqb08kzzdsvd8bx8n9u.png)

-5-. Then, create a basic EC2 instance and go back to our CodeDeploy group. We need the EC2 instance because our application is deployed on that server. In the Environment Configuration, select your EC2 instance.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u432ksa9to24j6w800cy.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fvl9u84b1zkx4v6juoxj.png)

-6-. Select the "never" value for installing the AWS CodeDeploy agent, disable the load balancer, and click on "Create Deployment Group." For the time being, I've chosen the "never" option, but we may need to change this setting later.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zy1fp3xu2j0h4ac0sphh.png)

-7-. The reason I selected the "never" option for installing the agent is because your application runs on an EC2 instance, and the EC2 instance needs specific requirements for the application to function. Installing the "agent" ensures compatibility between the EC2 instance and CodeDeploy. To address any potential version mismatches between the EC2 instance and CodeDeploy, I will provide you with a script. You'll need to connect to the instance and run the script to resolve the version mismatch.

```
#!/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 
# wget cmd end here
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
# dpkg end here
sed 's/Depends:.*/Depends:ruby3.0/' -i ./codedeploy-agent_1.3.2-1902_ubuntu22/DEBIAN/control
# sed end here
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
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eygo7ta71xc856f0d38x.png)

-8-. Now, if you recall, just as we used the `buildspec.yaml` for building the code, we need an `appspec.yaml` file for deploying the code.

```
version: 0.0
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
```

-9-. After that, create a "scripts" folder, and within it, create two new files named "install_nginx.sh" and "start_nginx.sh." Push these files to our AWS code repository. The file contents are as follows:

```
---------------------install_nginx.sh---------------------
#!/bin/bash

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

----------------------start_nginx.sh-------------------
#!/bin/bash

sudo service nginx start
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/02wmvkbsuute5wfxckga.png)

-10-. Afterward, we need to build this code so that our latest code is stored in "S3." Navigate to "Build" and initiate the build process.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wqcd76d841qkw3srcm1e.png)

-11-. Now, go to "Deployment" again and click on "Create Deployment." Choose the "Revision type," select the file type, and then click on "Create Deployment." You will see the deployment process starting. Also, choose the S3 location where our "Zip" file is stored; this is crucial. Click on that zip file, and it will show the details of the zip. Copy the zip URL and paste it in the revision location.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xzb6zffqttoj6uywlxfr.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gk1ldtfogjkoi6dtuz7r.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yrppkwvn3llyyext36gl.png)

-12-. But if you click on "Events," you will notice that all are pending because the EC2 instance doesn't have the permission to retrieve artifacts from S3. Additionally, the EC2 instance lacks permission to access the CodeDeploy service. Let's create one more role in IAM to address this issue.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lbzxq0dmeqyu59mmuwme.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/au90trinjnmd4blbi4cm.png)

-13-. Now, we need to grant these permissions to our instance. Go to the EC2 dashboard and select your instance.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kwek890u4y9uztbzkl1f.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kd7ullretq7gaiudneta.png)

-14-. Now, connect to the instance and restart the CodeDeploy agent service. After that, check the CodeDeploy dashboard, and you should see that all steps have run successfully.

```
sudo service codedeploy-agent restart
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9z6vqujg42bex4b183gi.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ardhr8j1yjq2ub5mincz.png)

-15-. Now, go to EC2, copy your public IP, and paste it into the browser. You should see the Nginx page, indicating that your deployment was successful.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d5i73e64un7qtubzldcv.png)

so this is how code-pipeline works