Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Must provide an explicit region in the builder or setup environment to supply a region

I tried setting up a user like this:

aws configure --profile MyUser

and activate it via:

export AWS_PROFILE=MyUser

However, I keep getting the following exception:

May 20, 2018 3:09:02 PM com.amazonaws.auth.profile.internal.BasicProfileConfigLoader loadProfiles

WARNING: The legacy profile format requires the 'profile ' prefix before the profile name. The latest code does not require such prefix, and will consider it as part of the profile name. Please remove the prefix if you are seeing this warning.

Exception in thread "main" 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.

It is strange that if I configure it with the default user, then everything is working fine:

aws configure

Why isn't configuration with a specific username doesn't work above?


I also know that we can configure the credential in code like this SO or this SO.

AmazonS3 amazonS3 = AmazonS3Client.builder()
    .withRegion("us-east-1")
    .withCredentials(new AWSStaticCredentialsProvider(creds))
    .build();

That works, but I just think it is a bad idea to put credentials in source code and check them in with git.


Also from the error message, it looks like I am using an old format with profile in the prefix name (as described in this SO. I've double checked the aws-cli version and make sure that it is up-to-date also double checked that I don't have the prefix in the profile name.

Here is about the version:

aws --version
aws-cli/1.11.129 Python/3.6.2 Darwin/17.5.0 botocore/1.5.92

And here is the configuration file:

cat ~/.aws/credentials             
[default]
aws_access_key_id = A***
aws_secret_access_key = I***
[MyUser]
aws_access_key_id = A***
aws_secret_access_key = D***
like image 511
Yuchen Avatar asked Dec 24 '22 07:12

Yuchen


1 Answers

Just figure out what's wrong. There are two files generated with aws configure --profile MyUser.

  • ~/.aws/config
  • ~/.aws/credential

I notice that the generated config file does have profile in the prefix

$ cat ~/.aws/config 
[default]
region = us-east-1
output = json
[profile MyUser]
region = us-west-1
output = json

After removing that, things work perfectly:

[MyUser]
region = us-west-1
output = json

Alternatively, we can specify the credential configuration file and the user explicitly. As an example, in Scala:

private lazy val credential =
    new ProfileCredentialsProvider("/Users/yuchen/.aws/credentials", "MyUser")

private lazy val lambda = AWSLambdaClientBuilder.standard()
    .withCredentials(credential)
    .withRegion(Regions.US_WEST_1)
    .build()
like image 197
Yuchen Avatar answered Feb 08 '23 23:02

Yuchen