Setting the Amazon Region - 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 (PDF).

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

Setting the Amazon Region

An Amazon Region is a named set of Amazon resources in the same geographical area. An example of a Region is us-east-1, which is the US East (N. Virginia) Region. You specify a Region when creating a service client in the SDK for JavaScript so that the SDK accesses the service in that Region. Some services are available only in specific Regions.

The SDK for JavaScript doesn't select a Region by default. However, you can set the Amazon Region using an environment variable, or a shared configuration config file.

In a client class constructor

When you instantiate a service object, you can specify the Amazon Region for that resource as part of the client class constructor, as shown here.

const s3Client = new S3.S3Client({region: 'us-west-2'});

Using an environment variable

You can set the Region using the AWS_REGION environment variable. If you define this variable, the SDK for JavaScript reads it and uses it.

Using a shared config file

Much like the shared credentials file lets you store credentials for use by the SDK, you can keep your Amazon Region and other configuration settings in a shared file named config for the SDK to use. If the AWS_SDK_LOAD_CONFIG environment variable is set to a truthy value, the SDK for JavaScript automatically searches for a config file when it loads. Where you save the config file depends on your operating system:

  • Linux, macOS, or Unix users - ~/.aws/config

  • Windows users - C:\Users\USER_NAME\.aws\config

If you don't already have a shared config file, you can create one in the designated directory. In the following example, the config file sets both the Region and the output format.

[default] region=us-west-2 output=json

For more information about using shared config and credentials files, see Loading credentials in Node.js from the shared credentials file or Configuration and credential files in the Amazon Command Line Interface User Guide.

Order of precedence for setting the Region

The following is the order of precedence for Region setting:

  1. If a Region is passed to a client class constructor, that Region is used.

  2. If a Region is set in the environment variable, that Region is used.

  3. Otherwise, if the AMAZON_REGION environment variable is a truthy value, that Region is used.

  4. Otherwise, the Region defined in the shared config file is used.