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.
Task-01 :
Read about Appspec.yaml file for CodeDeploy.
The application specification file (AppSpec file) is a YAML- or JSON-formatted file used by AWS CodeDeploy to manage a deployment. It specifies the deployment behavior for an application, including the resources to be deployed, the deployment steps, and the health checks to perform after deployment.
The AppSpec file consists of the following sections:
version
: The version of the AppSpec file format.Resources
: A list of the resources to be deployed, such as Amazon EC2 instances, Amazon ECS clusters, Amazon S3 buckets, and Lambda functions.Triggers
: A list of events that trigger a deployment, such as a change to the source code or a manual intervention.Deploy
: A list of deployment steps, such as installing the application, starting the application, and performing health checks.Rollback
: A list of steps to perform if a deployment fails.Hooks
: A list of scripts to execute before, during, or after a deployment.
Deploy index.html file on EC2 machine using nginx
For code commit and code build steps, please follow my Day 51 task article.
Create a CodeDeploy application:
You need to create a CodeDeploy application to deploy your index.html file. You can do this in the AWS Management Console.
In CodeDeploy, go to Applications and click on 'Create application'.
Select compute platform 'EC2/on premises' and click on 'Create application'.
The application has been created successfully.
Next we need to create a deployment group. Click on Create a Deployment Group.
Give it a desired naming. And for the service simultaneously on the other tab create a new role in IAM.
The new role should be based on AWS CodeDeploy and with the following permissions. You may not be able to add permissions right while creating the role. Create the role and add it later on.
After creating the role paste the ARN of the role inside this box.
Now, simultaneously on the other tab create a default EC2 instance with Ubuntu as a machine.
Paste the details of our EC2 instance inside the environment configuration of our deployment group.
Hold on for the Code-Deploy agent and do not enable the Load Balancer. Scroll down and create the Deployment group.
The deployment group has been created successfully.
You have to setup a CodeDeploy agent in order to deploy code on EC2.
Install the CodeDeploy agent:
You need to install the CodeDeploy agent on your Ubuntu EC2 instance. The CodeDeploy agent is a software package that runs on your instance and interacts with CodeDeploy to deploy your application. You can install the CodeDeploy agent by running the following script on your EC2 instance:
Step 1: Create a file.
vim install.sh
Step 2: Paste the below code-->(ESC:wq)-->Click Enter.
#!/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
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
Step 3: Running a bash
install.sh
file typically means you're executing a shell script named "install. sh" using the Bash shell.
bash install.sh
We can see that our agent is running successfully
You can edit the html file If you want.
Now lets update our CodeCommit repository.
Task-02 :
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.
Create appspec.yml
file
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 install_
nginx.sh
file
#!/bin/bash
sudo apt-get update
sudo apt-get install -y nginx
Create start_
nginx.sh
file
#!/bin/bash
sudo service nginx start
Now, we need to build it through CodeBuild so that the appspec file reaches the S3 bucket which later on can be picked by CodeDeploy. Also before this make sure to update the Artifact and remember to use the same artifact location later on.
Now let's go to CodeDeploy and create deployment out of the demo-app which we created. Follow the path as shown in the below image.
Now as mentioned earlier the revision location should be picked from the CodeBuild project which we will built earlier.
Copy the S3 url and paste it inside the deployment configuration.
Finally click on Create Deployment.
The deployment has been successfully created.
Now when you go to the Deployment Lifecycle events and see the status of the events it will be shown as Pending.
This is because EC2 doesn't have any role policy to retrieve the data from S3 to CodeDeploy. So create a new service role for enabling communication between EC2 and S3, code deploy.
Attach that service role to EC2 instance.
Select EC2 instance, In actions, go to security and click on 'Modify IAM role'.
Select the Service role that we created in above steps.
After updating IAM role, restart code-deploy agent.
Deployment status is Succeeded.
All events Succeeded.
Browse instance public IP address, it will show output of index.html file.
A bonus tip for troubleshooting: see the logs of the agent via commandsudo cat /var/log/aws/codedeploy-agent/codedeploy-agent.log
Also do not neglect the S3 location while creating the deployment in the CodeDeploy
Thank you for reading until here.