AWS Local DynamoDB The security token included in the request is invalid
Asked Answered
S

4

13

I am new to AWS and I am trying to perform CRUD operation on Local DynamoDB from a Java program. The Java program is an AWS sample.

I have AWS CLI installed and have set the following configuration - As per AWS documentation, I don't need a real AWS access and secret key for Local DynamoDB.

I have set the following values in in ~/.aws/config and ~/.aws/credentials through running aws configure in the AWS CLI.

[default]
aws_access_key_id = ''
aws_secret_access_key = ''
[default]
region = ap-south-1

I have the Local DYnamoDB JAR running with this.

java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb

And the code I'm trying to run is this.

https://github.com/aws-samples/aws-dynamodb-examples/blob/master/src/main/java/com/amazonaws/codesamples/datamodeling/ObjectPersistenceCRUDExample.java

Howevwer, I'm getting this Exception.

AmazonDynamoDBException: The security token included in the request is invalid.

The complete stack is this.

    Exception in thread "main" com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException: The security token included in the request is invalid. (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: UnrecognizedClientException; Request ID: UPGRD2BRNUN6S1702EN6N6S8RJVV4KQNSO5AEMVJF66Q9ASUAAJG)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleErrorResponse(AmazonHttpClient.java:1695)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeOneRequest(AmazonHttpClient.java:1350)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeHelper(AmazonHttpClient.java:1101)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:758)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:732)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:714)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.access$500(AmazonHttpClient.java:674)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:656)
    at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:520)
    at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.doInvoke(AmazonDynamoDBClient.java:4192)
    at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.invoke(AmazonDynamoDBClient.java:4159)
    at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.executeUpdateItem(AmazonDynamoDBClient.java:3868)
    at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.updateItem(AmazonDynamoDBClient.java:3835)
    at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper$SaveObjectHandler.doUpdateItem(DynamoDBMapper.java:854)
    at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper$2.executeLowLevelRequest(DynamoDBMapper.java:594)
    at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper$SaveObjectHandler.execute(DynamoDBMapper.java:733)
    at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.save(DynamoDBMapper.java:623)
    at com.amazonaws.services.dynamodbv2.datamodeling.AbstractDynamoDBMapper.save(AbstractDynamoDBMapper.java:123)
    at com.stackroute.Main.testCRUDOperations(Main.java:60
    at com.stackroute.Main.main(Main.java:17)

Any help will be highly appreciated. Thanks in Advance.

Stearoptene answered 21/4, 2020 at 15:10 Comment(0)
P
14

Nothing in the cited code points to the DynamoDB local instance from what I can tell. It looks like it is pointing to DynamoDB proper.

You need to change the endpoint to be the local version. Take a look at this page. It has an example of changing the endpoint to localhost:8080.

Pier answered 21/4, 2020 at 20:32 Comment(1)
and check this out: docs.aws.amazon.com/cli/latest/userguide/… for config service-specific endpoint globally on localBevvy
H
3

For those using DynamoDBLocal version greater than 1.23 or 2.0.0 must use ACCESS_KEY_ID value as alphaneumeric i.e characters strictly between [A-Z, a-z] and numbers [0-9].

For ex:

dummy-key-123 -> does not work.

dummyKey123 -> does work.

Please refer this official article here

Hauger answered 11/8, 2023 at 15:7 Comment(0)
F
2

As a update to Martín C's comment, this is how I solved the issue

DynamoDbClient dynamoDbClient =
                DynamoDbClient.builder()
.region(Region.US_EAST_1)
.endpointOverride(URI.create("http://localhost:8000"))
.credentialsProvider(EnvironmentVariableCredentialsProvider.create())
.build();

You can change the port to whichever port your DynamoDB is locally hosted.

Firelock answered 12/4, 2023 at 6:32 Comment(0)
A
1

Precisely, the problem indeed seems to be pointing to the DynamoDB on AWS rather than a local one, although I can't validate this for the original comment because the links are broken now, at least this is what happened to me.

The solution would be to explicitly connect to the local DynamoBD (needed to be installed) This is the code to achieve it:

AmazonDynamoDBClientBuilder.standard().withEndpointConfiguration(
            // we can use any region here
            new AwsClientBuilder.EndpointConfiguration("http://localhost:8000", "us-west-2"))
            .build();

This is the link to the example class mentioned on previous comments that was deleted(you will need to click on Load Diff for the DynamoDBLocalFixture.java class): https://github.com/aws-samples/aws-dynamodb-examples/commit/461b0e85bfad58f4a62c63f38426c650896fb870#diff-b331539c5e8b0398d9bbb85c5b87a4cdfa32efbefb27f68f3402c223b243b178

Ardisardisj answered 4/3, 2021 at 21:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.