AWS Fargate is a serverless compute engine for Amazon ECS that allows developers to run containers without managing servers or infrastructure. Deploying Java microservices on AWS Fargate provides automatic scaling, better resource management, high availability, and simplified deployment, making it ideal for modern cloud-based applications.
Understand Amazon Elastic Container Service
Amazon Elastic Container Service (ECS) is a fully managed AWS service used to run, manage, and scale Docker containers efficiently. It simplifies container deployment and integrates with AWS services like Elastic Load Balancing, VPC and IAM for better scalability, networking and security.
- ECS provides a simple setup and management process for container deployments.
- It supports automatic scaling and cost-effective resource utilization with a pay-as-you-go model.
- ECS integrates with AWS IAM and VPC services to provide secure container execution.
- ECS simplifies the deployment, scheduling, and scaling of containerized applications.
- It supports both EC2 instances and AWS Fargate for deploying containers.
- ECS integrates with Elastic Load Balancer to distribute traffic efficiently and ensure high availability.
Step-by-Step Deployment
The deployment process involves containerising the Java microservice using Docker, pushing the image to Amazon ECR and deploying it on Amazon ECS with AWS Fargate. ECS manages infrastructure, scaling and monitoring automatically with support from services like CloudWatch.
In this article, we will use AWS open-sourced project ECS_Java_Spring_PetClinic.
GitHub Link : https://github.com/aws-samples/amazon-ecs-java-microservices
Step 1: Launch EC2 Instance
- Create Amazon Linux EC2 instance
- Use t2.medium or t3.medium
- Download the Key-Pair
- Allow ports 22 (SSH) 80(HTTP) 8080(Custom TCP)
- Connect to EC2
Step 2: Update and Install packages
For update system
sudo yum update -yInstall Git
sudo yum install git -y
git -version
Install java 8
sudo yum install java-1.8.0-amazon-corretto-devel -y
java -version
Install Maven
sudo yum install maven -y
mvn -version
Install Docker
sudo yum install docker -yStart Docker
sudo service docker startEnable Docker
sudo systemctl enable dockerGive Docker permissions
sudo usermod -aG docker ec2-user
docker -version
Step 3: Clone GitHub Repository
Clone the GitHub repository to your EC2 instance so that you can access the Spring Boot microservices source code, build the applications, and create Docker images for deployment.
git clone https://github.com/aws-samples/amazon-ecs-java-microservices
Go inside
cd amazon-ecs-java-microservices/2_ECS_Java_Spring_PetClinic_Microservices
Step 4: Build Spring Boot Microservices
- Generate all .jar files individually.
- Go to each spring-petclinic-rest-directory
mvn clean package- Verify generated .JAR files
find . -name "*.jar" 
Step 5: Build Docker images and Tags for each service
- Make Dockerfile in each service
- Build Docker Image
docker build -t owner-service .- For spring-petclinic-rest-owner
FROM eclipse-temurin:8-jre-alpine
VOLUME /tmp
ADD target/spring-petclinic-rest-owner-1.7.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
- For spring-petclinic-rest-pet
FROM eclipse-temurin:8-jre-alpine
VOLUME /tmp
ADD target/spring-petclinic-rest-pet-1.7.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
- For spring-petclinic-rest- system
FROM eclipse-temurin:8-jre-alpine
VOLUME /tmp
ADD target/spring-petclinic-rest-system-1.7.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
- For spring-petclinic-rest-vet
FROM eclipse-temurin:8-jre-alpine
VOLUME /tmp
ADD target/spring-petclinic-rest-vet-1.7.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
- For spring-petclinic-rest-visit
FROM eclipse-temurin:8-jre-alpine
VOLUME /tmp
ADD target/spring-petclinic-rest-visit-1.7.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
- Check Docker images
docker images
- Login to Docker Hub
docker login- Tag Docker Images
docker tag SERVICE_NAME YOUR_USERNAME/SERVICE_NAME
- Push Docker Images
docker push YOUR_USERNAME/SERVICE_NAME
Step 6: Create ECS cluster
- Open ECS dashboard
- Choose "Create Cluster"
- Cluster name "petclinic-cluster"
- Infrastructure choose "Fargate only"

Step 7: Create Task Definitions for All Services
- Go to Task Definitions
- Click "Create New Task Definition"
- Task Definition Family name choose "owner-service
- Choose "Fargate"
- Operating System / Architecture keep "Linux/X86_64"
- Task Size CPU "0.5 vCPU" Memory "1GB"
- Task role leave empty
- Task Execution Role Select "ecsTaskExecutionRole" or "Create new role"
- Do Repeat for all service
Step 8: Container-1
- Scroll down to Container-1
- Task name "owner-service"
- Image URI "your-username/service-name"
- Container Port "8080"
- Protocol choose "TCP"
- App Protocol "HTTP"
- Click "Create"
- Do Repeat for all service
Step 9: Run ECS Tasks of all service
- Open Cluster in ECS
- Choose "petclinic-cluster"
- Click "Run New Task"
- Choose the service name
- In Compute configurationĀ advanced choose "Launch type"
- Choose Launch Type "Fargate"
Step 10: Scroll to Networking
- Select VPC
- Choose Public Subnet
- Select Security Group
- Auto Assign Public IP
Step 11: Verify Running Tasks
- Wait until status becomes "RUNNING" for all service

Step 12: Access Application
- Open Task
- Go to Networking section
- Copy Public IP
- Go to browser
http://PUBLIC IP OF YOUR TASK:8080