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

![](/files/-LkIIICVpe6k4XK84hm-)

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

![](/files/-LkIRbPa8TqcYSPuTIGK)

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

![](/files/-LkIRs38Sb2lxyCtw0ps)

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

![](/files/-LkID2kLOo9lhM9FejmF)

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

![](/files/-LkIS2bwpg2uzg2fBoqZ)

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

![](/files/-LkITYP56yt9ZZRNhTFl)

### Code deploy application&#x20;

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

![](/files/-LkITp8VBdDn3StlGGVL)

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

![](/files/-LkITsLN2L3ratMO_SU5)

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

![](/files/-LkITwVLtIFdfH-MYLEl)

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

![](/files/-LkIU0Gi-TKbCr94e4iT)

Create Deployment Group&#x20;

![](/files/-LkIU3KRvQD398RY4B5y)

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

![](/files/-LkIU5xl6XIjFs-dfQm5)

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

![](/files/-LkIU8gi0Sk_MGuW6_zG)

![](/files/-LkIUB4zojvr2srb_GFS)

Trigger a dummy Commit to run pipeline&#x20;

![](/files/-LkIUDTJsEBa8MAM58_K)

![](/files/-LkIUFvpYJYXAph6wrdG)

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

![](/files/-LkIUI5pCRDcB9Rkqdx1)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://tkssharma-devops.gitbook.io/devops-training/devops-01-continuous-integration/aws-code-pipeline-ci-cd/aws-ci-cd-tools/aws-code-pipeline.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
