Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Peter-Schorn/2d7b001d23069bd682d745489a9ef38f to your computer and use it in GitHub Desktop.
Save Peter-Schorn/2d7b001d23069bd682d745489a9ef38f to your computer and use it in GitHub Desktop.

How to Deploy a NodeJS Application to AWS using Elastic Container Registry and App Runner

Signup for Amazon Web Services (AWS)

To get started, sign up for an aws account here.

Install AWS CLI

Install the AWS CLI here.

Download Docker

To containerize your application, you must download Docker. After the download completes, launch Docker and leave it running.

Configure Repository

Next, create a file called Dockerfile in the root of your project, with the following text. You may need to customize the EXPOSEand CMD directives.

FROM node:18

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN npm install
# If you are building your code for production
# RUN npm ci --only=production

# Bundle app source
COPY . .

EXPOSE 3000  # <--- Set this to the port your application listens on

CMD [ "node", "index.js" ]  # <--- Set this to the path to the main file in the project; 
                            #      e.g., "app.js".

Create Private Elastic Container Registry

Ensure you are signed in to the aws console. Then go to the Amazon Elastic Container Registry Service here.

Ensure the "Private" tab is selected. Then, click on the orange "Create repository" button. For Visibility settings, choose "Private". Give your repository a unique, meaningful name. Leave the default values for all the other choices, scroll down, and click "Create repository".

Build Image and Push to Registry

Screenshot 2022-11-19 at 7.50.53 AM

Next, click on your newly-created repository.

Screenshot 2022-11-19 at 7.49.41 AM

Now click on "View push commands". In your terminal, ensure the root of your node project is your working directory. Then, follow the steps in the "Push commands" window. This will create a docker image of your project and push it to your private container registry. After the push commands finish, refresh the current page, and a docker image should show up, as demonstrated below.

Screenshot 2022-11-19 at 7.56.46 AM

Read more about Amazon Elastic Container Registry here.

Create App Runner Service

Create an App Runner service here.

image

  1. For "Repository type", choose "Container registry"
  2. For "Provider", choose "Amazon ECR". Then click "Browse" below. Select the image repository you just created.
  3. For "Deployment trigger", choose "Automatic".
  4. For "ECR access role", choose "Create new service role"
  5. Click Next.
  6. For "Service name", choose a meaningful name for the application.
  7. For port, use the port that your application listens on.
  8. Click "Next"; then, click "Create & deploy".
  9. Wait for the application to finish deploying.
  10. Finally, click on the link under "Default main" to see your live site.
  11. To re-deploy, follow the steps in the push commands window of your repository.

Read more about AWS App Runner here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment