In the Upcoming next 4 days, I will be making a CI/CD pipeline
on AWS with these tools.
CodeCommit
CodeBuild
CodeDeploy
CodePipeline
S3 Bucket
What is CodeDeploy?
CodeDeploy is a deployment service that automates application deployments to
Amazon EC2 instances
,on-premises instances
,serverless Lambda functions
, orAmazon ECS services
.You can deploy a nearly unlimited variety of application content, including:
Code
Serverless AWS Lambda functions
Web and configuration files
Executables
Packages
Scripts
Multimedia files
CodeDeploy can
deploy application
content thatruns on a server
and isstored in Amazon S3 buckets
,GitHub repositories
, orBitbucket 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.
Before moving to task let's create a CodeDeploy
application.
Step-01:
Go toCodeDeploy
service on AWS console.
Step-02:
Click onCreate application
button.
Step-03:
EnterApplication name
andDeployment group name
and selectEC2/On-premises
asEnvironment configuration
and click onCreate application
button.
What is EC2/On-premises?
- An on-premises instance is any physical device that is not an
Amazon EC2 instance
that can run theCodeDeploy agent
andconnect to public AWS service endpoints
.
- Thus we have created a
CodeDeploy
application.
Now Let's create a IAM role
for CodeDeploy
.
Step-01:
Go toIAM
service on the AWS console and create aIAM role
forCodeDeploy
.
Step-02:
SelectCodeDeploy
asService that will use this role
and click onNext:Permissions
button and selectAWSCodeDeployFullAccess
,AmazonS3FullAccess
andAmazonEC2FullAccess
asPermissions
and click onNext:Tags
button.
Step-03:
Now update the trust relationship of the role by clicking onEdit trust relationship
button. Change theService
tocodedeploy.amazonaws.com
and click onUpdate Trust Policy
button.
Now Let's create a CodeDeploy
application.
Step-01:
Go toCodeDeploy
service on the AWS console and go inside the application that we created earlier.
Step-02:
Click onCreate deployment group
button.
Step-03:
EnterDeployment group name
and selectIAM role
that we have created earlier and click onCreate deployment group
button.
Step-04:
SelectIn-place deployment
asDeployment type
and selectAmazon EC2 instances
asEnvironment configuration
and selectTag
asKey
andValue
asName
and click onCreate deployment group
button.
Step-05:
Agent configuration is optional so we will skip it and SelectNever
and click onCreate deployment group
button.
Step-06:
Thus we have created aCodeDeploy
application.
Task-01:
Setup a CodeDeploy agent in order to deploy code on EC2
Updates the package manager to ensure it has the latest information about available packages.
Installs Ruby because the CodeDeploy agent is written in Ruby and requires it to run.
Downloads the installation script provided by AWS CodeDeploy and makes it executable.
Executes the installation script in "auto" mode to install the CodeDeploy agent with default settings.
Starts the CodeDeploy agent service to begin the deployment process.
Optionally, enables the CodeDeploy agent to start automatically on boot.
#!/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
sudo chmod 777 installation_codedeploy_agent.sh
./installation_codedeploy_agent.sh
Read about Appspec.yaml file for CodeDeploy.
BeforeInstall
,AfterInstall
, andApplicationStart
are differenthooks
specifyingscripts
that will run at different stages of the deployment process.Replace scripts/before_install.sh
,scripts/after_install.sh
, andscripts/application_start.sh
with theactual paths to your scripts.
timeout
specifieshow long (in seconds) AWS CodeDeploy will wait
foreach script to finish before considering it a failure
.runas
specifies the usercontext under which the script will run
. In this case, the scripts willrun as the root user (root)
.files section
specifies that all files from the source(/)
should be deployed to the destination(/var/www/html)
.
example of "appspec.yaml" file
version: 0.0
os: linux
files:
- source: /
destination: /var/www/html
hooks:
BeforeInstall:
- location: scripts/before_install.sh
timeout: 300
runas: root
AfterInstall:
- location: scripts/after_install.sh
timeout: 300
runas: root
ApplicationStart:
- location: scripts/application_start.sh
timeout: 300
runas: root
Deploy index.html file on EC2 machine using the nginx web server.
- Let's create
install-nginx.sh
the file.
#!/bin/bash
sudo apt-get update
sudo apt-get install nginx -y
- let's create
start-nginx.sh
the file.
#!/bin/bash
sudo service nginx start
- At last, we push all the files to Git Hub.
git add .
git commit -m "add all files"
git push origin master
- Thus we can verify the file uploaded on CodeBuild.
Now Let's edit artifacts.
Developer tools > CodeBuild > Build projects > project name > edit artifacts
Now
ReBuild
the project.Developer tools > CodeBuild > Build projects > project name > Rebuild
Now create an S3 bucket for code deployment.
Services > S3 > Create bucket > bucket name > region > Create bucket
I'm using my previous bucket which I created for
code build
.Now add the
artifacts
in theS3 bucket
which we have created forcode deploy
andrebuild
again.Thus we can verify the file uploaded on the S3 bucket and copy the
object url
.
Step-01:
: Go toDeveloper tools > CodeDeploy > Applications > Deployment Groups > Select the deployment file
Step-02:
: Now click onCreate deployment
button and paste theobject url
in theRevision location
and click onCreate deployment
button.
Now we can see the
deployment
issuccessful
and you can check whether the file is running or not by going intoartifact.zip>index.html
and click onindex.html
file to have output.