We’re always on the lookout for ways we can improve our development and deployment processes. Most projects we undertake make use of Amazon AWS vast infrastructure. The ever popular AWS has grown leaps and bounds since its introduction to the startup world. Controlling startup cost is essential and AWS is a great asset to have in that regard.

Today we’ll take a peak at the CodeDeploy feature within the web service and how to integrate your app deployed on AWS with Github.

1_cover

(ref. http://docs.aws.amazon.com/codedeploy/latest/userguide/welcome.html)

The diagram above describes the workflow for the deployment. AWS (Amazon Web Service) CodeDeploy is a service that allows you to deploy your application to instances easily. Here are the steps to deploy an application from Github to Amazon EC2 instances:

Create IAM policies and IAM Roles

2.   Create an IAM user

3.   Setup an instance

4.   Setup Github

5.   Deploy your code

For more detailed information, please visit: http://docs.aws.amazon.com/codedeploy/latest/userguide/welcome.html

STEP1: Create IAM policies and IAM Roles

For each AWS account you need to create 2 IAM roles. First, you have to create a service role for CodeDeploy. Second, you need to create an IAM instance profile role.  Note: This must occur before creating an instance for your application.

2_aws_menu

3_create_policy

First, log into your AWS console and select IAM from the Services menu on the top (see above pic). In the Policies section, select Create Policy and choose “Create Your Own Policy”.  In the “Create Your Own Policy” section, add a policy “CodeDeployAccess” (you can use any name you want) and attach the following lines:

Screen Shot 2015-06-08 at 12.45.38 PM

Next, in the Roles section, create a service role and attach the policy you just created. Click Edit Trust Relationship under Trust Relationships and attach the following lines:

Screen Shot 2015-06-08 at 12.46.12 PM

(Note: You can specify “codedeploy.amazonaws.com” if you don’t care about restricting the service role by region)

Now you have created a service role that lets AWS CodeDeploy access your instance. 

Next you have to create an IAM instance profile. To do that you’ll need go back to the Roles section (left menu) and create a new role – CodeDeployDemo-EC2 (you can use any name you want) with the following policy. (see below for an example of a policy document).

Screen Shot 2015-06-08 at 12.46.12 PM

Basically, you’re attaching a policy, attaching it to a role and repeating the process again. Let’s move on to Step 2 and 3.

STEP 2: Create an IAM user

In the Users section, create an IAM user and attach the policy “CodeDeployDemo-EC2”.

STEP3: Create an instance

  4_ami

5_create_ec2_instance

Now we’re ready to have some fun. Let’s create an EC2 instance.

Go to your EC2 Dashboard and click Launch Instance. I’m using Amazon Linux AMI. Don’t forget to attach your IAM role CodeDeployDemo-EC2 (not service role) before launching your site. Next, install LAMP Web Server on your server. Open up your terminal and connect to your instance. You can see instructions if you click Actions->Connect in the EC2 Dashboard.

Here’s a link to the commands. 

6_LAMP

Screen Shot 2015-06-08 at 12.49.13 PM

Once you have installed LAMP Web Server successfully, you should see the following page:

7_test_page

(ref. http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/concepts.html)

Since there is no content in /var/ww/html, this test page will appear. If you upload your application file to the folder , your content will appear instead of this test page.

STEP 4: Setup Github

Upload an application to the repository on Github. An application can be code files, web files, configuration files, and so on.

You also need to upload this appspec.yml file to github in the root in order to upload your application to the right path.

Screen Shot 2015-06-08 at 12.50.46 PM

STEP 5: Deploy your Code

8_codedeploy

On AWS’s home page, click CodeDeploy and create new application. Choose the application name, deployment group name, tag and service role. (Select the service role you just created)

9_deploy_group

Expand the menu under your group and click Deploy New Revision.

10_connect with GIthub

Choose your application, group and select “My application is stored Github.” Then click “Connect With Github.”

11_github

You can find the Repository Name (repositoryName/projectName) and commit ID on Github.

12_commitIid

Click Deploy Now and you should be able to see your application in your instance soon when the deployment process is done.

All done!

Reference:

CodeDeply

http://docs.aws.amazon.com/codedeploy/latest/userguide/welcome.html

EC2

http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/concepts.html