How to set aws.access.key and aws.secret.key for inMemory DynamoDBLocal in java?

1.9k views Asked by At

How to set aws.access.key and aws.secret.key for inMemory DynamoDBLocal in java for junit purpose.

1

There are 1 answers

2
notionquest On

Actually, the API doesn't expect the access and secret key when you connect to local DynamoDB instance. You can just set the local endpoint URL and connect to DynamoDB.

Here is the same code.

public static void main(String[] args) {

        AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.standard()
                .withEndpointConfiguration(new EndpointConfiguration("http://localhost:8000", "us-east-1")).build();

        Map<String, AttributeValue> keys = new HashMap<>();

        keys.put("yearkey", new AttributeValue().withN("2016"));
        keys.put("title", new AttributeValue("The Big New Movie 1"));

        GetItemRequest getItemRequest = new GetItemRequest().withTableName("Movies").withKey(keys);

        GetItemResult result = ddb.getItem(getItemRequest);
        System.out.println(result.getItem().toString());

    }

Code with dummy credentials:-

As mentioned above, you don't need to set the credentials for local DynamoDB. In case, if you are looking for samples for some other purpose, you can set the credentials as mentioned below.

BasicAWSCredentials awsCreds = new BasicAWSCredentials("access_key_id", "secret_key_id");
        AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.standard()
                .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
                .withEndpointConfiguration(new EndpointConfiguration("http://localhost:8000", "us-east-1")).build();