Day 52: Your CI/CD pipeline on AWS - Part 3 🚀 ☁(Jan 17, 2024)
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.
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
Deploy index.html file on EC2 machine using nginx
- Navigate to CodeDeploy and click on Create Application.
- Provide an application name, select the compute platform as EC2 instance, and click on Create application
- Application created
Create Deployment group in application of CodeDeploy
Go to IAM role and create one role with below permission policies
Go to EC2 instance and create an instance
- Now go to codedeploy application and click on create deployment group
- Enter group name and select previously created role
- Select In place and enter name of EC2 instance which we created
- Deployment group created
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 file and enter below script and run it
#!/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
- 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.
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.
- Select the role that we created in above step & click on Update IAM role
- Restart code-deploy agent
Add appspec.yaml file to CodeCommit Repository and complete the deployment process
- Create appspec.yml file
- Push this file to code-commit repository
- Click on Strat build
- Navigate to CodeDeploy and click on the application we created, then proceed to the Deployments section & Click on Create deployment.
- Select the Deployment group name, enter the path, and click on Create deployment.
- Deployment completed successfully
- Copy the public IP address of the instance and paste it into your browser. This will display the output of the index.html file.