Create a spring-boot application with DynamoDB and deploy it to EC2 instance with docker

Harsha Siriwardana
4 min readFeb 4, 2020
Photo by Guillaume Bolduc on Unsplash

Today I am building a spring-boot application to save and read data with dynamoDB. Also gonna talk about how to deploy it to EC2 instance with docker.

Previously I wrote an article on how to create a notification system with AWS resources. This REST API is used to inserting data into DynamoDB as mentioned in that article.

Let’s start.

Spring-boot application

I am not going to explain the Spring boot code in depth. because it is self-explanatory and you can find a lot of articles on how to create spring boot applications with dynamoDB.

Dependency

<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-dynamodb</artifactId>
<version>1.11.573</version>
</dependency>

<dependency>
<groupId>com.github.derjust</groupId>
<artifactId>spring-data-dynamodb</artifactId>
<version>5.1.0</version>
</dependency>

Create a DynamoDB client

Configurations are passed as an application.property file like below.

amazon.dynamodb.endpoint=dynamodb.us-east-1.amazonaws.com
amazon.dynamodb.region=us-east-1

Keep in mind,

When you create an AmazonDynamoDB client, for locally testing use client builder with AWS access key and security key. but when deploying create a client without access key and try to achieve permissions using AWS roles. it is a more secure way of handling the permissions.

Create a Model class

Fields should be annotated with proper Dynamodb annotations.

Controller and Repository

Check the full code in Github.

once you up the service, you can use postman to check whether the REST application is working as expected.

Deployment

We need to deploy this spring-boot application and test it. We use docker as a packaging mechanism before deploying it to AWS EC2.

First, we need to create a docker image from the source. For that, we need a file called Dockerfile. Here is the sample docker file we use.

FROM openjdk:8
EXPOSE 9000
ADD /target/restapp-0.0.1-SNAPSHOT.jar restapp.jar
ENTRYPOINT [ "java", "-jar", "restapp.jar"]

What is the meaning of these commands in Dockerfile? Let’s explore.

  • FROM — Must be the first non-comment instruction in the Dockerfile. This command creates a layer from the Docker image. In our case, we have used openjdk:8 which means this application will run on Java.
  • EXPOSE — Exposing the port for the endpoint. In this example, we have configured 9000.
  • ADD — This command helps to take a source and destination. Normally, the source is your local copy. The COPY command also does same thing, but there is a small difference between the COPY and ADD commands.
  • ENTRYPOINT — It’s similar to CMD, where our command/jar file will be executed.

Build docker image

You only need docker installed in your local machine in order to build the docker image.

docker build -f Dockerfile -t dockerdemo .

Once you build the image, you can check docker images with the following command.

docker images

Let’s check the build image by running it.

docker run -p 80:9000 dockerdemo

You can see the running containers by executing this command.

docker ps

In order to access the docker image in EC2 instance, we need to publish it to commonplace. Docker hub is a place where we can share our docker images.

First, you need to create an account in docker hub and log in to docker hub

docker login --username=yourDockerHubUsername

After you logged in, you can tag the docker images.

docker tag bb38976d03cf yourHubUsername/dockerdata:firstTag

Now you can push your docker image to docker hub.

docker push  yourHubUsername/dockerdat

You can check whether actually it is pushed to the docker hub by logging in.

Deploying into EC2

Create an EC2 instance and launch. You should select a security group that has CLI access.

Login to EC2 instance and install docker. Follow the commands below.

sudo yum update -y
sudo yum install -y docker
sudo service docker start

Run the docker image in a given port. Here application runs on port 80.

docker run -d -p 80:9000 harshasira/restapp:tag

We need to provide a proper security group with access allowing to mentioned ports. Otherwise, it won’t work

Since you want to run docker in the background and service should be up and running even if you close the terminal, run it as a background process -d command is used for that.

Check if it is actually working using postman. Now exposed REST endpoints should work.

That’s it. Now we successfully created a Spring boot app with Dynamodb and deployed it to EC2 instance with docker.

Thanks for reading! (A few 👏 are always appreciated. 😀)

--

--