Setting the Amazon Web Services Region for the Amazon SDK for Ruby - Amazon SDK for Ruby
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).

Setting the Amazon Web Services Region for the Amazon SDK for Ruby

You can access Amazon Web Services services that operate in a specific geographic area by using Amazon Web Services Regions. This can be useful both for redundancy and to keep your data and applications running close to where you and your users access them.

Important

Most resources reside in a specific Amazon Web Services Region and you must supply the correct Region for the resource when using the SDK.

You must set a default Amazon Web Services Region for the SDK for Ruby to use for Amazon requests. This default is used for any SDK service method calls that aren't specified with a Region.

For more information on the region setting, see Amazon Web Services Region in the Amazon SDKs and Tools Reference Guide. This also includes examples on how to set the default region through the shared Amazon config file or environment variables.

Region search order for resolution

You need to set a Region when using most Amazon Web Services services. The Amazon SDK for Ruby searches for a Region in the following order:

  1. Setting the Region in a client or resource object

  2. Setting the Region by using Aws.config

  3. Setting the Region by using environment variables

  4. Setting the Region by using the shared config file

How to set the Region

This section describes different ways to set a Region, starting with the most common approach.

Setting the Region using the shared config file

Set the region by setting the region variable in the shared Amazon config file. For more information about the shared config file, see Shared config and credentials files in the Amazon SDKs and Tools Reference Guide.

Example of setting this value in the config file:

[default] region = us-west-2

The shared config file is not checked if the environment variable AWS_SDK_CONFIG_OPT_OUT is set.

Setting the Region using environment variables

Set the Region by setting the AWS_REGION environment variable.

Use the export command to set this variable on Unix-based systems, such as Linux or macOS. The following example sets the Region to us-west-2.

export AWS_REGION=us-west-2

To set this variable on Windows, use the set command. The following example sets the Region to us-west-2.

set AWS_REGION=us-west-2

Setting the Region with Aws.config

Set the Region by adding a region value to the Aws.config hash. The following example updates the Aws.config hash to use the us-west-1 Region.

Aws.config.update({region: 'us-west-1'})

Any clients or resources that you create later are bound to this Region.

Setting the Region in a client or resource object

Set the Region when you create an Amazon client or resource. The following example creates an Amazon S3 resource object in the us-west-1 Region. Choose the correct Region for your Amazon resources. A service client object is immutable, so you must create a new client for each service to which you make requests and for making requests to the same service using a different configuration.

s3 = Aws::S3::Resource.new(region: 'us-west-1')