Deploying Your Node API to AWS Using Elastic Beanstalk (Demo Project)

Intro

This article is a step-by-step guide on setting up and deploying a sample Node JS application on AWS using Elastic Beanstalk.

I completed this project as part of the Cloud Developer Nanodegree at Udacity. You can find the source code on GitHub.

You will walk through the following steps:

  • Run the Node project locally
  • Set up the AWS CLI, users, and permissions
  • Set up Elastic Beanstalk
  • Build and deploy the application
  • Consume the publicly accessible endpoint via Postman

About the Nanodegree

The Nanodegree will train you in critical AWS skills to become an effective cloud developer.

Some of the covered topics are:

  • Cloud Computing in AWS
  • Storage & Content Delivery
  • Cloud Security
  • Networking & Elasticity
  • Deployments via AWS Elastic Beanstalk
  • Microservices Design Principles
  • Messaging & Containers, Docker
  • Containers orchestration with Kubernetes in AWS
  • Serverless apps, Lambda functions, Auth

Each of the main sections of the Nanodegree ends with a small project where you can interactively put your knowledge into action.

Also, you’ll get to complete a final Capstone project putting together most of the concepts you go through during the learning journey.

Don’t let the pricing discourage you – at the time I took the Nanodegree, every customer could use a discount coupon that reduces the cost by up to 90%!

Project Overview

The business logic is not that important as the main focus is on deploying and running the project in AWS.

The functionality is quite simple.

There’s a single API endpoint where you pass some public image URL, and you get back a “normalized” version of the image e.g. converted to grayscale and resized to some standard dimensions. This can potentially be used in a Computer Vision pre-processing pipeline.

Let me help you get a better sense of this.

Imagine you have some image like this one:

You can use the deployed service and pass the image public URL to get a result like this:

Run Locally

Running the project on your local machine is quite simple.

After you’ve cloned the repo, you first need to install the Node dependencies via npm install

Then, execute npm run dev to start your local API on port 8082:

You can do another test with Postman on localhost, making sure everything works as expected:

Let’s move towards the AWS deployment.

Setup the AWS IAM User

In order to use Elastic Beanstalk to deploy your app, you need a user with the appropriate permissions. I advise against using your root user even for demo purposes.

Go to the IAM dashboard and start creating a new user. Check the Programmatic access checkbox.

Then attach the Elastic Beanstalk Admin policy:

Once you’re done with this, download the user credentials via the Download .csv button. This will be needed to set up the AWS CLI.

Install the AWS CLI

Now, you need to install the AWS CLI. Find the installation steps for your environment here.

Set Up the AWS CLI with the IAM User Credentials

You should then configure your AWS CLI to work with the user you just created. Refer to the downloaded .csv file to find the Access Key ID and the Secret Access Key to do so:

I have already deleted this user. Make sure these secrets are not shared with anyone!

Build and Deploy to AWS

In this section, we’ll go through the steps of building, deploying, and running your app in AWS.

Install Elastic Beanstalk CLI

First, you need to install the Elastic Beanstalk CLI following these steps.

In essence, you have to clone a git repo and run a python script that will automate the setup for you:

Once the installation is successful, you should add the EB CLI to your PATH variable. I am using WSL2 with Ubuntu on my Windows machine, so this means executing the following:

echo 'export PATH="/home/vasilkosturski/.ebcli-virtual-env/executables:$PATH"' >> ~/.bash_profile && source ~/.bash_profile

Generate an EC2 Key Pair

A public/private key pair allows you to prove your identity when connecting to the EC2 instances provisioned for your Elastic Beanstalk application. The public key is stored on the EC2 instance, and you keep the private key. Then, you can use the private key to securely login via SSH.

For instructions on creating a key pair, see Creating a Key Pair Using Amazon EC2.

Here’s a screenshot from my setup:

Build and Produce Deployment Artifacts

It’s time to build the project by running npm run build in your local directory.

This will do the following:

  1. Transpile Typescript
  2. Collect the source files
  3. Produce an archive

After the build is successful, you should see the following www output directory:

We’ll use the Archive.zip file to deploy the project via Elastic Beanstalk. You’ll see how to do that in a later section.

EB Init

You need to set up Elastic Beanstalk for the project using the eb init command.

In the screenshots below, you’ll see the steps to go through. For some of them, I’ve just used the default options by hitting Enter.

At this point, you should have a new directory named .elasticbeanstalk with a file config.yml

We’re using an artifact-based build project, so you need to add a reference to the Archive.zip file as shown below.

EB CLI – Create Environment

Now that we have the EB configuration for the project, it’s time to do the actual deployment using the eb create.

This launches an environment in AWS and deploys your app there.

Consuming the API

You can now navigate to the Elastic Beanstalk dashboard and see the newly created application. There, you will spot the public URL, which can be used to invoke your API.

Using Postman (or any other HTTP client), you can test your new API. In my case, I’m sending a GET request to the following URL:

http://image-processing-aws-udacity-dev.eu-central-1.elasticbeanstalk.com/filteredimage?image_url=https://vkontech.com/wp-content/uploads/2022/02/eb_panda_orig.jpg

You can get the Postman collection from the GitHub repo.

Summary

This article was a quick demo of how to deploy your Node API to AWS using Elastic Beanstalk.

I hope you found it useful!

See you next time!

Resources

  1. Udacity Nanodegree
  2. Source Code

Site Footer

Subscribe To My Newsletter

Email address