A region is a named set of AWS 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 configuring the SDK for JavaScript so that the SDK accesses the resources 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 region using an environment variable, a shared config
file, or the global configuration object.
When you instantiate a service object, you can specify the region for that resource as part of the client class constructor, as shown here.
var s3 = new AWS.S3({apiVersion: '2006-03-01', region: 'us-west-2'});
To set the region in your JavaScript code, update the AWS.Config
global configuration object as shown here.
AWS.config.update({region: 'us-east-1'});
For more information about current regions and available services in each region, see AWS Regions and Endpoints in the AWS General Reference.
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.
Much like the shared credentials file lets you store credentials for use by the SDK, you can keep your region and other configuration settings in a shared file named config
that is used by SDKs. 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 AWS Command Line Interface User Guide.
The order of precedence for region setting is as follows:
-
If a region is passed to a client class constructor, that region is used. If not, then...
-
If a region is set on the global configuration object, that region is used. If not, then...
-
If the
AWS_REGION
environment variable is a truthy value, that region is used. If not, then... -
If the
AMAZON_REGION
environment variable is a truthy value, that region is used. If not, then... -
If the
AWS_SDK_LOAD_CONFIG
environment variable is set to a truthy value and the shared credentials file (~/.aws/credentials
or the path indicated byAWS_SHARED_CREDENTIALS_FILE
) contains a region for the configured profile, that region is used. If not, then... -
If the
AWS_SDK_LOAD_CONFIG
environment variable is set to a truthy value and the config file (~/.aws/config
or the path indicated byAWS_CONFIG_FILE
) contains a region for the configured profile, that region is used.