AWS Region Selection - AWS SDK for Java 1.x

We announced the upcoming end-of-support for AWS SDK for Java (v1). We recommend that you migrate to AWS SDK for Java v2. For dates, additional details, and information on how to migrate, please refer to the linked announcement.

AWS Region Selection

Regions enable you to access AWS services that physically reside in a specific geographic area. This can be useful both for redundancy and to keep your data and applications running close to where you and your users will access them.

Checking for Service Availability in a Region

To see if a particular AWS service is available in a region, use the isServiceSupported method on the region that you’d like to use.

Region.getRegion(Regions.US_WEST_2) .isServiceSupported(AmazonDynamoDB.ENDPOINT_PREFIX);

See the Regions class documentation for the regions you can specify, and use the endpoint prefix of the service to query. Each service’s endpoint prefix is defined in the service interface. For example, the DynamoDB endpoint prefix is defined in AmazonDynamoDB.

Choosing a Region

Beginning with version 1.4 of the AWS SDK for Java, you can specify a region name and the SDK will automatically choose an appropriate endpoint for you. To choose the endpoint yourself, see Choosing a Specific Endpoint.

To explicitly set a region, we recommend that you use the Regions enum. This is an enumeration of all publicly available regions. To create a client with a region from the enum, use the following code.

AmazonEC2 ec2 = AmazonEC2ClientBuilder.standard() .withRegion(Regions.US_WEST_2) .build();

If the region you are attempting to use isn’t in the Regions enum, you can set the region using a string that represents the name of the region.

AmazonEC2 ec2 = AmazonEC2ClientBuilder.standard() .withRegion("{region_api_default}") .build();

After you build a client with the builder, it’s immutable and the region cannot be changed. If you are working with multiple AWS Regions for the same service, you should create multiple clients—​one per region.

Choosing a Specific Endpoint

Each AWS client can be configured to use a specific endpoint within a region by calling the withEndpointConfiguration method when creating the client.

For example, to configure the Amazon S3 client to use the Europe (Ireland) Region, use the following code.

AmazonS3 s3 = AmazonS3ClientBuilder.standard() .withEndpointConfiguration(new EndpointConfiguration( "", "eu-west-1")) .withCredentials(CREDENTIALS_PROVIDER) .build();

See Regions and Endpoints for the current list of regions and their corresponding endpoints for all AWS services.

Automatically Determine the Region from the Environment


This section applies only when using a client builder to access AWS services. AWS clients created by using the client constructor will not automatically determine region from the environment and will, instead, use the default SDK region (USEast1).

When running on Amazon EC2 or Lambda, you might want to configure clients to use the same region that your code is running on. This decouples your code from the environment it’s running in and makes it easier to deploy your application to multiple regions for lower latency or redundancy.

You must use client builders to have the SDK automatically detect the region your code is running in.

To use the default credential/region provider chain to determine the region from the environment, use the client builder’s defaultClient method.

AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();

This is the same as using standard followed by build.

AmazonEC2 ec2 = AmazonEC2ClientBuilder.standard() .build();

If you don’t explicitly set a region using the withRegion methods, the SDK consults the default region provider chain to try and determine the region to use.

Default Region Provider Chain

The following is the region lookup process:

  1. Any explicit region set by using withRegion or setRegion on the builder itself takes precedence over anything else.

  2. The AWS_REGION environment variable is checked. If it’s set, that region is used to configure the client.


    This environment variable is set by the Lambda container.

  3. The SDK checks the AWS shared configuration file (usually located at ~/.aws/config). If the region property is present, the SDK uses it.

    • The AWS_CONFIG_FILE environment variable can be used to customize the location of the shared config file.

    • The AWS_PROFILE environment variable or the aws.profile system property can be used to customize the profile that is loaded by the SDK.

  4. The SDK attempts to use the Amazon EC2 instance metadata service to determine the region of the currently running Amazon EC2 instance.

  5. If the SDK still hasn’t found a region by this point, client creation fails with an exception.

When developing AWS applications, a common approach is to use the shared configuration file (described in Using the Default Credential Provider Chain) to set the region for local development, and rely on the default region provider chain to determine the region when running on AWS infrastructure. This greatly simplifies client creation and keeps your application portable.