This post will walk you through how to AutoDeploy your application from Github using AWS CodeDeploy.
Let’s start first by creating 2 IAM roles we will use in this tutorial:
- IAM role for CodeDeploy to talk to EC2 instances.
- IAM role for EC2 to access S3.
1 – CodeDeployRole
Go to AWS IAM Console then navigate to “Roles“, choose “Create New Role“, Select “CodeDeploy” and attach “AWSCodeDeployRole” policy:
2 – EC2S3Role
Create another IAM role, but this time choose EC2 as the trusted entity. Then, attach “AmazonS3ReadOnlyAccess” policy:
Now that we’ve created an IAM roles, let’s launch an EC2 instance which will be used by CodeDeploy to deploy our application.
3 – EC2 Instance
Launch a new EC2 instance with the IAM role we created in last section:
Next to User Data type the following script to install the AWS CodeDeploy Agent at boot time:
yum install -y ruby
aws s3 cp s3://aws-codedeploy-us-west-1/latest/install .
chmod +x ./install
Note: make sure to allow HTTP traffic in the security group.
Once created, connect to the instance using the Public IP via SSH, and verify whether the CodeDeploy agent is running:
4 – Application
Add the appspec.yml file to the application to describe to AWS CodeDeploy how to manage the lifecycle of your application:
- source: /index.html
- location: deploy/before_install
- location: deploy/restart_server
The BeforeInstall, will install apache server:
yum install -y httpd
service httpd start
The AfterInstall will restart apache server
service httpd restart
5 – Setup CodeDeploy
Go to AWS CodeDeploy and create a new application:
Select In-Place deployement (with downtime):
Click on “Skip“, because we already setup our EC2 instance:
The above will take you to the following page where you need to give a name to your application:
Select the EC2 instance and assign a name to the deployment group:
Select the CodeDeployRole we created in the first part of the tutorial:
Then click on “Deploy“:
Create a deployment, select Github as the data source:
Just select “Connect to GitHub“. Doing that will pop up a new browser window, take you to Github login where you will have to enter your username and password
After that come back to this page, and you should see something like below, just enter the remaining details and click “Deploy”
This will take you to a page as follows:
If you point your browser to the EC2 public IP, you should see:
Now, let’s automate the deployment using Github Integrations.
6 – Continuous Deployment
Go to IAM Dashboard, and create a new policy which give access to register and create a new deployment from Github.
Next, create a new user and attach the policy we created before:
Note: copy to clipboard the user AWS ACCESS ID & AWS SECRET KEY. Will come handy later.
7 – Github Integration
Generate a new token to invoke CodeDeploy from Github:
Once the token is generated, copy the token and keep it. Then, add AWS CodeDeploy integration:
Fill the fields as below:
Finally, add Github Auto Deployment
Fill the form as below:
To test it out, let’s edit a file or commit a new file. You should see a new deployment on AWS CodeDeploy: