Menu
AWS Cloud9
User Guide

Node.js Sample for AWS Cloud9

This sample enables you to run some Node.js scripts in an AWS Cloud9 development environment.

Creating this sample might result in charges to your AWS account. These include possible charges for services such as Amazon EC2 and Amazon S3. For more information, see Amazon EC2 Pricing and Amazon S3 Pricing.

Prerequisites

Before you use this sample, be sure to meet the following requirements.

  • You must have an existing AWS Cloud9 development environment. This sample assumes you already have an AWS Cloud9 EC2 development environment that is connected to an Amazon EC2 instance running Amazon Linux. If you have a different type of environment or operating system, you might need to adapt this sample's instructions to set up related tools. See Creating an Environment for details.

  • You have the AWS Cloud IDE for the existing environment already open. When you open an environment, AWS Cloud9 opens the IDE for that environment in your web browser. See Opening an Environment for details.

Step 1: Install Required Tools

In this step, you install Node.js, which is required to run this sample.

  1. In a terminal session in the AWS Cloud9 IDE, confirm whether Node.js is already installed by running the node --version command. (To start a new terminal session, on the menu bar, choose Window, New Terminal.) If successful, the output contains the Node.js version number. If Node.js is installed, skip ahead to Step 2: Add Code.

  2. Run the yum update command to help ensure the latest security updates and bug fixes are installed.

    sudo yum -y update
  3. To install Node.js, begin by running this command to download Node Version Manager (nvm). (nvm is a simple Bash shell script that is useful for installing and managing Node.js versions. For more information, see Node Version Manager on the GitHub website.)

    curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash
  4. To start using nvm, either close the terminal session and start it again, or source the ~/.bashrc file that contains the commands to load nvm.

    . ~/.bashrc
  5. Run this command to install the latest version of Node.js.

    nvm install node

Step 2: Add Code

In the AWS Cloud9 IDE, create a file with this content, and save the file with the name hello.js. (To create a file, on the menu bar, choose File, New File. To save the file, choose File, Save.)

console.log('Hello, World!'); console.log('The sum of 2 and 3 is 5.'); var sum = parseInt(process.argv[2], 10) + parseInt(process.argv[3], 10); console.log('The sum of ' + process.argv[2] + ' and ' + process.argv[3] + ' is ' + sum + '.');

Step 3: Run the Code

  1. In the AWS Cloud9 IDE, on the menu bar, choose Run, Run Configurations, New Run Configuration.

  2. On the [New] - Idle tab, choose Runner: Auto, and then choose Node.js.

  3. For Command, type hello.js 5 9. In the code, 5 represents process.argv[2], and 9 represents process.argv[3]. (process.argv[0] represents the name of the runtime (node), and process.argv[1] represents the name of the file (hello.js).)

  4. Choose the Run button, and compare your output.

    Hello, World! The sum of 2 and 3 is 5. The sum of 5 and 9 is 14.

            Node.js output after running the code in the AWS Cloud9 IDE

Step 4: Install and Configure the AWS SDK for JavaScript

You can enhance this sample to use the AWS SDK for JavaScript to create an Amazon S3 bucket, list your available buckets, and then delete the bucket you just created.

In this step, you install and configure the AWS SDK for JavaScript, which provides a convenient way to interact with AWS services such as Amazon S3, from your JavaScript code. After you install the AWS SDK for JavaScript, you must set up credentials management in your environment. The AWS SDK for JavaScript needs these credentials to interact with AWS services.

To install the AWS SDK for JavaScript

Use npm to run the install command.

npm install aws-sdk

For more information, see Installing the SDK for JavaScript in the AWS SDK for JavaScript Developer Guide.

To set up credentials management in your environment

Each time you use the AWS SDK for JavaScript to call an AWS service, you must provide a set of credentials with the call. These credentials determine whether the AWS SDK for JavaScript has the appropriate permissions to make that call. If the credentials do not cover the appropriate permissions, the call will fail.

In this step, you store your credentials within the environment. To do this, follow the instructions in Call AWS Services from an Environment, and then return to this topic.

For additional information, see Setting Credentials in Node.js in the AWS SDK for JavaScript Developer Guide.

Step 5: Add AWS SDK Code

In this step, you add some more code, this time to interact with Amazon S3 to create a bucket, list your available buckets, and then delete the bucket you just created. You will run this code later.

In the AWS Cloud9 IDE, create a file with this content, and save the file with the name s3.js.

if (process.argv.length < 4) { console.log('Usage: node s3.js <the bucket name> <the AWS Region to use>\n' + 'Example: node s3.js my-test-bucket us-east-2'); process.exit(1); } var AWS = require('aws-sdk'); // To set the AWS credentials and region. var async = require('async'); // To call AWS operations asynchronously. AWS.config.update({ region: region }); var s3 = new AWS.S3({apiVersion: '2006-03-01'}); var bucket_name = process.argv[2]; var region = process.argv[3]; var create_bucket_params = { Bucket: bucket_name, CreateBucketConfiguration: { LocationConstraint: region } }; var delete_bucket_params = {Bucket: bucket_name}; // List all of your available buckets in this AWS Region. function listMyBuckets(callback) { s3.listBuckets(function(err, data) { if (err) { } else { console.log("My buckets now are:\n"); for (var i = 0; i < data.Buckets.length; i++) { console.log(data.Buckets[i].Name); } } callback(err); }); } // Create a bucket in this AWS Region. function createMyBucket(callback) { console.log('\nCreating a bucket named ' + bucket_name + '...\n'); s3.createBucket(create_bucket_params, function(err, data) { if (err) { console.log(err.code + ": " + err.message); } callback(err); }); } // Delete the bucket you just created. function deleteMyBucket(callback) { console.log('\nDeleting the bucket named ' + bucket_name + '...\n'); s3.deleteBucket(delete_bucket_params, function(err, data) { if (err) { console.log(err.code + ": " + err.message); } callback(err); }); } // Call the AWS operations in the following order. async.series([ listMyBuckets, createMyBucket, listMyBuckets, deleteMyBucket, listMyBuckets ]);

Step 6: Run the AWS SDK Code

  1. Enable the code to call Amazon S3 operations asynchronously by using npm to run the install command.

    npm install async
  2. In the AWS Cloud9 IDE, on the menu bar, choose Run, Run Configurations, New Run Configuration.

  3. On the [New] - Idle tab, choose Runner: Auto, and then choose Node.js.

  4. For Command, type s3.js YOUR_BUCKET_NAME THE_AWS_REGION , where YOUR_BUCKET_NAME is the name of the bucket you want to create and then delete, and THE_AWS_REGION is the ID of the AWS Region you want to create the bucket in. For example, for the US East (Ohio) Region, use us-east-2. For more IDs, see Amazon Simple Storage Service (Amazon S3) in the Amazon Web Services General Reference.

    Note

    Amazon S3 bucket names must be unique across AWS—not just your AWS account.

  5. Choose the Run button, and compare your output.

    My buckets now are: Creating a new bucket named 'my-test-bucket'... My buckets now are: my-test-bucket Deleting the bucket named 'my-test-bucket'... My buckets now are:

Step 7: Clean Up

To prevent ongoing charges to your AWS account after you're done using this sample, you should delete the environment. For instructions, see Deleting an Environment.