[Recipes] Setting Up Project and Environment For DynamoDB Java Spring Project

Problem: 

Set up a basic Java Spring project for working with DynamoDB.

Solution Summary: 

I will be using Spring Boot, however, I won’t be using any Spring Boot specific started dependencies for AWS. There will be a separate note on using spring-cloud-starter-aws starter dependency of Spring Boot.

Prerequisites: 

This is the beginning of a new lab. Though having done the previous labs are an advantage, it is not a requirement.

Solution Steps: 

Context class

I have created a separate context configuration class for DynamoDB in a package called context under the main class package:

package com.buddytutor.aws.dynamodb.context;

@Configuration

public class AWSDynamoDBContext {

 

In case of a non-Spring boot application, this context configuration class needs to be imported to the main Spring config file (@Configuration).

 

I have added the below dependency to maven file:

<dependency>

    <groupId>com.amazonaws</groupId>

    <artifactId>aws-java-sdk-dynamodb</artifactId>

    <version>1.11.98</version>

</dependency>

Note: You may use the latest version available in the maven repository at https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-dynamodb.

 

I will create two DynamoDB client beans inside AWSDynamoDBContext: one for cloud and one for local. While you have to only specify the region for the cloud, you need to specify the local endpoint for local. I will make the local version as Primary.

// For cloud.

@Bean

    public AmazonDynamoDB amazonDynamoDB() {

        final AWSCredentials credentials = new BasicAWSCredentials(

                awsAccessKeyId,

                awsSecretAccessKey);

        return AmazonDynamoDBClientBuilder.standard()

                .withClientConfiguration(PredefinedClientConfigurations.dynamoDefault())

                .withRegion(awsDynamoDBRegion)

                .withCredentials(new AWSStaticCredentialsProvider(credentials))

                .build();

    }

 

// For Local.

    @Primary

    @Bean

    public AmazonDynamoDB amazonDynamoDBLocal() {

        final AWSCredentials credentials = new BasicAWSCredentials(

                awsAccessKeyId,

                awsSecretAccessKey);

        return AmazonDynamoDBClientBuilder.standard()

                .withClientConfiguration(PredefinedClientConfigurations.dynamoDefault())

                .withEndpointConfiguration(

                        new AwsClientBuilder.EndpointConfiguration(

                                awsDynamoDBEndpoint,

                                awsDynamoDBRegion))

                .withCredentials(new AWSStaticCredentialsProvider(credentials))

                .build();

    }

Note: You could also specify endpoint in cloud, but is not recommended as it is already picked up based on the endpoint.

Learn Serverless from Serverless Programming Cookbook

Contact

Please first use the contact form or facebook page messaging to connect.

Offline Contact
We currently connect locally for discussions and sessions at Bangalore, India. Please follow us on our facebook page for details.
WhatsApp (Primary): (+91) 7411174113
Phone (Escalations): (+91) 7411174114

Business newsletter

Complete the form below, and we'll send you an e-mail every now and again with all the latest news.

About

CloudMaterials is my blog to share notes and learning materials on Cloud and Data Analytics. My current focus is on Microsoft Azure and Amazon Web Services (AWS).

I like to write and I try to document what I learn to share with others. I believe that knowledge is useless unless you share it; the more you share, the more you learn.

Recent comments

Photo Stream