DEV Community

Cover image for Building a Node.js Express Application with AWS CodeBuild and Amazon ECR🚀
Ukeme David Eseme
Ukeme David Eseme

Posted on • Updated on

Building a Node.js Express Application with AWS CodeBuild and Amazon ECR🚀

Table of Content:

  1. Introduction
  2. What is AWS CodeBuild
  3. What is Amazon ECR
  4. Prerequisites
  5. Step 1: Buildspec.yml
  6. Step 2: IAM Roles and Permissions
  7. Step 3: Create a CodeBuild Project
  8. 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

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

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

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
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
Enter fullscreen mode Exit fullscreen mode

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

create Build Project

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

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

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

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

ECR Image

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

Top comments (0)