Skullruss
Skullruss

Reputation: 83

How to explicitly provide AWS credentials for Amazon Athena Service Client object

The Amazon documentation says to use:

AwsBasicCredentials awsCreds = AwsBasicCredentials.create(
  "your_access_key_id",
  "your_secret_access_key");

S3Client s3 = S3Client.builder()
            .credentialsProvider(StaticCredentialsProvider.create(awsCreds))
            .build();

But that's for the S3Client, when I try to use the same structure for my AthenaClient my app won't even run and it fails to initialize.

For my application I'm grabbing the credentials from a repo with a configuration file for this app.

static @Value("${AWS_ACCESS_KEY_ID}") String amazonAccessId;

static @Value("${AWS_SECRET_ACCESS_KEY}") String amazonSecretKey;

AwsBasicCredentials awsCreds = AwsBasicCredentials.create(
                "your_access_key_id",
                "your_secret_access_key");
        

AthenaClient athenaClient = AthenaClient.builder()
                .region(Region.US_WEST_2)
                .credentialsProvider(StaticCredentialsProvider.create(awsCreds))
                .build();

I want to provide the credentials this way so that when I push the code from my local machine the code still functions, instead of relying on my environment variables. Please tell me what I'm doing wrong, because the documentation does not.

Upvotes: 3

Views: 4446

Answers (1)

smac2020
smac2020

Reputation: 10704

Understand how credentials work when using the AWS SDK for Java V2 SDK. You can read about them in the AWS Java V2 Developer Guide here:

Using credentials

Specifically, read about the Credential retrieval order in that doc topic.

As far as what service you are using, it does not matter. An S3 Service Client works the same way as other Java Service clients in terms of handling creds.

The topic linked about describes this way to handle creds. As described in this topic, you can use the shared credentials and config files:

The SDK uses the ProfileCredentialsProvider to load credentials from the [default] credentials profile in the shared credentials and config files.

You can create a file named credentials - as shown here:

enter image description here

In that file - specify your keys:

[default] aws_access_key_id=Axxxxxxxxxxxxxxxxxxxxxxxxxxx aws_secret_access_key=Kxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Then you can use this code to create a service client:

  AthenaClient athenaClient = AthenaClient.builder()
            .region(Region.US_WEST_2)
            .credentialsProvider(ProfileCredentialsProvider.create())
            .build();

And also - ensure you are using the correct POM in your Java project that uses Amazon Athena. See:

https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javav2/example_code/athena

Upvotes: 0

Related Questions