AWS Steppingstones #3- ECS Learning Series Part 03 - Deploying a Java Spring Boot app in ECS
Now it's time to get your hands dirty. In this post, we will try to deploy a sample Java Spring Boot application in Amazon ECS service and access it via internet.
Hello Reader,
I am sure you’re doing great today and thanks for taking time to read this post. Any kind of comments/feedback from you is always welcome. This will help me to improve myself and make the content better next time.
This is the third post in the “AWS SteppingStones” category and following it up on the previous post on ECS Concepts and ECR as part of the ECS learning series.
Missed the previous one? Click the below link to access it.
AWS Tip of the Week - “Cloud Comparisons”
This web page from AWS features content that helps readers understand common use cases for when to use one cloud solution or another. Compare and contrast cloud solutions and learn the nuances of different use cases that work best for your situation.
Some of the interesting topics you can see here are,
Kubernetes vs Docker
Containers vs Virtual Machines
IPv4 vs IPv6
Back to our topic.
To deploy our Java Spring Boot application on Amazon ECS (Elastic Container Service), we will need to package your application into a Docker container and set up the ECS cluster to run the containerized app.
Let’s begin with the prerequisites and then take it step-by-step.
Prerequisites
As indicated in the previous posts, we will need the following to complete this hands-on.
AWS Account
Java 17 and Maven setup
AWS CLI installed and configured with the needed permissions
Docker Desktop application
Step 1 - Package your application as Docker Image
To make it easy for you, I have already created the sample Java Spring Boot application to use in this exercise. To package it as a Docker Image, we’ll need a Dockerfile and that’s also available in the below GitHub link.
https://github.com/ChandruD-star/main-repo/tree/main/java/sample-java
Copy the contents of the above directory and save it in your workstation under the folder “sample-java”.
You can try running this application locally after successful build and the app can be accessed with the localhost URL in your browser at http://localhost:8080/hi and should see the message “Hi! I am running in Amazon ECS”.
To package this app as a Docker image locally, run the below command from the directory containing the Dockerfile.
docker build -t sample-java-app:v1 .
FROM openjdk:17-slim
COPY ./target/sample-java-1.0.jar /app/sample-java-1.0.jar
WORKDIR /app
ENTRYPOINT ["java","-jar", "sample-java-1.0.jar"]
Now we should see the image sample-java-app:v1
in the list of images in Docker Desktop tool under the “Images” tab.
Step 2 - Push the image to ECR
Now the Docker image created in the previous step can be pushed to ECR (Elastic Container Registry) from where the ECS service will pull the image from.
a. Now you need to create the repository in ECR which can be done in different ways like using AWS Management Console, AWS CLI command or using the ECR APIs. For this exercise, you can create a repository from AWS Management Console itself and give the name as you wish. (Ex: sample-java-ecs-app)
b. To push the image from our local workstation to this ECR repository, we need to authenticate with the AWS CLI. The sample command is given below.
aws ecr get-login-password --region <region> | docker login --username AWS --password-stdin <aws_account_id>.dkr.ecr.<region>.amazonaws.com
c. Tag and push the image with the below commands.
Tagging:
docker tag sample-java-app:v1 <aws_account_id>.dkr.ecr.<region>.amazonaws.com/sample-java-ecs-app:v1
Push to ECR:
docker push <aws_account_id>.dkr.ecr.<region>.amazonaws.com/sample-java-ecs-app:v1
Important Note
In the above commands, you can replace <region>
with the AWS region you use and <aws_account_id>
with your actual AWS ACCOUNT ID.
If you have completed the steps successfully, you should the image tag sample-java-ecs-app:v1
listed in the ECR repository images screen.
Step 3 - Setup the ECS Cluster and Task Definition
To set up an Amazon ECS cluster and create a task definition using the JSON option, you can define your ECS task definition in a JSON file and use either the AWS Management Console or the AWS CLI to register it.
You can create an ECS cluster using either Fargate (serverless) or EC2 (managed instances). For simplicity, let's create a Fargate cluster.
Go to the ECS service in the AWS Management Console.
On the left side, select Clusters and click Create Cluster.
Configure the cluster:
Cluster name:
sample-ecs-cluster
Leave other fields as default.
Click Create to finish creating the cluster.
An ECS Task Definition describes how the Docker containers should be launched and maintained. A sample ECS Task Definition file can be created using the JSON file and sample one is provided below.
Please remember to use the subnet ID and security group ID in your AWS account to configure the networking settings for the task to ensure public access.
{
"family": "sample-java-ecs-task",
"networkMode": "awsvpc",
"containerDefinitions": [
{
"name": "helloworld-container",
"image": "<aws_account_id>.dkr.ecr.<region>.amazonaws.com/sample-java-ecs-app:v1",
"portMappings": [
{
"containerPort": 8080,
"protocol": "tcp"
}
],
"essential": true,
"memory": 4096,
"cpu": 2048
}
],
"requiresCompatibilities": [
"FARGATE"
],
"cpu": "2048",
"memory": "4096",
"executionRoleArn": "arn:aws:iam::<aws_account_id>:role/ecsTaskExecutionRole",
"networkConfiguration": {
"awsvpcConfiguration": {
"subnets": [
"<subnet-ID>" // Replace with your subnet ID
],
"securityGroups": [
"<sg-ID>" // Replace with your security group ID
],
"assignPublicIp": "ENABLED"
}
}
}
Some of the important fields to understand here are,
"family"
: Defines the family name for your task definition."networkMode": "awsvpc"
: Uses the AWS VPC network mode, required for Fargate."containerDefinitions"
: Defines the container settings."image"
: This is your Docker image URI from ECR."portMappings"
: Maps container port8080
to the host."memory"
and"cpu"
: Specify container memory and CPU.
"requiresCompatibilities"
: Specifies that Fargate should be used."executionRoleArn"
: This is the IAM role that ECS tasks use to pull container images and publish logs."networkConfiguration"
: Specifies the VPC subnets and security groups for the task.
Now go to Task Definitions in the ECS console and click Create New Task Definition using JSON option. Then use the above JSON file to create the task “sample-java-ecs-task”.
Step 4- Setup ECS Service and Application Access
Once the task definition is registered, you can create an ECS service to run your tasks using the Management Console or CLI.
The steps are,
Go to ECS Console.
Select the Cluster (
sample-ecs-cluster
).Under the Services tab, click Create.
Configure the service:
Launch Type:
Fargate
Task Definition: Select the task definition you registered (e.g.,
sample-ecs-task
).Service Name:
sample-ecs-service
Number of Tasks: 1
Cluster VPC and Subnets: Select your VPC and subnets (ensure they have internet access).
Ensure that your security group allows traffic on port
8080.
Click Create Service.
How to access the application?
Go to the ECS Console, select the Task in running status and find the public IP address of the running task.
Open the browser and navigate to
http://<task_public_ip_address>:8080/hi
You should see the message “Hi! I am running in Amazon ECS“.
Summary
First, we have built the sample Java Spring Boot application locally and the jar file is generated. Then the Dockerfile is used to build the image.
The Docker image is pushed to Elastic Container Registry (ECR) after successful authentication.
A new ECS cluster is created with the Fargate compute provider and the ECS Task definition is created using the JSON option.
Note:- The VPC, subnet and security group creation steps are not detailed as it’s out of scope for this exercise.
Then the ECS service is created using the task definition file and the public IP address is retrieved from the running task.
Accessed the application using the public IP endpoint.
What’s next?
In the next post, we will see how we can auto scale the web application based on the incoming requests and how ECS handles it seamlessly.
Thank you again for your support. Happy learning!!!