Hello everyone!
In the last article, we have seen code build. Today in the next part of CI/CD pipeline we are going to explore AWS code deploy.
Next few days you’ll learn these tools/services:
- CodePipeline
- S3
What is AWS 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 -
Automate deployments to remove manual operations Repeat an application deployment across different groups or instances using a file and command-based install model.
Deploy to many hosts Manage deployments to thousands of hosts with advanced monitoring and traffic shifting.
Use advanced deployment techniques Support multiple deployment types, including in-place, canary, and blue/green deployments.
Monitor health and rollback Configure alarms that will initiate rollbacks, and stop application deployments in progress.
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.
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 > Developer Tools > CodeDeploy >Applications >My-App
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.
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.
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
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.