How to deploy Spring Boot apps in AWS (original) (raw)
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:
- Dockerize the application and push the image to your container registry.
- Create a Fargate task in AWS that references your published Docker image.
- Create an Amazon ECS cluster to host your Spring Boot app in AWS.
- Run the Fargate Task on your ECS cluster.
- 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
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:
- Navigate to the ECS page in the AWS console.
- Select Task definitions.
- 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.
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:
- Navigate to the ECS page in the AWS console.
- Select Clusters.
- Click on the Create cluster link.
- Select Fargate as the infrastructure type.
- Click Save to create the cluster.
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:
- Click on the cluster and navigate to the Tasks tab.
- Click the button that says Run new task.
- Choose the Spring Boot AWS task as the family type to run.
- Choose a public subnet for your task.
- Assign a security group to the task that allows incoming HTTP traffic.
- Choose the option to auto-assign a public IP address.
- 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.
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.