close
close
How to deploy Spring Boot apps on AWS

Spring Boot is the preeminent cloud-native software development framework of the Java world. Amazon prides itself on being the preeminent cloud hosting service, so it’s a natural choice to deploy apps built with Spring Boot to AWS, and this video shows you how.

Requirements

This Spring Boot tutorial assumes that you have already created a Spring Boot application and are ready to deploy. It assumes the following three things:

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

It is possible to deploy Spring Boot apps as WAR files, but JAR packaging embeds a Tomcat server into the built artifact, eliminating future application server configuration steps.

The destination folder is the default location for a JAR file built with Maven, and this is what we will assume for this tutorial. Different build tools save to different locations, so make a note of the location of your JAR file if your configuration differs.

How to deploy Spring Boot apps on AWS

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

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

Docker your Spring Boot application

The steps to containerize a Spring app and push the Docker image to a container directory can be 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=/:latest .

docker login

docker push

How to deploy Spring Boot apps on AWS
Tag your Docker build with the username of your container registry directory.

Creating an ECS task in AWS

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

Fargate simplifies container hosting by allowing users to deploy, manage, and scale a single container. This approach is in stark contrast to 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. To create an ECS task:

  1. In the AWS console, navigate to the ECS page.
  2. Choose Task definitions.
  3. Click Create 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 allocate to the Docker image.
  • The port mapping for the Spring Boot app’s RESTful APIs.
AWS Fargate
The ECS task points to the Docker image that contains the Spring Boot app to be deployed.

ECS Task vs. Service Delivery

ECS allows you to deploy using tasks or services. For testing a Spring Boot app, I like to use tasks. Tasks run like batch jobs in that they are not load managed and only run one container instance.

To host an active, robust and reliable website, one would use an ECS service. Configuring a task and a service is very similar and requires only a few additional steps to deploy the service.

Creating the Fargate cluster

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

  1. In the AWS console, navigate to the ECS page.
  2. Choose Cluster.
  3. Click on the Creating a cluster Link.
  4. Choose Fargate as an infrastructure type.
  5. Click Save to create the cluster.
Spring Boot AWS Fargate cluster definition.
The Fargate cluster must point to the task that hosts your Spring Boot app.

Run your task on the cluster

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

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

Access your AWS hosted Spring Boot app

The IP address assigned to your Spring Boot app can be found in the Configuration tab of the task being run. Use this IP address along with the task’s externalized port to access your Spring Boot application.

IP address of the Spring Boot AWS task.
You can find the AWS Spring Boot app public API address on the task configuration page.

Advanced Spring Boot and AWS configuration

A disadvantage of the described configuration is that a new IP address is assigned each time the task is redeployed. Therefore, this configuration is ideal for running batch workloads or quickly testing Spring Boot apps, but not suitable for non-stop hosting of a public website or SaaS service.

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, additional configuration tasks would be required.

For example, instead of deploying a task, it is better to deploy it to an ECS service. Fully hosting your Spring Boot application would also require assigning domain names and managing DNS records using Amazon Route 53.

Show all videos

By Olivia

Leave a Reply

Your email address will not be published. Required fields are marked *