Integrate AWS DynamoDB with Spring Boot

First published here: https://blog.contactsunny.com/tech/integrate-aws-dynamodb-with-spring-boot

Image for post
Image for post

Here is another POC to add to the growing list of POCs on my Github profile. Today, we’ll see how to integrate AWS DynamoDB with a Spring Boot application. This is going to be super simple, thanks to the AWS Java SDK and the Spring Data DynamoDB package. Let’s get started then.

Dependencies

First, as usual, we need to create a Spring Boot project, the dependencies of which look like:

Some of the packages here are used only for logging. So you don’t have to worry about why they’re there.

Configuration

There are two pieces of configuration we need to take care of for the integration to work.

  1. AWS Credentials configuration

For this, we just create a Bean which returns a BasicAWSCredentials object, which accepts the access key and secret key as constructor parameter:

There are other ways in which we can create or provide the AWS SDK with the credentials. But for a POC, this will do just fine.

2. DynamoDB configuration

We again create a Bean for this, which returns an object of class AmazonDynamoDB. We need the DynamoDB endpoint for this. Amazon provides a list of endpoints for each region available. You can get the list here. So depending on which region you’ve created your DB in, you can just copy-paste the endpoint from that page into your properties or YAML file in the project. The code snippet for this configuration looks something like this:

Make sure that the class in which you defined these beans is annotated with the @Configuration annotation. Also, to enable DynamoDB, you need to use the @EnableDynamoDBRepositories annotation, and pass the base package for the repositories that you’re going to create. The class looks like this:

Models (Entities or POJOs)

Once we’re done with the configuration, we have to start defining our models, or entity classes. This is similar to what you’d do with MongoDB models or MySQL models. The only difference, you have different annotations. So, to mark a POJO class as a data model or a DynomoDB entity, you’ll use the @DynamoDBTable annotation, and pass the table name as a parameter, something like this:

In most other DB integrations, you’d annotate the declaration of a variable as attributes. But with DynamoDB, you have to annotate the getter methods to declare a class property as a DynamoDB attribute. So for this particular model, the class looks like this:

As you can see, if you miss the @DynamoDBAutoGeneratedKey annotation for the ID attribute, the system will not create an ID for you, you’ll be responsible for maintaining unique IDs for your objects.

Repositories

This is similar to any other Spring Data repository that you’ve created in the past. I’ll not explain anything here and just give you the code snippet.

Here, AwsService is the model or entity class that we created earlier.

Read and Write to the DB

Now comes the fun part. To start reading data from and writing data to your DynamoDB, you need to get an instance of the DB first. For this, we need to @Autowire the AmazonDynamoDB Bean that we created in our configuration class, and the AwsServiceRepository that we created. After this, we create an object of the class DynamoDBMapper, which will help us create tables on the fly using a CreateTableRequest instance. Let’s see how it’s done:

Once the table is created, we can use the repository instance we have autowired to read and write to the DB:

In the code snippet above, we are:

  1. writing an object to the DB, and logging it to make sure we have the ID auto generated.
  2. fetching that ID into a String variable, and querying the DB for that object using the ID. We’re logging that as well.
  3. fetching all the items in the DB, looping through them, and logging each item to the console.

It’s as simple as that. You can browse your DB from the AWS console as well.

The complete POC is available on my Github profile as a project. Do let me know if you any doubt or if you can improve this POC in any way.

Follow me on Twitter for more Data Science, Machine Learning, and general tech updates. Also, you can follow my personal blog as I post a lot of my tutorials, how-to posts, and machine learning goodness there before Medium.

If you like my posts here on Medium or on my personal blog, and would wish for me to continue doing this work, consider supporting me on Patreon.

Coding, machine learning, reading, sleeping, listening, potato. https://blog.contactsunny.com and https://www.linkedin.com/in/sunnysrinidhi/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store