Deploying Node.js Applications to Elastic Beanstalk

Introduction

So you have a Node.js application that’s ready to be deployed and you have decided on using AWS’s Elastic Beanstalk.

With Elastic Beanstalk, you are getting automatic handling of the details of capacity provisioning, load balancing, scaling, and application health monitoring right out of the box.

Getting your application deployed properly can be a bit tricky. The purpose of this article is to serve as a how-to for deploying Node.js applications to Elastic Beanstalk specifically using command-line tooling.

Setup

There are two CLIs you will need in order to deploy from the command line.

AWS CLI

The first is AWS Command Line Interface (CLI) (aws). Downloads for various operating systems (Mac, Windows, Linux) can be found here: https://aws.amazon.com/cli/.

Once the CLI is installed, you will want to run aws configure to enter your access keys (access key ID and secret access key). These should be for an IAM user that has permissions required to deploy to Elastic Beanstalk.

More on that: https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-quickstart.html#cli-configure-quickstart-creds

Elastic Beanstalk CLI

The second CLI you want is the Elastic Beanstalk CLI (eb).

If you are a Mac user, using Homebrew is a good choice:

brew update
brew install awsebcli
eb --version (to verify the install went correctly)

Otherwise, you can manually install the CLI with directions here: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/eb-cli3-install-advanced.html

Deploying

Once we have our IAM user configured and the Elastic Beanstalk CLI in place, we can start the deployment process.

Procfile

First off, we will want to create a Procfile in your project directory.

Here we will tell Elastic Beanstalk what command to run when starting up your Node.js application. For example:

web: npm run start

If we don’t provide a Procfile, Elastic Beanstalk will run npm start if package.json exists. If that doesn’t exist, then it will try to run app.js and server.js.

Given how easy it is, creating a Procfile is the way to go, especially if you have a custom run command.

.ebignore

Next up, we can optionally create a custom ignore file that differs from .gitignore by creating .ebignore. The reason to use this is you may have items in your .gitignore that you want to keep out of source control, but are required for running your application (configs, etc).

At a minimum, your .ebignore can look something like:

node_modules
# Elastic Beanstalk Files
.elasticbeanstalk/*
!.elasticbeanstalk/*.cfg.yml
!.elasticbeanstalk/*.global.yml

As you can see node_modules is ignored. That is because Elastic Beanstalk will run npm install and download dependencies upon deployment.

eb init

Next, let’s initialize our Elastic Beanstalk deployment by running eb init. The CLI will ask you for an AWS region. I use us-east-1, but of course do what suits your need.

Next, you’ll enter the name of your new application.

After that, you should see the following question:

It appears you are using Node.js. Is this correct?

If you don’t get this question, something is likely off with your Node.js application!

Assuming you answered yes, we now get the choice of different Node versions:

Select a platform branch.
1) Node.js 14 running on 64bit Amazon Linux 2
2) Node.js 12 running on 64bit Amazon Linux 2
3) Node.js 10 running on 64bit Amazon Linux 2 (Deprecated)
4) Node.js running on 64bit Amazon Linux (Deprecated)

Again, this is up to you and your application, but 12 or 14 are the likely choices.

The next question is about CodeCommit, which will not cover in this tutorial so choose no.

Next up:

Do you want to set up SSH for your instances?
(Y/n):

Having SSH access to your instances is a big benefit but for this tutorial we will go without it.

We should now be complete with eb init and a new folder .elasticbeanstalk containing a config.yml file should be in your project directory.

eb create

Time to create an application environment using eb create. This may be your development, test, or production environment so name it accordingly.

After you’ll enter a DNS CNAME prefix which defaults to the environment name.

Then, we will select a load balancer. The default is application and it is a more dynamic choice but for this tutorial we will use classic.

Select a load balancer type
1) classic
2) application
3) network
(default is 2): 2

One last question about spot fleet requests that we will say no for the sake of the tutorial.

Now we can sit back and let the Elastic Beanstalk CLI do it’s magic. It will zip your application up and upload to S3, create a load balancer, and auto scaling configuration, among other things.

If all goes well, you should see something like:

INFO    Successfully launched environment:

eb open

Lastly, let’s confirm things are up and running with the eb open command. This will open your root URL in the browser.

Hopefully you see whatever output your application should have!

Conclusion

In conclusion, deploying to Elastic Beanstalk is a little quirky. But we have seen how to prep your project for deployment, and how relatively easy it is to initalize and create an application and environment.


Erik August Johnson is a software developer working with JavaScript to build useful things. Follow them on Twitter.