How to deploy Spring Boot apps in AWS

Spring Boot is the Java world's preeminent, cloud-native software development framework. Amazon prides itself as the preeminent cloud-hosting service. So, it's a natural fit to deploy apps built with Spring Boot in AWS, and this video walkthrough shows you how.

Prerequisites

This Spring Boot tutorial assumes you already have a Spring Boot application built and ready to deploy. That assumes the following three things:

  • Your Spring Boot app was configured to be packaged as a JAR file.
  • You have run and tested your Spring Boot application locally.
  • You have executed a Maven or Gradle build and the built JAR file is in the project's target folder.

It's possible to deploy Spring Boot apps as WAR files, but JAR packaging embeds a Tomcat server within the built artifact which eliminates future application-server configuration steps.

The target folder is the standard location for a JAR file built with Maven, which is what this tutorial assumes. Different build tools save to different locations, so take note of your JAR file's location if your configuration differs.

How to deploy Spring Boot apps in AWS

To deploy a cloud-native microservice built with Spring Boot in AWS, follow these steps:

  1. Dockerize the application and push the image to your container registry.
  2. Create a Fargate task in AWS that references your published Docker image.
  3. Create an Amazon ECS cluster to host your Spring Boot app in AWS.
  4. Run the Fargate Task on your ECS cluster.
  5. Access your AWS hosted Spring Boot app on its publicly assigned IP address.

Dockerize your Spring Boot application

The steps to containerize a Spring app and push the Docker image to a container registry are easily scripted and added to your CI/CD process.

Assuming your Spring Boot project contains a Spring Boot Dockerfile, the commands to build and push a dockerized Spring Boot app are as follows:

docker build --tag=<dockerhubname>/<imagename>:latest .

docker login

docker push

Docker build for Spring Boot AWS app.
Tag your Docker build with your container registry's username.

Create an ECS task in AWS

The AWS Elastic Container Service (ECS) provides a serverless, fully managed container-hosting service named Fargate.

Fargate simplifies container hosting in that it lets users deploy, manage and scale a single container. This approach contrasts starkly with a multi-pod, multi-node deployment with Kubernetes.

With ECS, you first describe your container by creating a task, and then create an ECS cluster that can run the task. Here are the steps to create an ECS task:

  1. Navigate to the ECS page in the AWS console.
  2. Select Task definitions.
  3. Click Create a new task.

A Fargate-based ECS task definition should specify these criteria:

  • The network addressable name of the Docker image.
  • The amount of memory to allocate to the Spring Boot microservice.
  • The number of virtual CPUs to assign to the Docker image.
  • The port mapping for the Spring Boot app's RESTful APIs.
Spring Boot in AWS Fargate.
The ECS task will point to the Docker image that contains the Spring Boot app to be deployed.

ECS task vs. service deployment

With ECS, you can deploy with tasks or services. To test a Spring Boot app, I like tasks. Tasks are run like batch jobs in that they are not workload managed and only run one container instance.

To host a live, robust, reliable website one would use an ECS service. The configuration of a task and service are very similar, requiring only a few extra steps for service deployment.

Create the Fargate cluster

An ECS task must run upon a cluster. To create a serverless ECS cluster, follow these steps:

  1. Navigate to the ECS page in the AWS console.
  2. Select Clusters.
  3. Click on the Create cluster link.
  4. Select Fargate as the infrastructure type.
  5. Click Save to create the cluster.
Spring Boot AWS Fargate Cluster definition.
The Fargate cluster must reference the task that hosts your Spring boot app.

Run your task on the cluster

With the ECS task configured and the Fargate cluster created, the final step is to run the task on the cluster. To do so, follow these steps:

  1. Click on the cluster and navigate to the Tasks tab.
  2. Click the button that says Run new task.
  3. Choose the Spring Boot AWS task as the family type to run.
  4. Choose a public subnet for your task.
  5. Assign a security group to the task that allows incoming HTTP traffic.
  6. Choose the option to auto-assign a public IP address.
  7. Click Run Task.

Access your AWS hosted Spring Boot app

The IP address assigned to your Spring Boot app is on the running task's Configuration tab. Use this IP address, along with the task's externalized port, to access your Spring Boot application.

Spring Boot AWS task IP address.
The public API address of the AWS Spring Boot App can be found on the task's configuration page.

Advanced Spring Boot and AWS configuration

One downfall to the described configuration is that a new IP address is assigned each time the task is redeployed. That makes this configuration ideal to run batch workloads or quickly test Spring Boot apps, but it's not suitable for disruption-free hosting of a public website or SaaS service.

Further configuration tasks would be required to provide high availability for a Fargate deployment of a dockerized Spring Boot app on AWS and ensure reliable website or RESTful web service hosting.

For example, instead of deploying a task, it's better to deploy to an ECS service. Full-scale hosting of your Spring Boot application also would require domain name assignment and DNS record management with Amazon's Route 53.

View All Videos
App Architecture
Software Quality
Cloud Computing
Security
SearchAWS
Close