1. Introduction
When using the AWS SDK to connect programmatically to an AWS service, a common issue related to the region configuration of AWS resources might arise. The reason for such an issue is that the default region isn’t set at the CLI level or in the client builder.
We’ll learn how to fix it for an Amazon S3 resource and which are the alternatives to do it.
2. Prerequisites
To use AWS SDK, we’ll need a few things:
- AWS Account: We need an Amazon Web Services account. If we don’t have one, we can create an account.
- AWS Security Credentials: Our access keys allow us to make programmatic calls to AWS API actions. We can obtain the AWS root account credentials from the Security Credentials page or the IAM console.
- Install and set up the AWS CLI locally by observing the official documentation. AWS CLI is necessary because in this tutorial we’ll be using it. When configuring the AWS CLI we’ll not set the AWS Region.
3. Maven Dependencies
Let’s first declare in our project the AWS Java SDK Core dependency:
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-core</artifactId>
<version>1.12.777</version>
</dependency>
Alongside with the AWS Java SDK S3 dependency:
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.12.777</version>
</dependency>
4. Amazon S3 Bucket Operations
Let’s proceed with creating the Amazon S3 bucket and observe what happens.
4.1. Initialize Amazon S3 Client
First, we have to initialize the Amazon S3 client and create the connection to the AWS infrastructure:
AWSCredentialsProvider credentialsProvider = new ProfileCredentialsProvider();
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(credentialsProvider.getCredentials()))
.build();
The ProfileCredentialsProvider uses the default profile credentials defined when setting up the AWS CLI.
4.2. Create S3 Bucket
Let’s create the Amazon S3 bucket and follow some steps and best practices.
Amazon S3 bucket names must be unique across all AWS accounts in all the AWS regions within a partition. We’ll ensure this programmatically.
By following a few other naming rules, let’s create the S3 bucket:
public CreateS3Bucket() {
AWSCredentialsProvider credentialsProvider = new ProfileCredentialsProvider();
this.s3Client = AmazonS3ClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(credentialsProvider.getCredentials()))
.build();
}
public void createBucket(String bucketName) throws SdkClientException, AmazonServiceException {
try {
if (!this.s3Client.doesBucketExistV2(bucketName)) {
this.s3Client.createBucket(bucketName);
}
String bucketRegion = this.s3Client.getBucketLocation(bucketName);
log.info(bucketRegion);
} catch (AmazonServiceException e) {
throw new AmazonServiceException(e.getErrorMessage());
} catch (SdkClientException e) {
throw new SdkClientException(e.getMessage());
}
}
Executing the method createBucket(String bucketName) we observe an error throw in completing this action:
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.
Amazon S3 buckets are region-specific, which is why we see that error message.
4.3. Fix the Configuration
Let’s now see what techniques we can use to solve this error.
Any explicit region set by using region on the builder itself takes precedence over anything else.
The first approach to solve the issue is to use the builder method withRegion() when initializing the Amazon S3 client:
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(credentialsProvider.getCredentials()))
.withRegion(Regions.EU_CENTRAL_1)
.build();
By setting the region on the builder, we have the advantage of being able to override the CLI configuration and create the resource in any region of choice programmatically.
The second option in the builder’s order of looking for an AWS region is searching for the environment variable AWS_DEFAULT_REGION. If it’s set, that region is used to configure the client.
Let’s set up a default region name in our AWS CLI configuration:
~> aws configure
AWS Access Key ID [None]: <AWS accesskey>
AWS Secret Access Key [None]: <AWS secretkey>
Default region name [None]: eu-central-1
Default output format [None]: json
Setting AWS_DEFAULT_REGION in the CLI configuration avoids such issues if we don’t want a programmatic approach.
Now after we’ve fixed the issue, let’s execute again the method createBucket(String bucketName), and observe that no error arises and the region of the created bucket is printed in the console correctly:
eu-central-1
5. Conclusion
In this article, we focused on solving the region error when creating an Amazon S3 Bucket, both programmatically and at the CLI configuration level.
As always, the example code is available over on GitHub.