Creating an Amazon EC2 instance - Amazon SDK for JavaScript
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China.

The Amazon SDK for JavaScript V3 API Reference Guide describes in detail all the API operations for the Amazon SDK for JavaScript version 3 (V3).

Starting October 1, 2022, Amazon SDK for JavaScript (v3) will end support for Internet Explorer 11 (IE 11). For details, see Announcing the end of support for Internet Explorer 11 in the Amazon SDK for JavaScript (v3).

Creating an Amazon EC2 instance

JavaScript code example that applies to Node.js execution

This Node.js code example shows:

  • How to create an Amazon EC2 instance from a public Amazon Machine Image (AMI).

  • How to create and assign tags to the new Amazon EC2 instance.

About the example

In this example, you use a Node.js module to create an Amazon EC2 instance and assign both a key pair and tags to it. The code uses the SDK for JavaScript to create and tag an instance by using these methods of the Amazon EC2 client class:

Prerequisite tasks

To set up and run this example, first complete these tasks.


These examples use ECMAScript6 (ES6). This requires Node.js version 13.x or higher. To download and install the latest version of Node.js, see Node.js downloads..

However, if you prefer to use CommonJS sytax, please refer to JavaScript ES6/CommonJS syntax

Creating and tagging an instance

Create a libs directory, and create a Node.js module with the file name ec2Client.js. Copy and paste the code below into it, which creates the Amazon EC2 client object. Replace REGION with your Amazon Region.

const { EC2Client } = require( "@aws-sdk/client-ec2"); // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create anAmazon EC2 service client object. const ec2Client = new EC2Client({ region: REGION }); module.exports = { ec2Client };

This example code can be found here on GitHub.

Create a Node.js module with the file name ec2_createinstances.js. Be sure to configure the SDK as previously shown, including installing the required clients and packages.

Create an object to pass the parameters for the RunInstancesCommand method of the EC2 client class, including the name of the key pair to assign and the ID of the AMI to run. To call the RunInstancesCommand method, create an asynchronous function for invoking an Amazon EC2 client service object, passing the parameters.

The code next adds a Name tag to a new instance, which the Amazon EC2 console recognizes and displays in the Name field of the instance list. You can add up to 50 tags to an instance, all of which can be added in a single call to the CreateTagsCommand method.


Replace AMI_ID with the ID of the Amazon Machine Image (AMI) to run, and KEY_PAIR_NAME of the key pair to assign to the AMI ID.

// Import required AWS SDK clients and commands for Node.js const { CreateTagsCommand, RunInstancesCommand, } = require("@aws-sdk/client-ec2"); import { ec2Client } from "./libs/ec2Client"; // Set the parameters const instanceParams = { ImageId: "AMI_ID", //AMI_ID InstanceType: "t2.micro", KeyName: "KEY_PAIR_NAME", //KEY_PAIR_NAME MinCount: 1, MaxCount: 1, }; const run = async () => { try { const data = await ec2Client.send(new RunInstancesCommand(instanceParams)); console.log(data.Instances[0].InstanceId); const instanceId = data.Instances[0].InstanceId; console.log("Created instance", instanceId); // Add tags to the instance const tagParams = { Resources: [instanceId], Tags: [ { Key: "Name", Value: "SDK Sample", }, ], }; try { await ec2Client.send(new CreateTagsCommand(tagParams)); console.log("Instance tagged"); } catch (err) { console.log("Error", err); } } catch (err) { console.log("Error", err); } }; run();

To run the example, enter the following at the command prompt.

node ec2_createinstances.js

This example code can be found here on GitHub.