AWS Elastic Beanstalk
Developer Guide (API Version 2010-12-01)
AWS services or capabilities described in AWS Documentation may vary by region/location. Click Getting Started with Amazon AWS to see specific differences applicable to the China (Beijing) Region.

Deploying an Express Application to Elastic Beanstalk

This section walks you through deploying a sample application to Elastic Beanstalk using EB CLI and git, and then updating the application to use the Express framework.

Set Up Your Express Development Environment

Set up Express and create the project structure. The following walks you through setting up Express on a Linux operating system.

To set up your Express development environment on your local computer

  1. Install Node.js. For instructions, go to Verify you have a successful installation before proceeding to the next step.

    $ node -v


    For information about the Node.js versions that are currently supported, see Elastic Beanstalk Supported Platforms.

  2. Create a directory for your Express application.

    ~$ mkdir node-express
    ~$ cd node-express
  3. Install npm if you don't already have it installed. Here's one example of how to install npm.

    ~/node-express$ yum install npm
  4. Install Express globally so that you have access to the express command.

    ~/node-express$ npm install -g express-generator
  5. Depending on your operating system, you may need to set your path to run the express command. If you need to set your path, use the output from the previous step when you installed Express. The following is an example.

    ~/node-express$ export:PATH=$PATH:/usr/local/share/npm/bin/express
  6. Run the express command. This generates package.json.

    ~/node-express$ express

    When prompted if you want to continue, type y.

  7. Set up local dependencies.

    ~/node-express$ npm install
  8. Verify it works.

    ~/node-express$ npm start

    You should see output similar to the following:

    > nodejs@0.0.0 start /home/local/user/node-express
    > node ./bin/www

    Press Ctrl+c to stop the server.

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

    ~/node-express$ git init
  10. 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.



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-west-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 different set of software, configuration files, and scripts to support a specific language version, framework, web container, or combination thereof. 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.

    • Instance security group – An Amazon EC2 security group configured to allow ingress on port 80. This resource lets HTTP traffic from the load balancer reach the EC2 instance running your web app. By default, traffic is not 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 ingress on port 80. This resource lets HTTP traffic from the Internet reach the load balancer. By default, traffic is not 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 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. Add a configuration file that sets the Node Command to "npm start":


        NodeCommand: "npm start"

    For more information, see Advanced Environment Customization with Configuration Files (.ebextensions).

  2. Stage the files:

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

    ~/node-express$ eb deploy
  4. 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:


        /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:

    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.