Setting the AWS region programmatically
amazon-web-services, aws-sdk, java
Solution
I have a similar scenario, we are building an AWS abstraction layer so programmers don't have to touch any AWS Code. And I were also having problems with unit testing and tried to set the variable `AWS_REGION` with `System.setProperty(String, String)`.
The solution I've found is to set the property aws.region instead. The class AwsSystemPropertyRegionProvider it is on the "region provider chain" and will get the value from this property.
I'm setting the property before my tests, in `@BeforeClass`:
@BeforeClass
public static void setUp() {
System.setProperty("aws.region", "us-west-2");
}
Hope it helps.
Problem
I use aws-java-sdk version 1.11.104. According to the AWS credentials doc the default region is `us-east-1`, however when I don't set the region manually when I create a client, like this: ``` AWSCredentialsProvider awsCredentialsProvider = new AWSStaticCredentialsProvider(new BasicAWSCredentials(awsAccessKey, awsSecretKey)); AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withCredentials(awsCredentialsProvider).build(); ``` I get this error: ``` com.amazonaws.SdkClientException: Unable to find a region via the region provider chain. Must provide an explicit region in the builder or setup environment to supply a region. ``` Why isn't the default region used? I tried to add the following before my code above but it still doesn't work. ``` System.setProperty(SDKGlobalConfiguration.AWS_REGION_ENV_VAR, "us-east-1"); ``` How to set the AWS region programmatically? (I would like to set it at runtime for all classes of my project). Thanks. Edit: I know I can use `.withRegion()` on the clients' builder, but I was expecting a default region, or, the region picked from an environment variable through the default region provider chain.