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.
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.
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:
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:
(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).
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
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.
Once you have installed LAMP Web Server successfully, you should see the following page:
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.
STEP 5: Deploy your Code
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)
Expand the menu under your group and click Deploy New Revision.
Choose your application, group and select “My application is stored Github.” Then click “Connect With Github.”
You can find the Repository Name (repositoryName/projectName) and commit ID on Github.
Click Deploy Now and you should be able to see your application in your instance soon when the deployment process is done.