Day 14: Unleashing the Deployment Dynamo — A Deep Dive into AWS CodeDeploy!🚀✨
Introduction:
Welcome to Day 14 of our exhilarating AWS 30 Days Challenge! Today, we’re embarking on a thrilling journey into the heart of AWS CodeDeploy, the deployment dynamo that transforms the way we launch applications into the cloud. 🌐💻
What is CodeDeploy?
Code Deploy is a service from Amazon Web Services (AWS) that automates the deployment of code to servers. It makes it easy for developers to update their applications without having to manually manage the process.
Code Deploy works by creating a deployment group, which is a collection of servers that you want to deploy your code. You can then create a deployment configuration, which specifies how you want to deploy your code. For example, you can choose to deploy your code to all servers in the deployment group at once, or you can deploy it to a subset of servers and then gradually roll it out to the rest of the group.
Code Deploy also provides several features that make it easier to manage your deployments. For example, you can use Code Deploy to track the progress of your deployments, and you can roll back to a previous version of your code if there is a problem.
Overall, Code Deploy is a powerful tool that can help developers deploy their code more easily and efficiently.
In very simple Word code deploy is a service to deploy your code to the Servers.
What is appspec.yaml file?
The Application Specification file (AppSpec file) is a YAML or JSON formatted file used by the code deploy service to manage deployment.
Later in this blog, we will discuss how to create the AppSpec.yaml file.
Before Jumping to the step-by-step guide let’s first discuss the Code Deploy Components:
- Application: An application is a name that uniquely identifies the application you want to deploy. CodeDeploy uses this name, which functions as a container, to ensure the correct combination of revision, deployment configuration, and deployment group are referenced during a deployment.
Compute Platform: A computing platform is a platform on which CodeDeploy deploys an application. There are three computing platforms:
EC2/On-Premises: Describes instances of physical servers that can be Amazon EC2 cloud instances, on-premises servers, or both. Applications created using the EC2/On-Premises compute platform can be composed of executable files, configuration files, images, and more.
- AWS Lambda: Used to deploy applications that consist of an updated version of a Lambda function. AWS Lambda manages the Lambda function in a serverless computing environment made up of a high-availability computing structure. All administration of the compute resources is performed by AWS Lambda
Amazon ECS: Used to deploy an Amazon ECS containerized application as a task set. CodeDeploy performs a blue/green deployment by installing an updated version of the application as a new replacement task set. CodeDeploy reroutes production traffic from the original application task set to the replacement task set. The original task set is terminated after a successful deployment.
Deployment Configuration: A deployment configuration is a set of deployment rules and deployment success and failure conditions used by CodeDeploy during a deployment. If your deployment uses the EC2/On-Premises compute platform, you can specify the minimum number of healthy instances for the deployment. If your deployment uses the AWS Lambda or the Amazon ECS compute platform, you can specify how traffic is routed to your updated Lambda function or ECS task set.
4. Deployment type: A deployment type is a method used to make the latest application revision available on instances in a deployment group. There are two deployment types:
- In-place deployment: The application on each instance in the deployment group is stopped, the latest application revision is installed, and the new version of the application is started and validated. You can use a load balancer so that each instance is deregistered during its deployment and then restored to service after the deployment is complete. Only deployments that use the EC2/On-Premises compute platform can use in-place deployments.
Blue/green deployment: The behaviour of your deployment depends on which compute platform you use
Revision: A revision is a version of your application.
6. Service Role :
A service role is an IAM role that grants permissions to an AWS service so it can access AWS resources. The policies you attach to the service role determine which AWS resources the service can access and the actions it can perform with those resources. For CodeDeploy, a service role is used for the following:
- To read either the tags applied to the instances or the Amazon EC2 Auto Scaling group names associated with the instances. This enables CodeDeploy to identify instances to which it can deploy applications.
- To perform operations on instances, Amazon EC2 Auto Scaling groups, and Elastic Load Balancing load balancers.
- Publish information to Amazon SNS topics so that notifications can be sent when specified deployment or instance events occur.
- To retrieve information about CloudWatch alarms to set up alarm monitoring for deployments.
Step by Step guide for Deploying your code using AWS Service Code Deploy
- Deploy index.html file on EC2 machine using nginx..??
As we have seen in previous days blog day 12 and day 13.
We launched and configured some tasks of CodeCommit, CodeBuild, Builspec file and index.html so please refer to day 12 and day 13 for better understanding. Here is the link for Day 12 and Day 13.
Day:12 🚀 Day 12: AWS CodeCommit — Version Control in the Cloud
Day:13 Unleashing CodeBuild Magic: A Deep Dive into AWS Development | 30 Days of AWS — Day 13" 🚀💻
- Create an Instance or Restart previously created in AWS.
- We Build the code in the AWS CodeBuild section in the management section and the code is committed, Built and stored in the S3 bucket.
- For today's deployment, we have to edit artifact configuration of the project as follows.
- Go to the Code Build and select your build project and click on the Edit button and select the Artifacts.
- Select Artifacts packaging as zip and click on the update artifacts.
- Now let’s Navigate to the AWS code deploy
- You will see this type of interface click on the Create Application.
- First, write the Application Name then select the EC2/On-premises and click on the Create application.
- The application is created now. Now create a deployment group.
- Before that, we need to give access to deployment to access the AWS resource for which we need to create a role in IAM. Provide all the necessary access as mentioned below.
- Provide or attach these all policies
- Now click on the create role
- Go to the role and select that role you recently created
- Now click on the trust policy
- Now add this one.
- Now, Create a deployment group. Provide the deployment group name, and service role and enter the ARN id from the above role.
- Deployment type select as In place
- In the environment configuration provide details regarding your ec2 instance
- In the Agent configuration select never and in the Deployment setting code deploy all at once
- Now click on the create deployment group.
- Our deployment group was successfully created.
- We need to install the CodeDeploy agent on the server for which we need to write a script file with all the dependencies.
#!/bin/bash
# This script installs the CodeDeploy agent and its prerequisites on Ubuntu 22.04.
# Update package list
sudo apt-get update
# Install Ruby and its dependencies
sudo apt-get install ruby-full ruby-webrick wget -y
# Change to temporary directory
cd /tmp
# Download the CodeDeploy agent package
wget aws-codedeploy-us-east-1.s3.us-east-1.amazo..
# Create a directory to extract the package contents
mkdir codedeploy-agent_1.3.2-1902_ubuntu22
# Extract the package contents
dpkg-deb -R codedeploy-agent_1.3.2-1902_all.deb codedeploy-agent_1.3.2-1902_ubuntu22
# Update the package dependencies to use Ruby 3.0
sed 's/Depends:.*/Depends:ruby3.0/' -i ./codedeploy-agent_1.3.2-1902_ubuntu22/DEBIAN/control
# Repackage the updated package
dpkg-deb -b codedeploy-agent_1.3.2-1902_ubuntu22/
# Install the updated package
sudo dpkg -i codedeploy-agent_1.3.2-1902_ubuntu22.deb
# List all systemd units that contain "codedeploy" in their name
systemctl list-units --type\=service | grep codedeploy
# Check the status of the CodeDeploy agent service
sudo service codedeploy-agent status
- Create install.sh, file and execute the shell script.
- Our code deploy agent is running
- Now we will add the appspec.yaml file to the repository and completed the deployment.
- The app Spec file is required to create a bridge between the AWS CodeDeploy and the EC2 instance. Create the yaml file.
- appspec. yaml
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 the dependency files as well for installing and starting nginx on the server.
- install_nginx.sh
#!/bin/bash
sudo apt-get update && sudo apt-get install -y nginx
- start_nginx.sh
#!/bin/bash
sudo systemctl start nginx
sudo systemctl enable nginx
- Make sure to change the buildspec.yaml file so that the CodeBuild will build the appspec.yml file and transfer the artifact to S3 bucket.
version: 0.2
phases:
install:
commands:
- echo Installing NGINX - echo apt-get install 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:
- "**/*"
Here is all required files index.html, buildspec.yaml, appspec.yaml, install_nginx.shand start_nginx.sh.
- Now build the project.
- Go to the AWS Code build and select Start build
- Our build part all are successes
- Now create a deployment in the deployment group that we made previously.
- But before that, we have to Create a role for giving access to EC2 instances with all the necessary permission policies
- Now, navigate to the instance and modify the IAM role. Select the IAM role created above.
- Now create a deployment in the deployment group that we made previously.
- Click on the Create deployment
- Revision type select as S3 and provide s3 uri and revision file type as .zip
- Now click on the Create deployment
- All the steps were successfully succeeded.
- Congrats on your deployment.
- Now check you instance id and access the web page using Public IP.
- Finally we completed the service of AWS (code Deploy)
If you did not understand anything then write in the comment section.
My GitHub Profile:
[PurushotamSharma - Overview
Purusing MCA from Charusat University. PurushotamSharma has 39 repositories available. Follow their code on GitHub.github.com](https://github.com/PurushotamSharma "github.com/PurushotamSharma")
Github Repo for this Series:
[GitHub - PurushotamSharma/30-Days-of-AWS-Adventure-
Contribute to PurushotamSharma/30-Days-of-AWS-Adventure- development by creating an account on GitHub.github.com](https://github.com/PurushotamSharma/30-Days-of-AWS-Adventure- "github.com/PurushotamSharma/30-Days-of-AWS-..")
Happy Learning:::
Stay tuned for the 15::