Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to load AWS credentials Error when accessing dynamoDB (local) with java

I have installed the local version of dynamoDB, and set up a maven java project to access the DB. When i run the code i get the below error. Since i have installed the server in local (it runs son localhost:8000), i dont have any credentials to provide... Any idea how to solve it?

import java.util.Iterator;

import org.apache.commons.cli.ParseException;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import com.amazonaws.services.dynamodbv2.document.Table;
import com.amazonaws.services.dynamodbv2.document.TableCollection;
import com.amazonaws.services.dynamodbv2.exceptions.DynamoDBLocalServiceException;
import com.amazonaws.services.dynamodbv2.local.embedded.DynamoDBEmbedded;
import com.amazonaws.services.dynamodbv2.local.main.ServerRunner;
import com.amazonaws.services.dynamodbv2.local.server.DynamoDBProxyServer;
import com.amazonaws.services.dynamodbv2.model.ListTablesResult;

public class Test {

    public static void main(String[] args) {

         AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().withEndpointConfiguration(
                    // we can use any region here
                    new AwsClientBuilder.EndpointConfiguration("http://localhost:8000", "us-west-2"))
                    .build();
         DynamoDB dynamoDB = new DynamoDB(client);
         //dynamoDB.listTables();
         TableCollection<ListTablesResult> list = dynamoDB.listTables();

         Iterator<Table> iterator = list.iterator();

            System.out.println("Listing table names");

            while (iterator.hasNext()) {
                Table table = iterator.next();
                System.out.println(table.getTableName());
            }


         System.out.println("over");
    }



}

Error is

Exception in thread "main" com.amazonaws.SdkClientException: Unable to load AWS credentials from any provider in the chain
    at com.amazonaws.auth.AWSCredentialsProviderChain.getCredentials(AWSCredentialsProviderChain.java:131)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.getCredentialsFromContext(AmazonHttpClient.java:1115)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.runBeforeRequestHandlers(AmazonHttpClient.java:764)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:728)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:721)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:704)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.access$500(AmazonHttpClient.java:672)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:654)
    at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:518)
    at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.doInvoke(AmazonDynamoDBClient.java:1831)
    at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.invoke(AmazonDynamoDBClient.java:1807)
    at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.listTables(AmazonDynamoDBClient.java:1123)
    at com.amazonaws.services.dynamodbv2.document.internal.ListTablesCollection.firstPage(ListTablesCollection.java:46)
    at com.amazonaws.services.dynamodbv2.document.internal.PageIterator.next(PageIterator.java:45)
    at com.amazonaws.services.dynamodbv2.document.internal.IteratorSupport.nextResource(IteratorSupport.java:87)
    at com.amazonaws.services.dynamodbv2.document.internal.IteratorSupport.hasNext(IteratorSupport.java:55)
like image 435
rematnarab Avatar asked Oct 12 '17 20:10

rematnarab


People also ask

How do I connect to AWS DynamoDB locally?

To access DynamoDB running locally, use the --endpoint-url parameter. The following is an example of using the AWS CLI to list the tables in DynamoDB on your computer. The AWS CLI can't use the downloadable version of DynamoDB as a default endpoint. Therefore, you must specify --endpoint-url with each AWS CLI command.

Could not connect to repository unable to load AWS credentials from any provider in the chain?

You are getting this exception because your AWS SDK is unable to load your credentials. What you should do is goto Preferences then goto AWS and add your secret key and access key. So that your project can retrieve both keys.

How do I get DynamoDB credentials?

Sign in to the AWS Management Console and open the IAM console at https://console.aws.amazon.com/iam/ . In the navigation pane, choose Users. Choose the name of the user whose access keys you want to create, and then choose the Security credentials tab. In the Access keys section, choose Create access key.

Can you use DynamoDB locally?

DynamoDB Local is available as a download (requires JRE), as an Apache Maven dependency, or as a Docker image. If you prefer to use the Amazon DynamoDB web service instead, see Setting up DynamoDB (web service).


2 Answers

Stumbled upon this when I was searching for the same problem. After half a day of wasting time, managed to solve the issue. Posting here in case anyone ever stumbles upon such situation again.

And the worst part? The solution I had to pierce through and experiment after going through thousands of pages, you would expect there to be some info about the problem. At the least, the documentation should have mentioned some note!

The solution :

Configuring AWS Credentials : go through that to set up some credential. Configure it as any random thing, it doesn't really matter.

Yeah, this was it!!


And for those ppl who are still lazy (like me ;-) ) to go through that, just follow the easiest of the methods :

  1. Open the default config file : ~/.aws/credentials
  2. Change the values in it to anything (like empty string here)

    [default]
    aws_access_key_id=''
    aws_secret_access_key=''
    
  3. Run the program. You can thank me later :D

like image 120
Kaushik NP Avatar answered Nov 15 '22 17:11

Kaushik NP


I had a similar issue. To get around when running tests locally, I have added few lines to set java System properties.

System.setProperty(ACCESS_KEY_SYSTEM_PROPERTY, "accesskey"); System.setProperty(SECRET_KEY_SYSTEM_PROPERTY, "secretkey");

like image 41
aborskiy Avatar answered Nov 15 '22 18:11

aborskiy