Deploying an Express application to Elastic Beanstalk - AWS Elastic Beanstalk

Deploying an Express application to Elastic Beanstalk

This section walks you through deploying a sample application to Elastic Beanstalk using Elastic Beanstalk Command Line Interface (EB CLI) and Git, and then updating the application to use the Express framework.


This tutorial requires the Node.js language, its package manager called npm, and the Express web application framework. For details on installing these components and setting up your local development environment, see Setting up your Node.js development environment.


For this tutorial, you don't need to install the AWS SDK for Node.js, which is also mentioned in Setting up your Node.js development environment.

The tutorial also requires the Elastic Beanstalk Command Line Interface (EB CLI). For details on installing and configuring the EB CLI, see Install the EB CLI and Configure the EB CLI.

Initialize Git

The prerequisite Node.js and Express setup results in an Express project structure in the node-express folder. If you haven't already generated an Express project, run the following command. For more details, see Install Express.

~/node-express$ express && npm install

Now let's set up a Git repository in this folder.

To set up a Git repository

  1. Initialize the Git repository. If you don't have Git installed, download it from the Git downloads site.

    ~/node-express$ git init
  2. Create a file named .gitignore and add the following files and directories to it. These files will be excluded from being added to the repository. This step is not required, but it is recommended.


    node_modules/ .gitignore .elasticbeanstalk/

Create an Elastic Beanstalk environment

Configure an EB CLI repository for your application and create an Elastic Beanstalk environment running the Node.js platform.

  1. Create a repository with the eb init command.

    ~/node-express$ eb init --platform node.js --region us-east-2 Application node-express has been created.

    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.

  2. Create an environment running a sample application with the eb create command.

    ~/node-express$ eb create --sample node-express-env

    This command creates a load-balanced environment with the default settings for the Node.js platform and the following resources:

    • EC2 instance – An Amazon Elastic Compute Cloud (Amazon EC2) virtual machine configured to run web apps on the platform that you choose.

      Each platform runs a specific set of software, configuration files, and scripts to support a specific language version, framework, web container, or combination of these. Most platforms use either Apache or NGINX as a reverse proxy that sits in front of your web app, forwards requests to it, serves static assets, and generates access and error logs.


      The Let's Encrypt cross-signed DST Root CA X3 certificate expired on September 30, 2021. Due to this, Beanstalk environments running on the Amazon Linux 2 and Amazon Linux AMI operating systems might not be able to connect to servers using Let's Encrypt certificates.

      On October 3, 2021 Elastic Beanstalk released new platform versions for Amazon Linux AMI and Amazon Linux 2 with the updated CA certificates. To receive these updates and address this issue turn on Managed Updates or update your platforms manually. For more information, see the platform update release notes in the AWS Elastic Beanstalk Release Notes.

      You can also apply the manual workarounds described in this AWS Knowledge Center article. Since Elastic Beanstalk provides AMIs with locked GUIDs, we recommend that you use the sudo yum install command in the article. Alternatively, you can also use the sudo sed command in the article if you prefer to manually modify the system in place.

    • Instance security group – An Amazon EC2 security group configured to allow inbound traffic on port 80. This resource lets HTTP traffic from the load balancer reach the EC2 instance running your web app. By default, traffic isn't allowed on other ports.

    • Load balancer – An Elastic Load Balancing load balancer configured to distribute requests to the instances running your application. A load balancer also eliminates the need to expose your instances directly to the internet.

    • Load balancer security group – An Amazon EC2 security group configured to allow inbound traffic on port 80. This resource lets HTTP traffic from the internet reach the load balancer. By default, traffic isn't allowed on other ports.

    • Auto Scaling group – An Auto Scaling group configured to replace an instance if it is terminated or becomes unavailable.

    • Amazon S3 bucket – A storage location for your source code, logs, and other artifacts that are created when you use Elastic Beanstalk.

    • Amazon CloudWatch alarms – Two CloudWatch alarms that monitor the load on the instances in your environment and that are triggered if the load is too high or too low. When an alarm is triggered, your Auto Scaling group scales up or down in response.

    • AWS CloudFormation stack – Elastic Beanstalk uses AWS CloudFormation to launch the resources in your environment and propagate configuration changes. The resources are defined in a template that you can view in the AWS CloudFormation console.

    • Domain name – A domain name that routes to your web app in the form

  3. When environment creation completes, use the eb open command to open the environment's URL in the default browser.

    ~/node-express$ eb open

Update the application

After you have created an environment with a sample application, you can update it with your own application. In this step, we update the sample application to use the Express framework.

To update your application to use Express

  1. On your local computer, create an .ebextensions directory in the top-level directory of your source bundle. In this example, we use node-express/.ebextensions.

  2. Add a configuration file that sets the Node Command to "npm start":


    option_settings: aws:elasticbeanstalk:container:nodejs: NodeCommand: "npm start"

    For more information, see Advanced environment customization with configuration files (.ebextensions).

  3. Stage the files:

    ~/node-express$ git add . ~/node-express$ git commit -m "First express app"
  4. Deploy the changes:

    ~/node-express$ eb deploy
  5. Once the environment is green and ready, refresh the URL to verify it worked. You should see a web page that says Welcome to Express.

Next, let's update the Express application to serve static files and add a new page.

To configure static files and add a new page to your Express application

  1. Add a second configuration file with the following content:


    option_settings: aws:elasticbeanstalk:container:nodejs:staticfiles: /public: /public

    This setting configures the proxy server to serve files in the public folder at the /public path of the application. Serving files statically from the proxy reduces the load on your application.

  2. Comment out the static mapping in node-express/app.js. This step is not required, but it is a good test to confirm that static mappings are configured correctly.

    // app.use(express.static(path.join(__dirname, 'public')));
  3. Add your updated files to your local repository and commit your changes.

    ~/node-express$ git add .ebextensions/ app.js ~/node-express$ git commit -m "Serve stylesheets statically with nginx."
  4. Add node-express/routes/hike.js. Type the following:

    exports.index = function(req, res) { res.render('hike', {title: 'My Hiking Log'}); }; exports.add_hike = function(req, res) { };
  5. Update node-express/app.js to include three new lines.

    First, add the following line to add a require for this route:

    var hike = require('./routes/hike');

    Your file should look similar to the following snippet:

    var express = require('express'); var path = require('path'); var hike = require('./routes/hike');

    Then, add the following two lines to node-express/app.js after var app = express();

    app.get('/hikes', hike.index);'/add_hike', hike.add_hike);

    Your file should look similar to the following snippet:

    var app = express(); app.get('/hikes', hike.index);'/add_hike', hike.add_hike);
  6. Copy node-express/views/index.jade to node-express/views/hike.jade.

    ~/node-express$ cp views/index.jade views/hike.jade
  7. Add your files to the local repository, commit your changes, and deploy your updated application.

    ~/node-express$ git add . ~/node-express$ git commit -m "Add hikes route and template." ~/node-express$ eb deploy
  8. Your environment will be updated after a few minutes. After your environment is green and ready, verify it worked by refreshing your browser and appending hikes at the end of the URL (e.g.,

    You should see a web page titled My Hiking Log.

Clean up

If you are done working with Elastic Beanstalk, you can terminate your environment.

Use the eb terminate command to terminate your environment and all of the resources that it contains.

~/node-express$ eb terminate The environment "node-express-env" and all associated instances will be terminated. To confirm, type the environment name: node-express-env INFO: terminateEnvironment is starting. ...