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:
- Navigate to CodeDeploy.
- Click on "Create Application".
-2-. Then, provide an "application name" and select the compute platform as "EC2." Finally, click on "Create."
-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.
-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.
-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.
-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.
-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
-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
-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.
-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.
-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.
-13-. Now, we need to grant these permissions to our instance. Go to the EC2 dashboard and select your instance.
-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
-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.
so this is how code-pipeline works
Top comments (0)