Learn how to setup AWS CodeDeploy using Github for Continuous Deployment & Continuous Integration...
Unlike Microsoft Azure cloud service which offers a very easy and staright forward way to setup the Continuous Deployment and Continuous Delivery with any version control like Github, Bitbucket, or a local repository, I found out AWS CodeDeploy requires a quite complex procedure to achieve the same and hence is limited to only S3 bucket and Github for continuous deployment of code to any EC2 instance. So kindly follow this step-by-step tutorial on to set this up with minimal steps. The AWS doc too seems complex and i searched a lot when finally found some video demonstration for the same which really helped me out.
Introduction
To AWS CodeDeploy
AWS CodeDeploy is one of the
developer tools by Amazon Web Services which allows for Continuous Integration
and Continuous Deployment. AWS CodeDeploy coordinates the application
deployments to Amazon EC2 instances, on-premises or both.
AWS CodeDeploy currently deploys
application revisions from Github and Amazon S3 buckets.
Pre-requisites
for the Tutorial
i. GitHub Account with Code Repository setup containing the appspec.yml
file and scripts folder in the root directory of the application.
Sample Code Structure and appspec.yml file can be found on the
following-
https://github.com/shoobomb/AWS-CodeDeploy-Example
ii. AWS account setup.
Tutorial
Here goes the tutorial to configure
and setup Continuous Deployment with Github Using CodeDeploy –
1. Sign in to your AWS
account by going to the following link:
2. Next, we create two IAM
roles by clicking the services tab and going to the IAM (Identity & Access
Management)
i. First we are going to create IAM role for CodeDeploy to talk
to our EC2 instance where our code is deployed. For this we click on create new
role by clicking on “Roles” tab, with some name “CodeDeploy”.
ii. Next, select EC2 and click on Next.
iii. Then, we are going to add a custom policy to this Role. Add
the following policy document to this role and click on create Role.
// Policy Role for Code Deploy
{
"Version":
"2012-10-17",
"Statement": [
{
"Action": [
"autoscaling:PutLifecycleHook",
"autoscaling:DeleteLifecycleHook",
"autoscaling:RecordLifecycleActionHeartbeat",
"autoscaling:CompleteLifecycleAction",
"autoscaling:DescribeAutoscalingGroups",
"autoscaling:PutInstanceInStandby",
"autoscaling:PutInstanceInService",
"ec2:Describe*"
],
"Effect":
"Allow",
"Resource":
"*"
}
]
}
iv. Next, we are also going to edit the trust relationship for
this IAM role by adding the following relationship policy.
// Policy Trust for Code Deploy
{
"Version":
"2012-10-17",
"Statement": [
{
"Sid":
"",
"Effect":
"Allow",
"Principal": {
"Service": [
"codedeploy.us-west-2.amazonaws.com",
"codedeploy.us-east-1.amazonaws.com"
]
},
"Action":
"sts:AssumeRole"
}
]
}
v. Now we are going to create another IAM role for our EC2
instance. For this we click on create new role by clicking on “Roles” tab, with
some name “EC2CodeDeploy”.
vi.
Then, select EC2 and click on Next.
vii. Then, we are going to add a custom policy to this Role. Add
the following policy document to this role.
// Instance Role for EC2 Instance
{
"Version":
"2012-10-17",
"Statement": [
{
"Action":
[
"s3:Get*",
"s3:List*"
],
"Effect":
"Allow",
"Resource":
"*"
}
]
}
viii.
Finally click on ‘Create Role’.
3. Now we are going to create
and launch a new EC2 instance by going under the services and click on EC2.
Then click Launch a New Instance.
4. For this tutorial, we’ll
select “Amazon Linux AMI” because it comes pre-installed with AWS CLI.
5. Next, we are going select
the instance type according to our requirements.
6. Then click on the button
“Configure Instance Details”.
7. Now under IAM we are going
to select the EC2 role we create i.e. EC2CodeDeploy.
8. Next the storage is fine
as default and then we can tag the instance by adding some value to the Key
name like “AWS CodeDeploy”.
9. Next configure the
Security Group to allow all inbound-outbound traffic, leave it as default and
proceed further. Finally click on Review and Launch button.
10. Now select the key pair for establishing the SSH or create a
new one if not already created and wait for the instance to launch.
11. Once the instance has been launched successfully, we are
going to connect to via SSH. For this go to your instance under EC2 and click
on connect.
12. After you are successfully connected to your EC2 instance, we
need to configure something and install Code Deploy agent in our EC2 instance.
13. Run the following commands on to your EC2 instance by
following the instruction sheet provided.
Instruction Sheet
i.
When server is booted run the following commands as root.
yum -y update
yum install -y aws-cli
cd /home/ec2-user
ii.
Here you will setup your AWS access, secret, and region.
aws configure
aws s3 cp s3://aws-codedeploy-us-east-1/latest/install . --region
us-east-1
chmod +x ./install
iii.
This is simply a quick hack to get the agent running faster.
sed -i "s/sleep(.*)/sleep(10)/" install
./install auto
iv.
Verify it is running.
service codedeploy-agent status
14. Next, we are going to open the CodeDeploy in the AWS console
by clicking the services tab and finding the option for CodeDeploy under
Developer tools.
15. We will click on “Get Started” and then select the “Custom
Deployment” option and click next.
16. And then under the Create New Application window, we would
need to add the entries to the fields provided.
Application Name: AWSCodeDeploy
Deployment Group: AWSCodeDeploy
Add the Tags: Key- name, Value – AWS CodeDeploy
Deployment Configuration: CodeDeployDefault.OneAtATime (fine
for now or select according to your requirements)
Service Role ARN: CodeDeploy (It’s the first role we created earlier)
Finally click on Create
Application.
17. Next click on the CodeDeploy application we just created and
then click on Actions button then “Deploy New Revision”.
18. Inside the “Create New Deployment” window, add the entries to
the following fields.
Application Name: AWSCodeDeploy
Deployment Group: AWSCodeDeploy
Revision Type: Select the option for Github
Repository Name: Enter the name of the repository in the following
format- username/repository_name
shoobomb/AWS-CodeDeploy-Example
Commit ID: In the commit id, enter the 40 characters commit id you can
find on your Github repository page.
Deployment Config: CodeDeployDefault.OneAtATime
Note- The Github repository
which contains the code to deploy should also contain an ‘appspec.yml’ file
which defines the application specification for how the application would be
deployed on CodeDeploy. You can have a look at the directory structure of the
application code and the appspec.yml file in the following Github repository
which also contains a sample code to be deployed.
More details on how about
creating your own ‘appspec.yml’ file or plan a revision for your own project,
you can have a look at the AWS Documentation for the same.
19. Finally you can click on ‘Deploy’ and wait for this revision
to get successfully deployed on the EC2 instance. You can check the ‘Status’
field to check whether the revision was successfully deployed or not.
20. If you see the Status field value as Succeeded, it means your
revision has been successfully deployed on your EC2 instance. To check for the
same, you can go to the EC2 tab and look out for running instances, then check
for the Public DNS or the public IP to check the changes made to the new revision
deployed.
21. That’s it about setting up continuous deployment using
CodeDeploy, you can explore more features by going to the CodeDeploy on AWS
console.