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.

Note

This sample assumes you already have the AWS Cloud9 IDE for your AWS Cloud9 development environment open in your web browser, and that you're using an AWS Cloud9 EC2 development environment that is connected to an Amazon EC2 instance running Amazon Linux. In you're using a different operating system or using an AWS Cloud9 SSH development environment, you might need to adapt this sample's instructions to correctly install and configure this sample's required tools. To create an environment, see Creating an Environment.

When you're using this sample, be sure you're signed in to AWS with the AWS account ID and name and password of the user you created or identified in Team Setup.

Creating this sample may 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.

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.

    Copy
    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.)

    Copy
    curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash
  4. Use a text editor to update your ~/.bashrc file to enable nvm to be loaded. Type or paste the following code at the end of the file, and then save the file.

    Copy
    export NVM_DIR="/home/ec2-user/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm.
  5. Source the ~/.bashrc file you just saved.

    Copy
    . ~/.bashrc
  6. Run this command to install Node.js, for example, version 6.9.5.

    Copy
    nvm install 6.9.5

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.)

Copy
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.

    Copy
    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.

Copy
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.

Copy
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: 'YOUR_REGION' }); var s3 = new AWS.S3({apiVersion: '2006-03-01'}); var bucket_name = process.argv[2]; var 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(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(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 ]);

In the preceding code, replace YOUR_REGION with the ID of an AWS Region. 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.

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.

    Copy
    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 , where YOUR-BUCKET-NAME is the name of the bucket you want to create and then delete.

    
                  Running the AWS SDK for Node.js code in the AWS Cloud9 IDE
  5. Choose the Run button, and compare your output.

    Copy
    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.