# AWS CodeCommit, CodeBuild and CodeDeploy: NodeJS App

Create an EC2 instance (Amazon Linux 2 AMI) and connect to it.

Clone this repo to the EC2 instance

```plaintext
git clone https://github.com/rjthapaa/CICD_CRUD-APP-NODEJS-.git
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693733871931/7f2c1a99-6145-4672-ae0c-91c7d47c6e99.png align="left")

### **<mark>CodeCommit</mark>**

Navigate to AWS Console &gt; CodeCommit and create a new repo

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693732674210/b9bf9926-d665-4afb-8a2e-4add74152823.png align="center")

This repo cannot be accessed or configured using the root account, so we have to create a new IAM user and configure it

### **<mark>IAM USER</mark>**

1. Navigate to **<mark>IAM &gt; Users</mark>**
    
2. You can create a new user by clicking **<mark>Add Users</mark>**
    
3. Give him an **<mark>AWSCodecommitPowerUser</mark>** Policy permissions
    
4. Click on **<mark>Create user</mark>**
    

Attach the below policy

```plaintext
AWSCodeCommitPowerUser
```

Once the IAM user is created under **security credentials** generate **Access keys** and **HTTPS Git credentials for AWS CodeCommit**

**Access keys** are used to connect as a User to the **AWS console** through **AWS CLI** and **HTTPS Git cred** for cloning and pushing the code to **AWS CodeCommit**.

### **<mark>EC2 instance</mark>**

Connect to the instance and start installing AWS CLI.

```plaintext
$ curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
sudo yum install unzip
unzip awscliv2.zip
sudo ./aws/install
aws --version
```

Configure the CLI in the instance

```plaintext
aws configure
```

Configure the Access and Secret keys that we generated from the IAM user

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693733345175/3c015a08-fbc7-4429-a50a-4b13e841b634.png align="left")

Now clone the repository from CodeCommit to the instance

Copy the HTTPS link from the repo

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693733473750/1d04eba4-a201-4d77-9a84-4de2a99dc76e.png align="left")

On the EC2 instance use the git clone command to clone the repo

```plaintext
git clone <HTTPS url>
```

Provide the **HTTPS creds,** we downloaded for the **IAM USER to clone.**

Once it is a successful empty repository from CodeCommit is cloned into the Instance.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693733953803/1d0ccb59-bd33-43f1-80c5-0dc521e86da9.png align="center")

Now copy all the files from **CICD\_CRUD-App** files into **nodejsrepo** and commit it.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693734092724/2168ccb7-dc29-4138-9183-6b739fbb9baf.png align="left")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693734168411/efe06d86-affa-4421-a120-8cd819963e25.png align="left")

Now check the AWS CodeCommit repository

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693734214200/e7b2312d-6f40-4cfa-b715-2e57d1b1740d.png align="left")

### **<mark>CodeBuild</mark>**

Go to CodeBuild and click on **Create Project**

Codebuild will be built from the CodeCommit repo files only

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693734963455/96ea6970-2516-4a0c-aa83-fe64929c5fd4.png align="left")

Provide the source details **AWS CodeCommit** and **<mark>Repo/branch details</mark>**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693735045908/c35c00de-840e-41b8-ad65-48a26aeb135e.png align="center")

Update the OS and Environment type

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693735121784/61a95417-a165-4afe-92e8-4838cad382a5.png align="left")

We need to create a service role so that CodeBuild can communicate with CodeCommit, CodeDeploy, S3, artefact and other resources

Select **<mark>New service role</mark>** and the **<mark>Role name</mark>** will be automatically provided

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693735154283/410d29a4-3e72-44b0-b40a-fb414637f1f5.png align="left")

Artifacts are the storage location of our S3 bucket.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693736035799/9301f33c-99a4-4227-9f16-9c27f584a603.png align="left")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693736065171/2564ed75-07cf-48a1-8329-8cf1c80fca9d.png align="center")

That's it Click **Create Build Project.**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693736122631/8022ab35-c6e2-47d4-8fe8-aeca39961354.png align="center")

Let's start the **Start Build** now

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693736174823/0216b4f0-13ee-40a8-bd74-f10e1ca27b2d.png align="left")

Build is success

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693737401233/8926fc13-cf5d-4284-b423-1649bf5480c3.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693737426197/e12655be-b2d4-43b5-9a32-1f6e578556ca.png align="center")

Check the artifacts in S3 Bucket

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693737545587/c6c348d4-e282-4069-8d10-beb3ddf54f43.png align="center")

### **<mark>CodeDeploy</mark>**

We will be deploying the application with the help of CodeDeploy, for that we need EC2 with sufficient privileges and a code deploy agent installed. We also need an application package in the form of a Zip in S3 bucket.

For Code Deploy we need **appspec.yml** and **deploy script**

Deploy agent needs to be installed first on the EC2 instance, before installing the Agent, we need to create an IAM role for the EC2 instance and attach it.

Once the role is created, go to the EC2 instance and select it. Click on&gt; **<mark>Actions &gt; Security &gt; Modify IAM role</mark>**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693740342956/dec2cd4f-8d05-4866-abd3-f95f0e8d80ad.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693740661102/08b109aa-7f81-49b9-ba26-25d9c4a0525e.png align="center")

Click on Update IAM role

**$ nano deployagent.sh**

refer [link](https://docs.aws.amazon.com/codedeploy/latest/userguide/codedeploy-agent-operations-install-linux.html)

```plaintext
#!/bin/bash
sudo yum update
sudo yum install ruby
sudo yum install wget
cd /home/ec2-user
wget https://aws-codedeploy-us-east-1.s3.us-east-1.amazonaws.com/latest/install
chmod +x ./install
sudo ./install auto
sudo service codedeploy-agent status
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693741250888/1ad6ccde-98f8-42ea-912b-7a869dac2196.png align="center")

Agent is running successfully on instance

**<mark>Code Deploy</mark>**

Click on Create application and provide the details

Click on Create application.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693738491033/d223f62e-75c2-4775-8efd-030506d134e4.png align="left")

Application is created and now we should create a deployment group

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693738613814/840e77f7-2969-43e2-857f-72027d197229.png align="center")

To run or deploy one or more applications on multiple servers we need a deployment group

Click on **<mark>Create Deployment Group</mark>,** give it a name

Under **<mark>service role</mark>**, we need to create a **<mark>new role</mark>** and attach it here

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693739009068/b23179f8-e13b-46a6-8aa5-61d88b3e4765.png align="left")

Let's navigate to **<mark>IAM</mark>** and create a role **<mark>code-deploy-service-role </mark>** with the below permissions (**AWSCodeDeployRole)**

Once the role is created get back to CodeDeploy You will be able to view a service role that we have created, select it

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693739352416/0591060f-9f68-4c7c-ade7-d1d3bb9730c4.png align="left")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693739399105/abb43fab-e518-4367-af02-6d4785300461.png align="left")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693739453167/f28b86b1-bab3-44d8-aba3-b42c4f94fe65.png align="center")

Click on **Create Deployment group**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693739634599/466e7015-ab9b-4332-9ad5-56573199c304.png align="left")

Deployment Group is created successfully, now we will create a deployment by clicking Create Depolyment

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693739873645/51064b8a-d79f-4904-a74c-09cef877ef2d.png align="left")

Now move to the S3 bucket and make sure to make the project public

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693739920256/1e04e2e5-ea47-48f9-a9c8-b801175e2178.png align="center")

Leave the rest to default and click on **Create deployment (Make sure to check the agent status)**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693741354937/edeb69f3-d0f6-413d-be59-5728f79978cd.png align="center")

Make sure you install nodejs 16 or 18 version on the EC2 instance

Refer [link](https://github.com/rjthapaa/Node-distributions)

```plaintext
sudo yum install https://rpm.nodesource.com/pub_16.x/nodistro/repo/nodesource-release-nodistro-1.noarch.rpm -y
sudo yum install nodejs -y
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693813549227/e0d0012e-5f9c-4329-8d30-6110d0ca0627.png align="left")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693813851961/68bcb6ee-c2ca-4751-93ce-ca9dacb371a4.png align="center")

Click on View Events to check the status of the events

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693813904224/80386c68-c3b5-4572-800b-050664246019.png align="center")

Our deployment is successful, let's check our application using EC2 instance publicIP:3000

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693814021654/c173ff70-e938-4640-8f67-0758eeb0b400.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693814037168/9787eb62-f8cc-4bb5-86bc-b431634cfd28.png align="center")

Learn [AWS Continuous integration](https://www.youtube.com/watch?v=vvy0MNWkKyo&list=PLy_Xydy8Vp7nJV4LvQSDMaYxAQ3ES0daW&index=1)

---

### **<mark>On Ubuntu20</mark>**

refer [link](https://docs.aws.amazon.com/codedeploy/latest/userguide/codedeploy-agent-operations-install-ubuntu.html)

Git link [https://github.com/rjthapaa/CICD\_CRUD-APP-NODEJS\_Ubuntu20.git](https://github.com/rjthapaa/CICD_CRUD-APP-NODEJS_Ubuntu20.git)

```plaintext
#!/bin/bash
sudo apt update
sudo apt install ruby
sudo apt install wget
cd /home/ubuntu
wget https://aws-codedeploy-us-east-1.s3.us-east-1.amazonaws.com/latest/install
chmod +x ./install
sudo ./install auto
sudo service codedeploy-agent start
sudo service codedeploy-agent status
```

Attach the IAM role to EC2 before installing the deploy agent and run the script

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693817205357/61fb5147-acb0-42bd-a73b-3fb8597144ce.png align="center")

pm command not found issue

```plaintext
sudo apt install npm
sudo npm install -g pm2
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693822657268/0341f6d3-795a-42d3-866b-9a14ada583d2.png align="center")
