software.amazon.awssdk.services.dynamodb.model.DynamoDbException: The security token included in the request is invalid

I want to use the AWS SKD v2 of DynamoDB which can accepts the table name dynamically and return all the records and I am creating the Rest API for the same. I have to use the access key and secret key to configure the client for DynamoDB. Please note that I am using the direct AWS resource and have not setup any local DynamoDB. When I call the API I am getting the software.amazon.awssdk.services.dynamodb.model.DynamoDbException: The security token included in the request is invalid.

I have also tried to setup using the aws configure with correct AWS access key and secret key along with region but when I to hit the API I am getting the same.

To verify the keys, I have created basic Python script which can retrieve the records from DynamoDB successfully. So keys are valid.

DynamoDBConfig

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
@Configuration
public class DynamoDBConfig {
@Bean
public DynamoDbClient dynamoDbClient() {
return DynamoDbClient.builder()
.region(Region.EU_WEST_2)
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(
"access key",
"secret key"
)))
.build();
}
}
</code>
<code>import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; @Configuration public class DynamoDBConfig { @Bean public DynamoDbClient dynamoDbClient() { return DynamoDbClient.builder() .region(Region.EU_WEST_2) .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create( "access key", "secret key" ))) .build(); } } </code>
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;

@Configuration
public class DynamoDBConfig {

  @Bean
  public DynamoDbClient dynamoDbClient() {
    return DynamoDbClient.builder()
        .region(Region.EU_WEST_2)
        .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(
            "access key",
            "secret key"
        )))
        .build();
  }

}

DynamoDBService

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
import software.amazon.awssdk.services.dynamodb.model.GetItemRequest;
import software.amazon.awssdk.services.dynamodb.model.ScanRequest;
import software.amazon.awssdk.services.dynamodb.model.ScanResponse;
import org.springframework.stereotype.Service;
import java.util.Map;
@Service
public class DynamoDBService {
private final DynamoDbClient dynamoDbClient;
public DynamoDBService(DynamoDbClient dynamoDbClient) {
this.dynamoDbClient = dynamoDbClient;
}
public ScanResponse getAllItems(String tableName) {
ScanRequest scanRequest = ScanRequest.builder()
.tableName(tableName)
.build();
return dynamoDbClient.scan(scanRequest);
}
public Map<String, AttributeValue> getItem(String tableName, String keyFieldName, String keyFieldValue) {
GetItemRequest getItemRequest = GetItemRequest.builder()
.tableName(tableName)
.key(Map.of(keyFieldName, AttributeValue.builder().s(keyFieldValue).build()))
.build();
return dynamoDbClient.getItem(getItemRequest).item();
}
}
</code>
<code>import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import software.amazon.awssdk.services.dynamodb.model.AttributeValue; import software.amazon.awssdk.services.dynamodb.model.GetItemRequest; import software.amazon.awssdk.services.dynamodb.model.ScanRequest; import software.amazon.awssdk.services.dynamodb.model.ScanResponse; import org.springframework.stereotype.Service; import java.util.Map; @Service public class DynamoDBService { private final DynamoDbClient dynamoDbClient; public DynamoDBService(DynamoDbClient dynamoDbClient) { this.dynamoDbClient = dynamoDbClient; } public ScanResponse getAllItems(String tableName) { ScanRequest scanRequest = ScanRequest.builder() .tableName(tableName) .build(); return dynamoDbClient.scan(scanRequest); } public Map<String, AttributeValue> getItem(String tableName, String keyFieldName, String keyFieldValue) { GetItemRequest getItemRequest = GetItemRequest.builder() .tableName(tableName) .key(Map.of(keyFieldName, AttributeValue.builder().s(keyFieldValue).build())) .build(); return dynamoDbClient.getItem(getItemRequest).item(); } } </code>
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
import software.amazon.awssdk.services.dynamodb.model.GetItemRequest;
import software.amazon.awssdk.services.dynamodb.model.ScanRequest;
import software.amazon.awssdk.services.dynamodb.model.ScanResponse;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class DynamoDBService {

  private final DynamoDbClient dynamoDbClient;

  public DynamoDBService(DynamoDbClient dynamoDbClient) {
    this.dynamoDbClient = dynamoDbClient;
  }

  public ScanResponse getAllItems(String tableName) {
    ScanRequest scanRequest = ScanRequest.builder()
        .tableName(tableName)
        .build();
    return dynamoDbClient.scan(scanRequest);
  }

  public Map<String, AttributeValue> getItem(String tableName, String keyFieldName, String keyFieldValue) {
    GetItemRequest getItemRequest = GetItemRequest.builder()
        .tableName(tableName)
        .key(Map.of(keyFieldName, AttributeValue.builder().s(keyFieldValue).build()))
        .build();
    return dynamoDbClient.getItem(getItemRequest).item();
  }

}

pom.xml

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.7</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.abc</groupId>
<artifactId>poc.dynamodb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>poc.dynamodb</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<!-- Spring Boot Starter Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- AWS SDK for DynamoDB -->
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>dynamodb</artifactId>
<version>2.20.26</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
</code>
<code><?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.2.7</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.abc</groupId> <artifactId>poc.dynamodb</artifactId> <version>0.0.1-SNAPSHOT</version> <name>poc.dynamodb</name> <description>Demo project for Spring Boot</description> <properties> <java.version>17</java.version> </properties> <dependencies> <!-- Spring Boot Starter Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- AWS SDK for DynamoDB --> <dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>dynamodb</artifactId> <version>2.20.26</version> </dependency> <dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId> <version>2.6.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> </code>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.7</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.abc</groupId>
    <artifactId>poc.dynamodb</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>poc.dynamodb</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <!-- Spring Boot Starter Web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- AWS SDK for DynamoDB -->
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>dynamodb</artifactId>
            <version>2.20.26</version>
        </dependency>

        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
            <version>2.6.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

1

There is an example of using DynamoDB Java V2 API in a Spring Boot project in AWS Github. Also, when using V2, you do not have to state your keys in the code. There is a order in whick keys are checked. I personally use:

C:Usersscmacdon.awscredentials.

SO if you have your keys in that file, they will be used.

Read and follow this content:

Creating a Spring Boot application that queries Amazon DynamoDB data

you must have to setup your IDE with the AWS plugin like aws toolkit for vscode before running any aws services in local throu the IDE, check your folder ~/.aws/credentials and replace new downloaded file with working non expired values,

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[default]
aws_access_key_id = AWS access key ID goes here
aws_secret_access_key = Secret key goes here
</code>
<code>[default] aws_access_key_id = AWS access key ID goes here aws_secret_access_key = Secret key goes here </code>
[default]
aws_access_key_id = AWS access key ID goes here
aws_secret_access_key = Secret key goes here 

default will be the profile you have to setup with your IDE and aws plugin

as this error specific to only access keys, You have to verify that correct values are getting used, You are having this error due to inactive user credentials, incorrect access keys, or incorrect secret access keys.
You can verify with aws GetCallerIdentity
Your python script have this keys values configured so not having issues with them, but specific java IDE still seems not configured yet

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật