aws-sdk-java-v2 icon indicating copy to clipboard operation
aws-sdk-java-v2 copied to clipboard

Unable to load AWS credentials from any provider in the chain

Open poonamtr opened this issue 9 years ago • 5 comments

I have my application running on EC2. Trying to access dynamoDB from my Java application. But for every operation, I get "Unable to load AWS credentials from any provider in the chain".

  • I have added dynamoDB permissions in the IAM role that gets attached to EC2 instance
  • And also configured EC2 to assume that role in trust relationship.

Code:

try {
            AWSCredentialsProvider provider = new DefaultAWSCredentialsProviderChain();
            AWSCredentials credentials = provider.getCredentials();
            if(credentials != null) {
                LOG.info("Credentials Key: " + credentials.getAWSAccessKeyId());
                LOG.info("Credentials secret: " + credentials.getAWSSecretKey());
            }
        } catch (Exception e) {
            LOG.info("Exception in credentials cause:" + e.getCause() +";message: " + e.getMessage() +";stack: " +e.getStackTrace());
        }

The getCredentials() line itself throws an Exception with message: "Unable to load AWS credentials from any provider in the chain". If I do not try to get credentials here and pass it to the DynamoDB client:

dynamoDBClient = builder.withEndpointConfiguration(new 
                AwsClientBuilder.EndpointConfiguration(url, region.getName()))
                .withCredentials(credentialProvider())
                .withClientConfiguration(clientConfig)
                .build();

Then it throws the same exception when I try to do any operation.

I tried setting up the proxy too:

ClientConfiguration clientConfig = new ClientConfiguration();
        clientConfig.setProxyHost("yyy");
        clientConfig.setProxyPort(port);
        clientConfig.setNonProxyHosts("xxx");
        clientConfig.setProtocol(Protocol.HTTP);

and then passing it while creating the client:

dynamoDBClient = builder.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(url, region.getName()))
                .withCredentials(credentialProvider())
                .withClientConfiguration(clientConfig)
                .build();

But no success.

Can someone @here guide me what am I missing here?

poonamtr avatar Oct 02 '17 13:10 poonamtr

Try withDefaultCredentialsProvider.builder().build()

        DynamoDBClient client = DynamoDBClient.builder()
            .region(Region.US_EAST_1)
            .credentialsProvider(DefaultCredentialsProvider.builder().build())
            .build();

It's working fine for me on ec2

MatteCarra avatar Oct 02 '17 17:10 MatteCarra

I think you are not using aws-sdk-java-v2 It looks like https://github.com/aws/aws-sdk-java

MatteCarra avatar Oct 02 '17 18:10 MatteCarra

Hi Matte,

Thanks for response. I am using aws-sdk-java-v2.

But digging some more into this, I realized that I am unable to access the EC2 metadata information from my app running on EC2. Doing curl for the metadata endpoint on the actual EC2 box where my app is running is working fine.

On Mon, Oct 2, 2017 at 10:57 AM, MatteCarra [email protected] wrote:

Try with DefaultCredentialsProvider.builder().build() It's working fine for me on ec2

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/aws/aws-sdk-java-v2/issues/189#issuecomment-333613482, or mute the thread https://github.com/notifications/unsubscribe-auth/AIB8A8CQvo6qeT0urCbPbY_7XFOQ0qKiks5soSPzgaJpZM4PqplL .

poonamtr avatar Oct 02 '17 18:10 poonamtr

Which version of the AWS Java SDK v2 are you using? The code samples you provided definitely look like you are using v1.

spfink avatar Oct 24 '17 23:10 spfink

Please let us know if this is still an issue.

millems avatar Jan 30 '18 23:01 millems