You are currently viewing Creating a scalable Node.js application on AWS using Elastic Beanstalk

Creating a scalable Node.js application on AWS using Elastic Beanstalk

Welcome to our guide on Deploying Node.js application on AWS using Elastic Beanstalk. In this tutorial, we will show you how to use the power of AWS Elastic Beanstalk to deploy and scale your Node.js application on the cloud. 

Node.js is a popular JavaScript runtime that allows developers to create fast and efficient server-side applications. When building a Node.js application, it’s important to consider scalability from the start, as the application will likely need to handle a growing number of users and requests over time.

AWS Elastic Beanstalk is a fully managed service for deploying and scaling web applications and services. It makes it easy to deploy, run, and scale Node.js applications on AWS.

In this blog post, we will show you how to create a simple “Hello World” Node.js application that can be easily deployed and scaled on AWS using Elastic Beanstalk. The application will be a web server that listens for incoming HTTP requests and returns a “Welcome to the Node.JS World” message when the user navigates to the “/” endpoint.

Creating the “Hello World” Application

Here is a step-by-step guide on how to create a sample Node.js “Hello World” application:

  • Install Node.js: In order to create a Node.js application, you will need to have Node.js and npm (Node Package Manager) installed on your local machine. You can download the latest version of Node.js from the official website (https://nodejs.org/en/download/)

  • Create a new directory: Use the command line interface (CLI) of your choice to create a new directory for your application.

mkdir my-app
  • Navigate to the new directory: Use the `cd` command to navigate to the newly created directory.
cd my-app
  • Initialize a new Node.js project: Use the npm init command to initialize a new Node.js project. This command will prompt you for information about your project, such as the name, version, and entry point. You can accept the defaults by pressing enter or input the desired value.
npm init
  • Install the Express.js framework: Express.js is a popular web framework for Node.js. Use the npm install command to install the express framework.
npm install express
  • Create a new file named app.js: Use a text editor of your choice to create a new file named app.js in the root of your project directory. This file will contain the code for your “Hello World” application. 
  • Write the “Hello World” code: Add the following code to the app.js file:

const express = require('express');
const PORT = process.env.PORT || 3000;
const app = express();

app.get('/', function (req, res) {
  res.send('Welcome to the Node.JS World!');
});

app.listen(3000, function () {
  console.log('Example app listening on port '+PORT);
});

This code creates a new Express application, sets up a route for the root URL (/), and sets up the application to listen on port 3000.

  • Start the application: Use the node command to start the application.
node app.js
  • Verify the application: Open a web browser and navigate to http://localhost:3000/. You should see the “Welcome to the Node.JS World!” message displayed on the page.

Deploying the Application to Elastic Beanstalk

To deploy the application to Elastic Beanstalk, you will need to create a new Elastic Beanstalk application version and then deploy it to your environment. The easiest way to do this is by uploading your application code to an S3 bucket and then selecting the uploaded file in the Elastic Beanstalk console. Alternatively, you could use the Elastic Beanstalk command line interface (CLI) to create and deploy an application version. For this example, we will use the Elastic Beanstalk command line interface to deploy our node application. 

  • Install the Elastic Beanstalk CLI: To deploy a Node.js application to Elastic Beanstalk using the CLI, you will first need to install the Elastic Beanstalk CLI on your local machine. You can do this by running the following command:
pip install awsebcli

You may need to use pip3 instead of pip, depending on your setup. If you want more details you can check the EBCLI Manual Installation documentation. 

  • Initialize your Elastic Beanstalk Application: Navigate to the root directory of your Node.js application and run the following command to initialize your Elastic Beanstalk application: 
eb init --platform node.js --region us-east-1

This command creates a configuration file in a folder named .elasticbeanstalk that specifies settings for creating environments for your application, and creates an Elastic Beanstalk application named after the current folder.

  • Create an Elastic Beanstalk environment: Once your application is initialized, you can create an Elastic Beanstalk environment by running the following command:
eb create --sample my-app-env

This command creates a load-balanced environment with the default settings for the Node.js. 

  • Configuring the application process with a Procfile: You can include a file that’s called Procfile at the root of your source bundle to specify the command that starts your application.

web: node app.js
  • Deploy your application: To deploy your application to the Elastic Beanstalk environment, you can use the following command:
eb deploy

This command will package your application and its dependencies into a .zip file, and then upload and deploy the file to your Elastic Beanstalk environment. Elastic Beanstalk will automatically update the instances and load balancer with the new version of the application.

Scaling the application 

There are several ways to scale a Node.js application on Elastic Beanstalk, we will use the Elastic Beanstalk CLI to perform operations such as scaling up or down, checking the current capacity and configure automatic scaling.

  • Check the current capacity: To check the current capacity of your Elastic Beanstalk environment, you can use the following command:
eb scale

This command will show the current number of instances running in your environment and the amount of memory and CPU allocated to each instance.

  • Scale up or down: To scale up or down your Elastic Beanstalk environment, you can use the following command:
eb scale <environment_name> --instance-type <instance_type> --instance-count <instance_count>

For example, to increase the number of instances to 2 and change the instance type to t3.medium

eb scale my-environment --instance-type t3.medium --instance-count 2
  • Monitor the scaling process: You can monitor the progress of the scaling process by running the following command:
eb events

This command will show the events related to your environment, including scaling events.

It’s worth mentioning that, when scaling up or down, Elastic Beanstalk will automatically adjust the number of instances running in your environment and will automatically reconfigure the load balancer to route traffic to the new instances. Additionally, Elastic Beanstalk also allows you to configure Auto Scaling policies to automatically adjust the number of instances in response to CloudWatch alarms.

In summary, deploying and scaling a Node.js application on AWS Elastic Beanstalk can be a quick and efficient way to manage and run your application on the cloud. Elastic Beanstalk provides a user-friendly interface, integration with other AWS services, and the option to automate deployment and scaling tasks using the Elastic Beanstalk CLI. With Elastic Beanstalk, you can easily deploy and scale your Node.js application on the cloud, without worrying about configuring and maintaining the underlying infrastructure.

This Post Has One Comment

  1. Omar

    This article was extremely helpful in getting my nodejs application deployed to AWS! Props to Joel, this was exactly what I needed. Please note on MacOS the command brew can be used instead of pip or pip3.

Leave a Reply