CI/CD pipeline on AWS (Part 3) - CodeDeploy
In the previous blogs, we dealt with AWS CodeCommit & CodeBuild. In this blog, we will explore AWS CodeDeploy and its usage to deploy an index.html file on an EC2 machine using Nginx. We'll cover the setup of CodeDeploy, creation of a deployment group, installation of the CodeDeploy agent on an EC2 instance, and the deployment process. We'll also create an appspec.yaml file to define the deployment actions and scripts to install and start Nginx. Along the way, we'll use other AWS services like CodeCommit, CodeBuild, and IAM roles to enable seamless integration and smooth deployment. Let's dive into the details!
Table of Contents:
Creating a CodeDeploy Application
Setting up a Deployment Group
Installing and Configuring the CodeDeploy Agent
Creating the index.html File
Adding the appspec.yaml File to CodeCommit Repository
Pushing Files to CodeCommit
Configuring the Build Process in CodeBuild
Initiating the Deployment
Permissions and Role Configuration
Restarting the CodeDeploy Agent
Conclusion
AWS CodeDeploy
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.
CodeDeploy makes it easier for you to:
Rapidly release new features.
Update AWS Lambda function versions.
Avoid downtime during application deployment.
Handle the complexity of updating your applications, without many of the risks associated with error-prone manual deployments.
The service scales with your infrastructure so you can easily deploy to one instance or thousands.
TASK
Task 4: 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.
Enter name and select compute platform and click on 'Create application'.
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 'code-deploy-service-role' with the below permissions.
Let us change the trust relationship:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": [
"codedeploy.amazonaws.com"
]
},
"Action": "sts:AssumeRole"
}
]
}
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
Once you have created a CodeDeploy application, you need to create a deployment group. A deployment group is a set of EC2 instances where you want to deploy your application.
Enter a name and select the service role that you previously created, which has all the necessary permissions.
Deployment type: In-place
Environment configuration: Choose "Amazon EC2 instances" and select the key-value pair to specify the EC2 instance associated with this activity.
Select "Never" for installing the CodeDeploy agent and uncheck the "Load Balancer" option. Then, click on "Create Deployment Group".
The deployment group has been successfully created.
Now let us set up a CodeDeploy agent to deploy code on EC2.
The AWS CodeDeploy agent is a software package that is installed on instances in an Amazon EC2 Auto Scaling group or an Amazon EC2 instance. It enables the deployment of applications to these instances by interacting with the AWS CodeDeploy service.
You can install the CodeDeploy agent by running the following script on your EC2 instance:
#!/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
Run the script using bash command.
We can see that the CodeDeploy agent is installed and running successfully.
Create an index.html file:
You need to create an index.html file that you want to deploy. You can create a simple HTML file using any text editor. I am using my previous day's tasks index.html file.
Task 5: Add appspec.yaml file to CodeCommit Repository and complete the deployment process.
Create an appspec.yaml file:
You need to create an appspec.yaml file that tells CodeDeploy what to do with your application.
Here is an appspec.yaml file that deploys the index.html file on nginx. also, create 2 scripts for installing nginx and starting nginx.
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
Create start_nginx.sh in the scripts folder
#!/bin/bash
sudo service nginx start
Create install_nginx.sh in the scripts folder
#!/bin/bash
sudo apt-get update
sudo apt-get install nginx -y
Let’s push all the files to code commit using 'git add' and 'git commit' commands.
We can see all the files in the CodeCommit repository.
Make sure to change the buildspec.yml file so that the CodeBuild will build the appspec.yml file and transfer the artifact to the S3 bucket.
version: 0.2
phases:
install:
commands:
- echo Installing NGINX
- sudo apt-get update
- sudo apt-get install nginx -y
build:
commands:
- echo Build started on 'date'
- cp index.html /var/www/html/
post_build:
commands:
- echo Configuring NGINX
artifacts:
files:
- '**/*'
In build projects, Edit and choose 'Artifacts'.
Enter path and packaging type zip and update.
The artifact upload location has been successfully added. Click on 'Start build' to initiate the build process.
After the build is completed, navigate to the S3 bucket and copy the S3 URI.
Create Deployment
In the Application, navigate to Deployments and click on 'Create deployment'.
For the revision type, select Amazon S3 and paste the previously copied S3 URL into the revision location field.
Click on 'Create deployment' to initiate the deployment process.
The deployment is created, but the events are currently in a pending state.
EC2 doesn't have any role policy to retrieve the data from S3 to CodeDeploy.
To create a new service role for enabling communication between EC2 and S3, code deploy.
Go to IAM service and create 'EC2-S3-CodeDeploy' with permissions.
"AmazoneEC2FullAccess", "AmazoneS3FullAccess", "AWSCodeDeployFullAccess".
Next, attach the newly created service role to the EC2 instance. Select the EC2 instance, go to the Actions menu, navigate to Security, and click on 'Modify IAM role'.
Choose the service role that was created in the previous steps.
After updating the IAM role, restart the CodeDeploy agent to ensure the changes take effect.
sudo service codedeploy-agent restart
sudo service codedeploy-agent status
Finally, the Code deployment is successful.
Browse the public IP address of the instance to view the output of the index.html file.
In this blog, we covered the steps required to deploy an index.html file on an EC2 machine using Nginx and AWS CodeDeploy. We started by setting up a CodeDeploy application and creating a deployment group. Then, we installed and configured the CodeDeploy agent on an EC2 instance. We created the index.html file and prepared the appspec.yaml file to define the deployment actions. We also used Git to push the files to CodeCommit and configured CodeBuild to build the appspec.yaml file. Finally, we initiated the deployment process, configured the necessary IAM role, and restarted the CodeDeploy agent. The result was a successful deployment of the index.html file, which could be accessed by browsing the public IP address of the EC2 instance. By following these steps, you can effectively deploy your applications using AWS CodeDeploy.
Thank you for reading this blog. If you found this blog helpful, please like, share, and follow me for more blog posts like this in the future.
— Happy Learning !!!