Java: 20.0
Springboot: 3.0.1
Dependency
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>aws-sdk-java</artifactId>
<version>2.20.115</version>
<scope>provided</scope>
</dependency>
Service class
@Slf4j
@Service
public class DynamodbClient {
private final DynamoDbClient dynamoDbClient;
@Value("${amazon.aws.dynamodb.endpoint}")
private String endpoint;
@Value("${amazon.aws.dynamodb.region}")
private String region;
public DynamodbClient() {
this.dynamoDbClient =
DynamoDbClient.builder()
.endpointOverride(URI.create(endpoint))
.region(Region.of(region))
.build();
}
}
Note: Auth credential is not required. dynamodb is accessible through cli.
Context: Main database is Cassandra for this application and actual requirement should be implemented through an API call, but for some reason, we are not doing it, instead updating a record in dynamodb directly and it is once a while operation.
The correct way of doing it is
@Configuration
public class DynamodbClient {
@Value("${amazon.aws.dynamodb.endpoint}")
private String endpoint;
@Value("${amazon.aws.dynamodb.region}")
private String region;
@Bean
public DynamodbClient dynamodbClient {
return
DynamoDbClient.builder()
.endpointOverride(URI.create(endpoint))
.region(Region.of(region))
.build();
}
}
Now we can use it like
@Autowired private DynamodbClient dynamodbClient;
in service classes.