# AWS Code Deploy to EC2

## Code deploy to EC2 instance using Code deploy Agent&#x20;

\
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**.

![](https://3776180966-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LgLv25e2BrxRC5m6flh%2F-LkIWPTgLJBEJydPshs0%2F-LkIIICVpe6k4XK84hm-%2FScreen%20Shot%202019-07-21%20at%2012.21.44%20PM.png?alt=media\&token=c658a8f5-1929-485d-9a60-1baecef17dc0)

**1 — CodeDeployRole**‌

Go to [AWS IAM Console](https://console.aws.amazon.com/iam/home) then navigate to “**Roles**“, choose “**Create New Role**“, Select “**CodeDeploy**” and attach “**AWSCodeDeployRole**” policy:​‌

![](https://cdn-images-1.medium.com/max/1600/0*x42w_F_DHdVDOu0p.)

**2 — EC2S3Role**‌

Create another **IAM** role, but this time choose **EC2** as the trusted entity. Then, attach “**AmazonS3ReadOnlyAccess**” policy:​‌

![](https://cdn-images-1.medium.com/max/1600/0*305ZkJ-z_tfJou9A.)

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:​ and add proper tag on EC2 instance.

![](https://cdn-images-1.medium.com/max/1600/0*eA1OrS-8b0jaCnG-.)

Next to **User Data** type the following script to install the **AWS CodeDeploy Agent** at boot time:Plain Text exit: ⌘↩

```
#!/bin/sh
 yum update
 yum install -y ruby
 cd /home/ec2-user
 aws s3 cp s3://aws-codedeploy-us-west-1/latest/install .
 chmod +x ./install
 ./install auto
```

![](https://cdn-images-1.medium.com/max/1600/0*T72RioSZq7b_9Dw5.)

Note: make sure to allow **HTTP** traffic in the security group.​‌

![](https://cdn-images-1.medium.com/max/1600/0*NWLLHGfkMb-C0hfp.)

Once created, connect to the instance using the **Public IP** via **SSH**, and verify whether the **CodeDeploy** agent is running:​‌

![](https://cdn-images-1.medium.com/max/1600/0*TX2ppY0ESB0463uN.)

**4 — Application**‌

Add the *appspec.yml* file to the application to describe to AWS CodeDeploy how to manage the lifecycle of your application:Plain Text exit: ⌘↩

```
‌version: 0.0
os: linux
files:
  - source: /
    destination: /home/ec2-user/api
permissions:
  - object: /
    pattern: "**"
    owner: ec2-user
    group: ec2-user
hooks:
  AfterInstall:
    - location: deploy/before_install.sh
      timeout: 1200
      runas: ec2-user
  ApplicationStart:
    - location: deploy/restart.sh
      timeout: 60
      runas: ec2-user
ValidateService:
    - location: deploy/validate.sh
      timeout: 60
      runas: ec2-user
```

**`Branch - travis-ci-codedeploy-ec2`**

{% embed url="<https://github.com/tkssharma/DevOpsTraining>" %}

## Now Lets build Pipeline Step by Step&#x20;

1. Create Pipeline and name it as Code-deploy-ec2-node and ass source provider as Github

![](https://3776180966-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LgLv25e2BrxRC5m6flh%2F-LkIWPTgLJBEJydPshs0%2F-LkIRbPa8TqcYSPuTIGK%2FScreen%20Shot%202019-07-21%20at%2012.38.31%20PM.png?alt=media\&token=05731e4a-5b50-4101-993d-f0667833388d)

Add Build Stage Next after setting up Source Repository with Branch, From here create new Build Project&#x20;

![](https://3776180966-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LgLv25e2BrxRC5m6flh%2F-LkIWPTgLJBEJydPshs0%2F-LkIRs38Sb2lxyCtw0ps%2FScreen%20Shot%202019-07-21%20at%2012.38.44%20PM.png?alt=media\&token=f2cf2185-ee1e-4d8b-89e5-8e689cbfc899)

Add Build configuration with default  build spec file which says just copy node JS code to Artifacts&#x20;

![](https://3776180966-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LgLv25e2BrxRC5m6flh%2F-LkIGuCd7UApSJz_xANs%2F-LkID2kLOo9lhM9FejmF%2FScreen%20Shot%202019-07-21%20at%2011.42.10%20AM.png?alt=media\&token=7e2856d2-e971-4065-a4c1-a8eca7534788)

finally add this  build  as  a Stage to Pipeline&#x20;

![](https://3776180966-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LgLv25e2BrxRC5m6flh%2F-LkIWPTgLJBEJydPshs0%2F-LkIS2bwpg2uzg2fBoqZ%2FScreen%20Shot%202019-07-21%20at%2012.39.54%20PM.png?alt=media\&token=3ca0924e-81e6-408c-8145-396c3d4070e0)

Add deployment PROVIDER as code deploy and Configure application and return back to same screen (Create code deploy application and deployment group )

![](https://3776180966-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LgLv25e2BrxRC5m6flh%2F-LkIWPTgLJBEJydPshs0%2F-LkITYP56yt9ZZRNhTFl%2FScreen%20Shot%202019-07-21%20at%2012.40.06%20PM.png?alt=media\&token=5fd739bf-cbd6-4b94-920b-67641028903e)

### Code deploy application&#x20;

Crate code deploy application and code deploy group&#x20;

![](https://3776180966-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LgLv25e2BrxRC5m6flh%2F-LkIWPTgLJBEJydPshs0%2F-LkITp8VBdDn3StlGGVL%2FScreen%20Shot%202019-07-21%20at%2012.41.46%20PM.png?alt=media\&token=6e2f2415-64df-4392-8cdc-df396e85533c)

Code deploy can push code to EC2 instance so lets spin up EC2 instance having S3 read only access role&#x20;

![](https://3776180966-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LgLv25e2BrxRC5m6flh%2F-LkIWPTgLJBEJydPshs0%2F-LkITsLN2L3ratMO_SU5%2FScreen%20Shot%202019-07-21%20at%2012.43.44%20PM.png?alt=media\&token=07509d01-cedd-47e3-beb1-afddadca1f40)

In User data we can add installation commands to add code deploy agent to EC2 instance as startup script&#x20;

![](https://3776180966-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LgLv25e2BrxRC5m6flh%2F-LkIWPTgLJBEJydPshs0%2F-LkITwVLtIFdfH-MYLEl%2FScreen%20Shot%202019-07-21%20at%2012.44.55%20PM.png?alt=media\&token=0e90ea53-bec5-4b9f-9f27-a96c59372a96)

Define Tag for EC2 instance so these EC2 instance can be provided as Target for Code deploy stage&#x20;

![](https://3776180966-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LgLv25e2BrxRC5m6flh%2F-LkIWPTgLJBEJydPshs0%2F-LkIU0Gi-TKbCr94e4iT%2FScreen%20Shot%202019-07-21%20at%2012.47.04%20PM.png?alt=media\&token=adb533bb-7216-45ba-b3a4-8bffcde17c1d)

Create Deployment Group&#x20;

![](https://3776180966-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LgLv25e2BrxRC5m6flh%2F-LkIWPTgLJBEJydPshs0%2F-LkIU3KRvQD398RY4B5y%2FScreen%20Shot%202019-07-21%20at%2012.47.23%20PM.png?alt=media\&token=afc4492c-4a6e-494f-b41f-d5c77c9edef0)

Once Code deployment created add Code deploy to our pipeline Last step&#x20;

![](https://3776180966-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LgLv25e2BrxRC5m6flh%2F-LkIWPTgLJBEJydPshs0%2F-LkIU5xl6XIjFs-dfQm5%2FScreen%20Shot%202019-07-21%20at%2012.49.28%20PM.png?alt=media\&token=be0fdbdc-8564-4ada-8d9f-b156e38233bf)

Now Our Pipeline has ben setup with Code Build and Code deploy to push Node JS Code TO ec2 instance&#x20;

![](https://3776180966-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LgLv25e2BrxRC5m6flh%2F-LkIWPTgLJBEJydPshs0%2F-LkIU8gi0Sk_MGuW6_zG%2FScreen%20Shot%202019-07-21%20at%2012.49.56%20PM.png?alt=media\&token=a5958fb7-554a-4fb6-bb4a-402bb259584f)

![](https://3776180966-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LgLv25e2BrxRC5m6flh%2F-LkIWPTgLJBEJydPshs0%2F-LkIUB4zojvr2srb_GFS%2FScreen%20Shot%202019-07-21%20at%2012.50.03%20PM.png?alt=media\&token=c1188b3a-4f48-4f21-bcaf-b2ba399d3323)

Trigger a dummy Commit to run pipeline&#x20;

![](https://3776180966-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LgLv25e2BrxRC5m6flh%2F-LkIWPTgLJBEJydPshs0%2F-LkIUDTJsEBa8MAM58_K%2FScreen%20Shot%202019-07-21%20at%2012.55.24%20PM.png?alt=media\&token=35aa8369-4034-4fa3-994d-8de1b2ca5ce2)

![](https://3776180966-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LgLv25e2BrxRC5m6flh%2F-LkIWPTgLJBEJydPshs0%2F-LkIUFvpYJYXAph6wrdG%2FScreen%20Shot%202019-07-21%20at%2012.59.32%20PM.png?alt=media\&token=46854561-521d-415b-9946-71a9572af25d)

Here we see everything worked as expected and we are able to see application deployed on EC2 instance&#x20;

![](https://3776180966-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LgLv25e2BrxRC5m6flh%2F-LkIWPTgLJBEJydPshs0%2F-LkIUI5pCRDcB9Rkqdx1%2FScreen%20Shot%202019-07-21%20at%201.00.08%20PM.png?alt=media\&token=e0962592-53ca-42f5-abf7-3b7e07a194ba)
