Using Regions and Availability Zones with Amazon EC2 - 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).

Using Regions and Availability Zones with Amazon EC2

JavaScript code example that applies to Node.js execution

This Node.js code example shows:

  • How to retrieve descriptions for Amazon Regions and Availability Zones.

The scenario

Amazon EC2 is hosted in multiple locations worldwide. These locations are composed of Regions and Availability Zones. Each Region is a separate geographic area. Each Region has multiple, isolated locations known as Availability Zones. Amazon EC2 provides the ability to place instances and data in multiple locations.

In this example, you use a series of Node.js modules to retrieve details about Regions and Availability Zones. The Node.js modules use the SDK for JavaScript to manage instances by using the following methods of the Amazon EC2 client class:

For more information about Regions and Availability Zones, see Regions and Availability Zones in the Amazon EC2 User Guide for Linux Instances or Regions and Availability Zones in the Amazon EC2 User Guide for Windows Instances.

Prerequisite tasks

To set up and run this example, you must first complete these tasks:

Important

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

Describing Regions and Availability Zones

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_describeregionsandzones.js. Be sure to configure the SDK as previously shown. Create an empty JSON object to pass as parameters, which returns all available descriptions. Then call the DescribeRegionsCommand and DescribeAvailabilityZonesCommand methods.

// Import required AWS SDK clients and commands for Node.js import { DescribeRegionsCommand } from "@aws-sdk/client-ec2"; import { ec2Client } from "./libs/ec2Client"; const run = async () => { try { const data = await ec2Client.send(new DescribeRegionsCommand({})); console.log("Availability Zones: ", data.Regions); return data; } catch (err) { console.log("Error", err); } }; run();

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

node ec2_describeregionsandzones.js

This example code can be found here on GitHub.