Skip to content
Advertisement

How to connect to DynamoDB on AWS using a Spring Boot application running on local system?

I’m working on Spring Boot microservice REST based application and backedn database is currently local Dynamodb database. I can make REST calls using Postman to my application running locally and can perform CRUD operation with the local dynamodb database. Now I want to make changes in the configuration so that whenever I make similar CRUD operations on my application running locally the data should be saved/updated on actual DynamoDB running on AWS & not local. Basically, I want to point application to remote DynamoDB endpoint. Is it possible? Is there any sample reference guide available to make such configuration changes?

Currently, this is my configuration class and properties file:-

package com.user.profile.jpa;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
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.datamodeling.DynamoDBMapper;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig;
import com.amazonaws.util.StringUtils;
import org.socialsignin.spring.data.dynamodb.repository.config.EnableDynamoDBRepositories;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

//@Profile("dev")
@Configuration
@EnableDynamoDBRepositories(basePackages = "com.user.profile.jpa")
public class DynamoDbConfig {

    @Value("${amazon.dynamodb.endpoint}")
    private String amazonDynamoDBEndpoint;

    @Value("${amazon.dynamodb.region}")
    private String amazonDynamoDBRegion;

    @Value("${amazon.aws.accesskey}")
    private String accessKey;

    @Value("${amazon.aws.secretkey}")
    private String secretKey;

    @Bean
    public AmazonDynamoDB amazonDynamoDB() {
        AmazonDynamoDB dynamoDB = new AmazonDynamoDBClient(amazonAWSCredentials());

        if (!StringUtils.isNullOrEmpty(amazonDynamoDBEndpoint)) {
            dynamoDB.setEndpoint(amazonDynamoDBEndpoint);
        }

        return dynamoDB;
    }

    @Bean
    public AWSCredentials amazonAWSCredentials() {
        return new BasicAWSCredentials(accessKey, secretKey);
    }
}



amazon.dynamodb.endpoint=http://localhost:8000/
amazon.aws.accesskey=JGDSGJSGS8729224
amazon.aws.secretkey=HJKAHDD83ERWRnHAHDJASD8782rlGb0F1k2Ijg
amazon.dynamodb.region="us-east-1"

spring.main.allow-bean-definition-overriding=true
spring.profiles.active=dev

server.port=8090

Please assist. Thank you

Advertisement

Answer

Just change the endpoint (leave empty for remote) and credentials/keys:

amazon.dynamodb.endpoint=[leave empty if using AWS, or http://localhost:[dynamodb port] if using local ]
amazon.aws.accesskey=[your AWS access key if using AWS or arbitrary text if using local]
amazon.aws.secretkey=[your AWS secret key if using AWS or arbitrary text if using local]

You have some examples here: https://tech.smartling.com/getting-started-with-amazon-dynamodb-and-java-universal-language-850fa1c8a902

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement