AWS

How to Build and Deploy a Java Application on AWS Using ECS and Fargate – Part 1

As part of my day-to-day work, I spend a lot of time managing service infrastructure in AWS. Mostly, I deal with internal build environments where CDK is already the norm. But I’ve been on a quest to design an AWS-hosted API service outside of work—something I fully control end to end.

I’ve worked with systems that run on single hosts and even bare metal. But as more applications move into the cloud, containers have become the go-to solution.

Running a container locally is easy. But making that container publicly accessible, secure, and scalable is another story.

This series is my attempt to bridge that gap. And in Part 1, we’re starting with the basics:

Getting our Java app into a container.

Why ECS + Fargate?

You might be wondering: why not just spin up an EC2 box, install Docker, and call it a day?

ECS + Fargate makes life easier:

  • ECS: It’s AWS management container orchestration. Schedules and manages workloads.
  • Fargate: No servers to patch or manage. Just tell AWS how much CPU and memory I want, and it runs my containers.
  • Application Load Balancers (ALB): Handles traffic and health checks. Distributes traffic when the application is scaled to multiple containers.
  • Route 53: Friendly DNS instead of some ugly AWS URL.

The Example Project

As a huge book reader, the example which came to my mind is to build a tiny Books API . Nothing fancy, just a Spring Boot app that returns some book titles:

GET /books
→ ["Harry Potter", "The Fourth Wing", "The Midnight Library", "The Way of Kings"]

We will keep really simple to focus on the infrastructure

Step 1: Create the Java Project

mvn archetype:generate -DgroupId=com.example \\
                       -DartifactId=books-api \\
                       -DarchetypeArtifactId=maven-archetype-quickstart \\
                       -DinteractiveMode=false

Add Spring Boot dependencies in pom.xml and create a controller:

@RestController
public class BookController {
    @GetMapping("/books")
    public List<String> getBooks() {
        return List.of("Harry Potter", "The Fourth Wing", "The Midnight Library", "The Way of Kings");
    }
}

Locally, ./mvnw spring-boot:run gave the endpoint http://localhost:8080/books to access the Books API

Step 2: Containerize with Docker

Create Dockerfile under root book-api folder

# 1. Use Maven image to build the app
FROM maven:3.9.6-eclipse-temurin-21 AS build
WORKDIR /app
COPY pom.xml .
COPY src ./src
RUN mvn clean package -DskipTests

# 2. Use a slim JDK runtime for production
FROM eclipse-temurin:21-jre-alpine
WORKDIR /app
COPY --from=build /app/target/books-api-1.0.0.jar app.jar

# Expose Spring Boot default port
EXPOSE 8080

# Run the app
ENTRYPOINT ["java", "-jar", "app.jar"]

Build and run locally

mvn clean package
docker build -t books-api .
docker run -p 8080:8080 books-api

Verify

curl <http://localhost:8080/books>

Step 3: Cross-Platform Build (for AWS Fargate)

If you’re on an M1/M2 Mac, AWS Fargate expects linux/amd64, not arm64 and hence the docker need to be build with linux/amd64.

docker buildx build --platform linux/amd64 -t books-api .

Without this, you’ll hit weird runtime errors later.

Architecture Diagram (Series Overview)

Here’s the high-level flow we’ll build across this series:

What’s Next?

We now have a working Java application running in a Docker container.

In the next article, we’ll:

  • Initialize a CDK TypeScript project
  • Write the first stack that creates an ECS cluster and deploys our container
  • Attach an Application Load Balancer

By the end, you’ll hit your app through a public ALB DNS name.

Series Roadmap

  1. (This article) Build and containerize the Java app
  2. Deploy to ECS Fargate with ALB (via CDK)
  3. Add Route 53 and a custom domain
  4. Scaling, monitoring, and production-ready tweaks

One comment on “How to Build and Deploy a Java Application on AWS Using ECS and Fargate – Part 1

Leave a Reply

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