Launching Spring Boot Apps on AWS ECS Fargate

Deploying Spring Boot applications can be challenging, especially when it comes to managing containers in a production environment. However, by using AWS ECS Fargate, you can easily deploy and manage your Spring Boot applications in a scalable and reliable way. In this blog post, we’ll explore the benefits of using AWS ECS Fargate for Spring Boot application deployment, and walk through the steps to prepare your application for deployment on ECS Fargate. We’ll cover creating a Docker image, setting up an ECS Fargate cluster and task definition Let’s get started!

Quick Overview about ECS

AWS ECS (Elastic Container Service) is a fully managed container orchestration service that makes it easy to run, manage, and scale Docker containers on AWS. ECS provides several key concepts for managing containerized applications:

Cluster: An ECS cluster is a logical grouping of container instances that run your tasks. Each cluster can contain multiple container instances, and each instance can run multiple tasks.

Task: An ECS task is a running instance of a Docker container that has been launched from a task definition. A task definition is a blueprint for how to run a Docker container, including the Docker image to use, the CPU and memory requirements, the network settings, and more.

Service: An ECS service is a long-running task that runs continuously in the background, ensuring that your application is always available. A service is defined by a task definition, and it can be scaled up or down based on demand. ECS can automatically manage the load balancing and auto scaling of services.

Batch: An ECS batch job is a task that is launched for a finite amount of time to perform a specific job, like running a batch script or processing a batch of data. Batch jobs are defined by a job definition, which specifies the Docker image to use, the CPU and memory requirements, and other parameters. Batch jobs can be run on demand or scheduled to run at specific times.

By using ECS, you can easily deploy and manage containerized applications, while taking advantage of AWS features like load balancing, auto scaling, and monitoring.

ECS vs ECS Fargate
With ECS, you manage your own EC2 instances that run your containerized applications.

AWS ECS Fargate is a serverless container orchestration service that allows you to run containers without having to manage the underlying EC2 instances. With Fargate, AWS takes care of the infrastructure for you, allowing you to focus on running and scaling your containers. Fargate provides the same key concepts as ECS, including clusters, tasks, services, and batch jobs.

The main difference between ECS and ECS Fargate is that with ECS, you manage the EC2 instances that run your containers, whereas with Fargate, AWS manages the instances for you. This makes Fargate a more hands-off approach to container management, and can be beneficial if you don’t want to deal with the operational overhead of managing EC2 instances

let’s Create Simple Spring Boot Application that we will deploy on ECS

The code for this post is available on Github here

1 Create Simple Spring Boot APP
Create Rest API that will return simple String.

1
2
3
4
5
6
7
8
@RestController
public class HelloController {

@GetMapping("/hello")
public String hello() {
return "Hello Message from ECS Service";
}
}

2 Create Docker Image
Add Simple Docker file.

1
2
3
FROM openjdk:11
COPY target/spring-boot-ecs-Fargate-0.0.1-SNAPSHOT.jar spring-boot-ecs-Fargate-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java", "-jar", "/spring-boot-ecs-Fargate-0.0.1-SNAPSHOT.jar"]

Run Below command from root of your project. This will create the docker image with name ‘spring-boot-ecs’
docker build -t spring-boot-ecs .
Run Below command to verify docker image is working. You should be able to access API on http://localhost:8080/hello
docker run -p 8080:8080 spring-boot-ecs

3 Create ECR repo for uploading Image
AWS ECR (Elastic Container Registry) is a fully-managed Docker container registry that makes it easy to store, manage, and deploy Docker container images. ECR is tightly integrated with ECS, making it easy to store and manage your Docker images for use in ECS tasks.

Here are some key concepts to understand about ECR:

Repository: An ECR repository is a collection of Docker images with the same name and tag. Each repository has a unique URL that you can use to access the images.

Image: An ECR image is a Docker image that has been uploaded to an ECR repository. Images can have multiple tags, allowing you to have different versions of the same image.

Registry: An ECR registry is a collection of ECR repositories that you own. Each AWS account can have one ECR registry.

To use ECR with ECS, you first need to create an ECR repository to store your Docker images. You can then build your Docker image locally, tag it with the ECR repository URL and push it to ECR using the docker push command. Once your Docker image is in ECR, you can reference it in your ECS task definitions and services.

ECR provides a secure and scalable way to manage your Docker images, and it integrates seamlessly with ECS to provide a complete container management solution.

Create an ECR Repo by following step by step guide

Upload Image
Create Separate IAM User which will have access to upload image to ECR Repo. You can use AWS CLI for uploading images.
docker push <your-aws-account.your-aws-repo/spring-boot-ecs:latest

4 Setting up AWS ECS Fargate cluster and task definition

Here are the steps to follow:
Create an ECS cluster, which is a logical grouping of container instances.

Define a task definition for your application, which specifies the Docker image to use, along with any required configuration.

Configure the network settings and security groups for your task definition.

Deploying your Spring Boot application on AWS ECS Fargate

With your ECS cluster and task definition in place, you can now deploy your Spring Boot application on AWS ECS Fargate. Here are the steps to follow:

Create an ECS service for your task definition, which specifies the number of tasks to run, along with the desired load balancing and auto scaling settings.

Once’s the service is successfully deployed, You can access the API by using Public IP Address of running task

Share Comments