Building a Node.js Express Application with AWS CodeBuild and Amazon ECR🚀
Tags: aws, node, docker, devops
Published: 2024-01-19 07:08:09

## Table of Content:

1. [Introduction](#introduction)
2. [What is AWS CodeBuild] (#what-is-aws-codebuild)
3. [What is Amazon ECR] (#what-is-amazon-ecr)
4. [Prerequisites] (#prerequisites)
5. [Step 1: Buildspec.yml] (#step-1-buildspecyml)
6. [Step 2: IAM Roles and Permissions] (#step-2-iam-roles-and-permissions)
7. [Step 3: Create a CodeBuild Project] (#step-3-create-a-codebuild-project)
8. [Step 4:  Run CodeBuild project] (#step-4-run-codebuild-project)



### Introduction 

In this article, we'll explore how to use AWS CodeBuild to build a Node.js Express application, create a Docker image, and push it to Amazon ECR.

### What is AWS CodeBuild:

![AWS CodeBuild](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lhmwbemdtlj5e9tydmpk.png)

AWS CodeBuild is a fully managed continuous integration service by Amazon Web Services that automates the build and testing phases of software development. 

Developers define build projects using buildspec.yml files, specifying the steps for building Docker images and running tests. 

With support for various programming languages, build tools, and seamless integrations with AWS services and version control systems, CodeBuild facilitates the efficient creation, testing, and deployment of applications within a scalable and customizable environment.


### What is Amazon ECR:


![Amazon ECR Logo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g6og8qgh39m3c77vukzn.png)

Amazon Elastic Container Registry (Amazon ECR) is a fully managed Docker container registry service provided by Amazon Web Services (AWS). It allows developers to store, manage, and deploy Docker container images. 

ECR integrates seamlessly with other AWS services, making it easy to build, store, and deploy containerized applications using tools like Amazon ECS (Elastic Container Service) and AWS Fargate. With features such as image scanning, encryption, and fine-grained access control, Amazon ECR provides a secure and scalable solution for container image management within the AWS ecosystem.

> Without delay, let's delve into the practical aspects of the topic at hand.


![Practice Meme](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6cylrf9engh7r690m4yc.jpg)

### Prerequisites

Before you begin, make sure you have the following prerequisites:
1. An AWS account
2. A Node.js Express application hosted on a version control system like CodeCommit or GitHub
3. Already setup ECR repository

## Step 1: Buildspec.yml 

![buildspec.yml file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p0rdtj07no6hjbw7sl1j.JPG)
Create a `buildspec.yml` file in the root of your Node.js Express project. This file defines the build steps for CodeBuild.

```
version: 0.2

phases:
  install: 
    runtime-versions:
      nodejs: latest

  pre_build:
    commands:
      - echo Logging in to Amazon ECR...    
      - aws --version
      - aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com
      - COMMIT_HASH=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)


  build:
    commands:
      - echo Build started on `date`
      - echo Building the Docker image...
      - docker build -t $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$IMAGE_REPO_NAME:$IMAGE_TAG .
      - docker tag $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$IMAGE_REPO_NAME:$IMAGE_TAG $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$IMAGE_REPO_NAME:latest
  
  post_build:
    commands:
      - echo Build completed on `date` 
      - echo Pushing the Docker image...
      - docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$IMAGE_REPO_NAME:$IMAGE_TAG
```

## Step 2: IAM Roles and Permissions

To enable CodeBuild to push Docker images to Amazon ECR, it is essential to establish IAM roles and permissions.

Begin by crafting an IAM role specifically for CodeBuild, equipped with the necessary authorizations

1. ECR permissions to facilitate the pushing of Docker images to your repository. Opt for the aws-managed `AmazonEC2ContainerRegistryPowerUser` role for a streamlined approach.

2. CloudWatch Logs permissions to enable the writing of build logs to CloudWatch Logs. Activating CloudWatch during the setup will automatically add the requisite policies.

## Step 3: Create a CodeBuild Project

a) To create a CodeBuild project, you need to follow these steps.
- Open the AWS Management Console.
- Navigate to the CodeBuild service.
- Click on "Create build project.


![CodeBuild AWS Console](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zsexlobwg8d7f4mji8vj.JPG)

![create Build Project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/du4daedatp70aesdn3rp.JPG)

b) Give your project a name and description, and select the source code location as the newly created AWS CodeCommit repository, and the associated branch.


![Source](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/38jaeujog8xs5dhjtdie.JPG)

c) For the Environment section, use the below settings:
- **Provisioning mode**: On-demand
- **Environment image**: Managed image
- **Compute**: EC2
- **Operating system**: Amazon Linux
- **Runtime(s)**: Standard
- **Image**: use the latest standard image
- **Image version**: Always use the latest image for this runtime version

![Environment Settings](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e31a0f13kktec3ppea7s.JPG)

d) For the Service role, select the Existing IAM Role, you created earlier with `AmazonEC2ContainerRegistryPowerUser`

e) Additional configuration:
Scroll down to Environment Variables and update your environmental variables

![Environmental Variables](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vq0w9s3zf8rrhghz3nka.JPG)

f) For Build specifications, select **Use a buildspec file** and for **Logs**, add CloudWatch Group name, Stream name and click **Create build project**

## Step 4:  Run CodeBuild project

Select the codebuild project and click on **Start build**
Wait for build to Succeed!
Then check ECR for build Image.

![build complete status](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ed1lwyb8f50tstcb1jz.JPG)

![ECR Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r9wj1q4d7715uig5igyf.JPG)

And that's it! You have successfully used AWS CodeBuild to build a Node.js Express application, create a Docker image, and push it to Amazon ECR.


![Victory Dance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ub27bbgk5m0l1p35jk9n.jpg)