Menu
AWS Elastic Beanstalk
Developer Guide (API Version 2010-12-01)

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.

Step 1: Set Up Your Git Repository

EB CLI is a command line interface that you can use with Git to deploy applications quickly and more easily. EB is available as part of the Elastic Beanstalk command line tools package. For instructions to install EB CLI, see Install the EB Command Line Interface (CLI).

Initialize your Git repository. After you run the following command, when you run eb init, EB CLI will recognize that your application is set up with Git.

git init .

Step 2: 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 http://nodejs.org/. Verify you have a successful installation before proceeding to the next step.

    $ node -v

    Note

    For information about what Node.js versions are supported, see 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$ cd . && 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$ cd . && 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.

    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.

    node-express/.gitignore

    node_modules/
    .gitignore
    .elasticbeanstalk/

Step 3: Configure Elastic Beanstalk

The following instructions use the Elastic Beanstalk command line interface (EB CLI) to configure an Elastic Beanstalk application repository in your local project directory.

To configure Elastic Beanstalk

  1. From the directory where you created your local repository, type the following command:

    eb init
  2. When you are prompted for the Elastic Beanstalk region, type the number of the region. For information about this product's regions, go to Regions and Endpoints in the Amazon Web Services General Reference. For this example, we'll use US West (Oregon).

  3. When you are prompted for the Elastic Beanstalk application to use, type the number corresponding to the option Create new Application. Elastic Beanstalk generates an application name based on the current directory name if an application name has not been previously configured. In this example, we use expressapp.

    Enter an AWS Elastic Beanstalk application name (auto-generated value is "node-express"): expressapp

    Note

    If you have a space in your application name, make sure you do not use quotation marks.

  4. Type y if Elastic Beanstalk correctly detected the correct platform you are using. Type n if not, and then specify the correct platform.

  5. When prompted, type y if you want to set up Secure Shell (SSH) to connect to your instances. Type n if you do not want to set up SSH. In this example, we will type n.

    Do you want to set up SSH for your instances?
    (y/n): n
  6. Create your running environment.

    eb create
  7. When you are prompted for the Elastic Beanstalk environment name, type the name of the environment. Elastic Beanstalk automatically creates an environment name based on your application name. If you want to accept the default, press Enter.

    Enter Environment Name
    (default is HelloWorld-env):

    Note

    If you have a space in your application name, make sure you do not have a space in your environment name.

  8. When you are prompted to provide a CNAME prefix, type the CNAME prefix you want to use. Elastic Beanstalk automatically creates a CNAME prefix based on the environment name. If you want to accept the default, press Enter.

    Enter DNS CNAME prefix
    (default is HelloWorld):

EB CLI will display your environment details and the status of the create operation.

To deploy a sample application

  • From the directory where you created your local repository, type the following command:

    eb deploy

    This process may take several minutes to complete. Elastic Beanstalk will provide status updates during the process. If at any time you want to stop polling for status updates, press Ctrl+C. Once the environment status is Green, Elastic Beanstalk will output a URL for the application. You can copy and paste the URL into your web browser to view the application.

Step 4: View the Application

To view the application

  • To open your application in a browser window, type the following:

    eb open

Step 5: Update the Application

After you have deployed 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":

    node-express/.ebextensions/nodecommand.config

    option_settings:
      - namespace: aws:elasticbeanstalk:container:nodejs
        option_name: NodeCommand
        value: "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:

    node-express/.ebextensions/staticfiles.config

    option_settings:
      - namespace: aws:elasticbeanstalk:container:nodejs:staticfiles
        option_name: /public
        value: /public
  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);
    app.post('/add_hike', hike.add_hike);

    Your file should look similar to the following snippet:

    var app = express();
    app.get('/hikes', hike.index);
    app.post('/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., http://node-express-env-syypntcz2q.elasticbeanstalk.com/hikes).

    You should see a web page titled My Hiking Log.

Step 6: Clean Up

If you no longer want to run your application, you can clean up by terminating your environment and deleting your application.

Use the terminate command to terminate your environment and the delete command to delete your application.

To terminate your environment and delete the application

  • From the directory where you created your local repository, run eb terminate:

    $ eb terminate

    This process may take a few minutes. Elastic Beanstalk displays a message once the environment has been successfully terminated.