DEV Community

Cover image for Building Scalable Java Applications on AWS ECS with Fargate - A Step-by-Step Guide
ProsperAgada
ProsperAgada

Posted on

Building Scalable Java Applications on AWS ECS with Fargate - A Step-by-Step Guide

As a passionate advocate for AWS cloud technologies, I constantly explore ways to optimize and automate the deployment of applications. In this article, I will share a recent project where I deployed a containerized Java application on Amazon Elastic Container Service (ECS) using Fargate, Elastic Container Registry (ECR), and an Application Load Balancer (ALB). This hands-on experience is a reflection of my drive to share best practices and empower others in the community to leverage AWS services effectively.

Why This Project?

My goal is to contribute to the community by sharing practical, real-world examples of how AWS services can simplify application deployment. In this project, I demonstrate how to deploy a Java application with PostgreSQL using ECS and Fargate, a serverless compute engine that eliminates the need to manage servers. This approach reflects AWS's philosophy of automation and scalability, making it accessible for developers and businesses alike.

The Project Architecture

Image description
The key services used in this project are:

  • Amazon ECS (Elastic Container Service): Simplifies the orchestration of containerized applications.
  • Amazon ECR (Elastic Container Registry): Secures and manages Docker images.
  • AWS Fargate: Provides a serverless environment to run containers without managing infrastructure.
  • Application Load Balancer (ALB): Distributes incoming traffic across multiple containers for high availability.
  • Security Groups: Enforces security and access control at various layers. This combination of services showcases how AWS cloud-native solutions allow for seamless, scalable, and secure deployments.

Step 1: Cloning the Repository and Building the Java Application
To begin, I cloned the repository containing the Java and PostgreSQL application from GitHub:

Copy code
git clone https://github.com/ProsperAgada/java-springboot-app.git
Next, I used Maven to build the application into an artifact (.jar file), which would later be containerized:

Copy code
mvn clean package
This process generates the Java application's .jar file, which is the core component that will be deployed using AWS services.

Step 2: Containerizing the Application with Docker
To prepare the application for deployment, I created a Docker image using the following Dockerfile:

Copy code

FROM openjdk:11
WORKDIR /app
COPY target/*.jar app.jar
EXPOSE 8080
CMD ["java", "-jar", "app.jar"]
Enter fullscreen mode Exit fullscreen mode

Building the Docker image ensures that the application can run consistently across different environments. Here’s how I built the image:

Copy code
docker build -t simple-java-app .

Step 3: Pushing the Docker Image to Amazon ECR
With the Docker image ready, the next step was to push it to Amazon Elastic Container Registry (ECR):
1. Create ECR repository
Image description
2. Login to ECR:


Copy code



aws ecr get-login-password --region <region> | docker login --username AWS --password-stdin <aws_account_id>.dkr.ecr.<region>.amazonaws.com



3. Tag and Push the Docker Image:
Copy code



docker tag simple-java-app:latest <aws_account_id>.dkr.ecr.<region>.amazonaws.com/simple-java-app

docker push <aws_account_id>.dkr.ecr.<region>.amazonaws.com/simple-java-app

Step 4: Deploying the Application on ECS with Fargate
AWS ECS and Fargate make it easy to manage and scale containerized applications without worrying about infrastructure. To deploy the Java application, I followed these steps:
1. Create a Fargate Cluster: Using the ECS console, I created a Fargate cluster that supports our containers.
Image description

  • select fargate(serverless) for infrastructure
  • click create

2. Define a Task: The task defines how the containers run, specifying the image pulled from ECR and necessary configurations like port 8080.
Image description

  • select the ecsTaskExecutionRole
  • configure container 1 Image description container name: postgres container image: postgres container port : 5432
  • add environment variables for postgres database:
    POSTGRES_PASSWORD
    POSTGRES_USER = postgres
    POSTGRES_DB

  • configure container 2
    Image description

  • container port: 8080

  • protocol: http

  • add environment variables for java api:
    Image description
    DB_HOST = localhost
    DB_PORT = 5432
    POSTGRES_PASSWORD =
    POSTGRES_USER = postgres
    POSTGRES_DB

  • Click create

3. Set Up Security Groups: I configured security groups to allow traffic on HTTP (port 80), ensuring that the application is publicly accessible but remains secure.

4. Create a New Service
In the ECS dashboard, click on your cluster and navigate to the Services tab. Here, you will create the service.

  1. Configure Basic Service Settings Launch Type: Choose Fargate as the launch type since we’re using serverless container management.

Task Definition: Select the task definition that you created earlier. This task definition will pull your Docker image from ECR.
Service Name: Enter a name for your service (e.g., java-app-service).

Number of Tasks: Specify the desired number of tasks (replicas). For instance, if you want two instances of your container running, enter 2.

  • Select capacity provider strategy
  • application type select service
  • name your service Image description Click on Create to start the service creation wizard.

Under networking drop down
Configure VPC and Subnets

Cluster VPC: Select the VPC (Virtual Private Cloud) in which your ECS cluster is running. If you're using the default VPC, select it here.

Subnets: Choose the subnets where your tasks will be deployed. These subnets need to have internet access if you're deploying a web application.

Security Groups: Assign the security groups that control access to your service. You can create a new security group or use an existing one. Ensure port 80 is open for HTTP access if you want your application to be publicly accessible.
Image description

Step 5: Using an Application Load Balancer for High Availability
To ensure high availability, you will configured an Application Load Balancer (ALB):

  • ALB distributes incoming traffic across the running containers in the ECS cluster.
  • This setup helps the application handle more requests and improves its fault tolerance.

Step 6: Testing and Verifying the Deployment
Once everything was set up, I deployed the ECS service, and the application was accessible through the ALB’s public DNS. The load balancer successfully routed traffic between the running containers, ensuring the Java app was accessible to users through a single endpoint.




Top comments (0)