Code Deploy PipeLine Project
Code Deploy PipeLine Project
Code Deploy PipeLine Project
Application: A CodeDeploy application can be defined from AWS CodeDeploy web console.
Appspec file: This contains the instruction to CodeDeploy, like copying of files, executing the
scripts etc during the code deployment process. It is present in the root directory of
unzipped code with name appspec.yml.
Deployment Group: Represent set of machines of Lambda function where code has to be
deployed.
Setup in Brief:
I have used two EC2 instance of AMZ2 Linux. First one is the web server we will be
configuring, also called CodeDeploy agent. Second EC2 machine is supposed to use by
developer where the codes are programmed. The names of the resources in the experiment
are arbitrary and may name the resources your own.
Steps in Detail
a - Create IAM Role for EC2 instance to access S3. Select EC2 as AWS service and
assign AmazonS3FullAccess permission. Use any arbitrary name for the Role. I have used a
name s3-ec2-full. This Role must be attached the EC2 instance (webserver) later.
b- Create another IAM Role for CodeDeploy access. Select CodeDeploy as AWS Service and
assign AWSCodeDeployRole permission like below. I have assigned a name cdrole. This role
must be used while the CodeDeploy deployment is configured in a later stage.
a- Use the existing desktop/laptop or Launch a new EC2 instance. This is used by the
Developer for the code creation and manual pushing of code to S3 bucket.
c - execute aws configure command on developer’s machine and install the access/secret
keys.
a- Launch the EC2 instance. This is used for deploying webserver with CodeDeploy.
b-Create a Tag for the instance. The deployment group member ship for the EC2 instance is
decided by this Tag. I have used AppName Tag with value SampleApp.
yum update
yum install ruby -y
yum install wget -y
wget https://aws-codedeploy-us-east-1.s3.amazonaws.com/latest/install
chmod +x install
./install auto
service codedeploy-agent status
Note: you may copy the contents from this document to create code. scripts should be given
execute permissions.
b- Make sure that zip file of the of the code and its extracted directory is kept inside the
directory /root/deploy_dir. my application name is sampleapp.
c-Let us visit the code now. The output shows a sampleapp directory which is extracted
from the code sampleapp.zip
[root@ip-172-30-0-178 deploy_dir]# ls
sampleapp sampleapp.zip
[root@ip-172-30-0-178 deploy_dir]# ls -R
.:
sampleapp sampleapp.zip
./sampleapp:
appspec.yml index.html scripts
./sampleapp/scripts:
httpd_install.sh httpd_start.sh httpd_stop.sh
The code should contain a file appspec.yml. The files: section says what are files to be copied
in which directory of the destination machine. I want to copy index.html to
/var/www/html. BeforeInstall: section says what action must be done before install
application in my case before copying the file I wanted httpd rpm package has to be
installed.
[root@ip-172-30-0-178 deploy_dir]# cat sampleapp/appspec.yml
version: 0.0
os: linux
files:
- source: /index.html
destination: /var/www/html/
hooks:
BeforeInstall:
- location: scripts/httpd_install.sh
timeout: 300
runas: root
- location: scripts/httpd_start.sh
timeout: 300
runas: root
ApplicationStop:
- location: scripts/httpd_stop.sh
timeout: 300
runas: root
f-contents of/root/deploy_dir/sampleapp/index.html
c- Now upload the code to S3 by the executing the command below. Directory of execution
is important.
b- Select sampleapp and click Create Deployment Group from Deployment Groups tab.
c- Enter the values like below and leave the other parameters default
In the sampleapp click Create Deployment. Enter values like below. Other parameter can can
be kept default
Now access the public Ip address fo the webserver from the browser and see that it is
working
AWS CodePipeline
We use AWS CodeDeploy along with AWS Codepipeline for this experiment. Initial version of
the code in uploaded to the S3 bucket. Whenever new version is released, the only
operation required by the developer is to upload the new version of the code to the same
S3 bucket. CodePipeline does the rest. It detects a new upload in the S3 bucket with help of
CloudWatch and trigger the deployment to the target server (webserver in our case) using
CodeDeploy.
Setup in brief:
Step 1,2,3 was already completed with CodeDeploy, hence move on from the Step 4.
# mkdir /root/deploy_dir
# cd /root/deploy_dir
# mkdir sampleapp
c- Under sampleapp create files appspec.yml, index.html and scripts directory. Inside scripts
create three scripts httpd_install.sh, httpd_start.sh and httpd_stop.sh.
d- Let us see the contents of the appspec.yml file.
# cd /root/deploy_dir/sampleapp
# zip -r ../sampleapp.zip .
a - Now you have to develop the code for the second release in a different directory
/root/deploy_dir2. We will create second version just by modifying the contents of
index.html. In the file index.html the text "Version 1" is changed to "Version 2". Rest of the
files and codes are same. For that follow the steps below
# mkdir /root/deploy_dir2
# cd /root/deploy_dir2
# cp -var /root/deploy_dir/sampleapp .
# cat /root/deploy_dir2/sampleapp/index.html
<html>
<h2> Sample App Version 2</h2>
</html>
# cd /root/deploy_dir2/sampleapp
# zip -r ../sampleapp.zip .
[root@ip-172-30-0-178 deploy_dir2]# ls -R
.:
sampleapp sampleapp.zip
./sampleapp:
appspec.yml index.html scripts
./sampleapp/scripts:
httpd_install.sh httpd_start.sh httpd_stop.sh
Click Create Pipeline and enter the values like in the screen shot
In the Deploy stage Enter the value like below and Click Next.
a - Now it the time to test AWS CodePipeline. SSH to the Developer machine to upload the
Version 2 of the code.
# cd /root/deploy_dir2
# aws s3 cp sampleapp.zip s3://gir-sampleapp
b - Wait for a few minutes for CloudWatch to detect the new upload to S3 bucket. Refresh
the webbrowser. Now you should see contents of index.html ("Version 2" Text")