Day 52: Your CI/CD pipeline on AWS -Part 3
Welcome to the third installment of our comprehensive series, “Day 52: Your CI/CD Pipeline on AWS.” In this journey through the intricacies of modern software development and deployment, we delve into a pivotal aspect of the AWS ecosystem: AWS CodeDeploy. In this segment, we unravel the power of AWS CodeDeploy, shedding light on how it streamlines application deployments, automates release processes, and ultimately contributes to the seamless orchestration of code changes.
Next few days you’ll learn these tools/services:
- CodeDeploy
- CodePipeline
- S3
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. CodeDeploy can also deploy a serverless Lambda function. You do not need to make changes to your existing code before you can use CodeDeploy.
Key features and benefits of AWS CodeDeploy include -
- Automated Deployments: CodeDeploy automates the deployment process, eliminating the need for manual steps. This ensures consistency and reduces the chances of errors that can occur during manual deployments.
- Easy Rollbacks: In case a deployment doesn’t go as planned, CodeDeploy allows you to easily roll back to a previous version of your application. This helps mitigate risks associated with faulty releases.
- Deployment Configurations: CodeDeploy lets you define deployment configurations, which are sets of rules and parameters that control how deployments are executed. This allows for flexible and customizable deployment strategies.
- Deployment Monitoring: During and after deployment, CodeDeploy provides detailed logs and metrics, allowing you to monitor the progress and status of your deployment in real-time.
- Deployment Health Monitoring: CodeDeploy monitors the health of instances during and after deployment. If an instance doesn’t meet the specified criteria, CodeDeploy can automatically stop the deployment on that instance, ensuring the health of your application.
- Integration with Other AWS Services: CodeDeploy seamlessly integrates with other AWS services like AWS CodePipeline (for end-to-end CI/CD workflows), Amazon CloudWatch (for monitoring), and AWS Identity and Access Management (IAM) for fine-grained access control.
- Broad Compatibility: CodeDeploy supports a range of application types and deployment scenarios, including rolling updates, blue/green deployments, and in-place deployments.
- Cost-Efficiency: With CodeDeploy, you pay only for what you use, making it a cost-efficient solution for organizations of all sizes.
Tasks
Task 1) Deploy index.html file on EC2 machine using nginx (you have to set up a CodeDeploy agent to deploy code on EC2)
Let’s create a CodeDeploy application:
Navigate to CodeDeploy > Applications > Click on Create Application.
Give name for your application
Click on create
Our application is ready.
We need to establish connections between CodeDeploy and other AWS services. How do we do it? We can connect the CodeDeploy to other AWS services by creating a service role in the IAM.
Navigate to Roles in IAM. And Create a New Role having these permissions:
AmazonEC2FullAccess, AmazonEC2RoleforAWSCodeDeploy, AmazonS3FullAccess, AWSCodeDeployRole, AWSCodeDeployFullAccess, AmazonEC2RoleforAWSCodeDeployLimited.
We will need to have an EC2 instance to deploy the index.html file.
Let us create a deployment group:
In the CodeDeploy console > Go to the Deployment Groups Tab > Click on Create deployment group:
Deployment group name: codedeploy-group
Service role: Select the service role which you created previously with all the permissions.
Deployment type: In-place
Environment configuration: Select Amazon EC2 instances. Select the key and value to select the EC2 instance you created for this activity.
Install AWS CodeDeploy Agent: Never
Disable load balancing.
And click on create deployment group.
Your deployment group will be created.
Now let us set up a CodeDeploy agent to deploy code on EC2.
Install the CodeDeploy agent on your EC2 instance using the installation script.
#!/bin/bash
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
bash install_codedeploy_agent.sh
We can see that the CodeDeploy agent is installed and running successfully.
Let us create an index.html file. I am using my previous day’s tasks index.html file.
Task 2: Add appspec.yaml file to CodeCommit Repository and complete the deployment process.
Let us create an appspec.yml file to deploy index.html on nginx. Also we will create two scripts for installing and starting nginx.
The contents of the appspec.yml file would look like:
version: 0.0
os: linux
files:
- source: /
destination: /var/wwiw/html
hooks:
AfterInstall:
- location: scripts/install_nginx.sh
timeout: 300
runas: root
ApplicationStart:
- location: scripts/start_nginx.sh
timeout: 300
runas: root
push all the files to our CodeCommit repo.
Now let us build the project using CodeBuild. While building select the S3 for Artifacts and also enable artifact packaging (.zip). Create a S3 before that.
We can see that our build is succeeded.
Now go to the S3 and copy the location where the .zip file is located.
Before that, I will have to create a Service role named new-service-role-for-ec2-s3-codedeploy for the EC2, S3, and CodeDeploy to communicate with each other, with the following permissions: AmazonEC2FullAccess, AmazonS3FullAccess, AWSCodeDeployFullAccess.
We will have to attach this role to our EC2 instance.
Now go to Deployment Groups > For the group which we created before > Revision type, S3, and paste the above S3 URL:
And click on Create the Deployment.
Once the deployment is successful, you should be able to reach it the output file of index.html.