Have you ever wondered how to deploy a Java containerized application on AWS? If so, this article will show you a way to do it. We are going to build a small Java Spring Boot application step by step. You will learn how to define resources, classes, controllers, dependencies, etc. You will also learn how to deploy it on , which is a managed service that allows you to deploy containers without needing to spend any time on orchestration.

What Is Spring Boot? 

What exactly is Spring Boot? Spring Boot is an opinionated version of the Spring framework, which includes by default a set of packages that will give you a ready-to-run Spring application. Check .

Developing a Spring Boot Application

The application (let’s call it “Booksapp”) that will be deployed on AWS Fargate is a small REST API with two endpoints:

Booksapp endpoints

For simplicity, the data layer will be a static array of book resources, and it will be located in our main controller. Real applications will require that you connect to a database engine and use an extra layer of logic for data access. 

The final application tree will look like this:

Booksapp files tree

Before getting into the code, you’ll need to make sure that you have the following requirements on your machine:

Let’s start by creating the Book.java file, in which you’ll define a resource class for your books.

The code for our resource will look like:

Your Book resource class will contain three properties: id, name, and author. This class only contains a few getter methods since it will be a read-only API.

Below you can see how the pom.xml is defined:

There are only two dependencies needed for this example: , which will automatically parse the Book resource objects to JSON, and the Spring Boot framework itself. 

You can now code your controller (BooksController.java):

The controller consists of two endpoints, both of which are mapped to handler functions via the GetMapping annotation. The first endpoint will return a list of books, and the second one will return a single book based on its ID. 

Now, you need to define your Java Application:

To run your app, you need to compile and package it (the latter includes the compilation):

If no errors show up, you can run the application as:

This will launch a tomcat instance that will listen for traffic on port 8080. Fantastic! Now you can try your endpoints using :

GET /books/11

When it comes to Spring Boot, it’s usually a good idea to use . This comes with more utilities plus a basic structure for testing. 

In this post, we have started from scratch on purpose so that you’ll be aware of every piece of code that will be deployed. The final code can be .

Packing a Spring Boot Application in a Container

Now that you have your application running locally, the next step will be to containerize it. To do this, you are going to use . Your goal on this step will be to create a that allows you to create a Docker image and run it on a container.

Let’s take advantage of the fact that the application was built locally. So your first attempt would be to only run the application on a container and skip the building and packaging work. The Dockerfile should then look like this:

This will allow you to build an image that contains the jar file and be able to run the application by defining an entry point with the java -jar command. If you’re wondering why an alpine image was used, please check .

For building your image, let’s execute (from the root folder):

If everything went OK, you can now run the application inside a container:

Go ahead and check the endpoints again. They should work as expected. Notice how in the run command the port 8080 is mapped from the container to the host OS. This allows you to have the application running on the same port as on the host OS.

Our Dockerfile still needs some love since its present state doesn’t cover the build of the application. Let’s try to do it the right way by having a multi-stage build:

What this Dockerfile is telling you is that you’re going to have two stages for building your final image. The first one will take care of the build itself:

The second stage will take advantage of the previous build stage, use it for copying extracted files, and then actually run the application. If you want to dig around further on Docker and Spring Boot applications, feel free to check the .

Amazon AWS Fargate: What Is It?

is an AWS managed service that is responsible for provisioning and orchestrating your containerized application. This means that you can deploy hundreds of containers without having to define any compute resources because the service will do it for you.

Deploying a Spring Boot Application on AWS Fargate

With the Docker image in place, you are now ready for deploying your Booksapp to AWS Fargate. The steps to follow are:

In order to push the image to AWS ECR, you need to first create a repository:

Using Docker for building and running the application

Once this is created, you will be provided with the push commands:

After pushing the image, you will have something like this:

Image pushed on AWS ECR

With the image on AWS ECR, you can now define your deployment on AWS ECS/Fargate. Let’s create your task by going to “create a new task definition” option on AWS ECS and then select the type FARGATE:

AWS ECS/Fargate

AWS will ask for a task name, some hardware allocation (take your time with this step, as you will be charged per the size of the allocated resources), and the container definition, where the Docker image URI provided by AWS ECR will be set.

ECS Fargate task definition

ECS Fargate hardware allocation

ECS Fargate container definition

It’s also important to set the port that your container will expose, 8080 in your case. The next step is to actually run the task on a cluster. You can take advantage of the default cluster for this, and you will need to specify which task you want to run on this cluster as well as how many instances of the task you want to have.

Here, you can set the number of tasks to 1 since it’s a small application:

ECS Fargate cluster definition

You can increase this number and set the service with a load balancer in front of handling the traffic. Take a look at .

ECS Fargate cluster definition

Once you create the cluster and it’s up and running, you can go ahead to the task definition and obtain the public IP provided by ECS on the details page:

ECS Fargate cluster details

ECS Fargate task details

With that IP, you can hit your endpoints again (don’t forget to open the port 8080 on the security group assigned to your cluster):

GET /books/ from AWS

Great job! Now you have your application running on AWS Fargate!

Monitoring Your Spring Boot Application

AWS Fargate is integrated with AWS , meaning that you can set alarms based on metrics defined by you as well as see the logs generated from every task/container. For logging, make sure that the log integration option is checked:

CloudWatch Logs

Once the task is running, logs will appear on the Log tag:

Take a look at . If you’re interested in a deeper level of monitoring and troubleshooting, check out Epsagon’s automated tracing for AWS Fargate.

Tracing ECS Fargate with Epsagon

In this post, we took you through how to code and deploy a small Spring Boot application on AWS Fargate. You started by coding a Book resource class and ended up using the default cluster of AWS ECS for deploying a service. You have now seen how AWS Fargate took the server provisioning and server orchestration out of the equation and how you spent more time coding the application than deploying it. 

This is the value that Fargate provides: allowing you to focus on code and business logic, not server configurations and provisioning.

This content was originally published here.