How to Easily Deploy a Spring Boot Application on AWS Fargate

Hello, I am a full stack developer since 2010 focused for a long moment more on backend stuffs. I embraced cloud development since 2019.
Search for a command to run...

Hello, I am a full stack developer since 2010 focused for a long moment more on backend stuffs. I embraced cloud development since 2019.
No comments yet. Be the first to comment.
Security in internet-facing or local applications is one of the most challenging parts of the software development life cycle. This post will provide a quick step-by-step guide to implementing access management for the previous REST API using Amazon ...

In this post, we will create a REST API endpoint with GET and POST methods for the previous backend using Amazon API Gateway's HTTP API. Prerequisites All you need is just an AWS account: no worries, if you are new to AWS, AWS offers you a 12-month f...

In this article, we'll build a simple CRUD JavaScript-based backend with AWS. We'll use Lambda and DynamoDB to show how you can quickly create a secure and robust app backend. Prerequisites To follow this tutorial, you need an AWS account. Thankfully...

There are many articles on how to build a real-world Spring Boot CRUD REST API or Application with MySQL as a Relational Database. In this post, we are not only going to show how to create a Spring Boot CRUD operations API, but we will also dive deep...

Before entering the AWS Cloud world, as a Java developer, it was often difficult for me to get Java-based applications online for my clients. Fortunately, I found out later that AWS provides many proven, fully managed services for deploying Java-based applications such as EC2, Elastic Beanstalk, Amazon ECS, etc.
In this post, we will show how to quickly deploy a Spring Boot Application on Amazon ECS with AWS Fargate deployment option. AWS Fargate is a Serverless compute engine that can be used to launch and run containers without having to provision or manage EC2 instances.
In the next lines, we will :
See how to create a Spring Boot REST API Docker image
Go through how to push a containerized API on the docker repository and deploy this containerized API on AWS
Create an ECS Cluster
Create a Fargate compatible Task Definition
Run the configured Task to make the application available online
Have a Docker repository account
Install Docker Desktop on your local machine
Have an AWS account
Through this section, instead of creating a Spring Boot Web application, we will create a Spring Boot Rest API for simplicity. Notice that the deployment process is the same even for Spring Boot Web App and Spring Boot REST API. For demonstration purposes, you can clone the Git hub repository here and open it in your favorite IDE.
In the project root, create a docker file named: Dockerfile (with no file extension). Paste the below content:
FROM openjdk:11
COPY ./target/docker-demo-0.0.1-SNAPSHOT.jar docker-demo-
0.0.1-SNAPSHOT.jar
CMD ["java","-jar","docker-demo-0.0.1-SNAPSHOT.jar"]
EXPOSE 8080
Line 1: Instruction to build docker image from openjdk:11 image
Line 2: Instruction to copy the jar file into the Docker container file system while building
Line 3: is a java command to launch the jar file while executing the container
Line 4: informs Docker that the container listens on 8080 port at runtime.
Open the terminal and execute the following docker command in the project root:
docker image build -t docker-demo .
You should have a result like this:

Open the terminal and execute the following command:
docker container run --name dockerDemoApp -p 8080:8080 -d
docker-demo
Go to Docker Desktop, you should see this:

You can then paste this link http://localhost:8080/api/ in the browser to get the following result :

In order to push the Docker local image into the Docker hub repository, you just need to execute the following command line:
docker push xxx/docker-demo-app:latest
Make sure to replace xxx with your user name.
In this section, we will work step by step to finally get the REST API online.

Click on Create Cluster
Step 1: On Select cluster template step, select Networking only option and click on Next Step
Step 2: Provide the Cluster name : Demo-cluster, check Create VPC option, leave all other default options and click on Create.


On the left side, click Task Definitions and Create new Task Definition to access the Create new Task Definition page.
-- Container name : dockerDemoApp
-- Image : xxx/docker-demo-app:latest, the Docker hub image repository (xxx : your Docker hub user name)
-- Memory Limits (MiB) : Soft limit : 128
-- Port mappings : 80 -> tcp and 8080 -> tcp
-- Click on Add to navigate back to Create new Task Definition page
Leave all other options and click on Create
Click on View task definition to see the created task definition details.

In this last section, we are going to run our Docker container image.
In the Task Definition: docker-demo-task:1 page, click on Actions then Run Task, the Run Task page will open.

-- Assigned security groups : Create new security group
-- Security group name : docker-demo-sg
-- Inbound rules for security group: Choose All Traffic Type and Anywhere Source
-- Click on Save, and you will be redirected to the Run Task page.
Auto-assign public IP : ENABLED
Leave all other default options and click on Run Task.

Now that the deployed API's container is running, let's try to access it through a public IP address.

In the Demo-cluster details page, go to Tasks tab.
In the listed tasks, click on the Task whose Task definition column is docker-demo-task:1, and the Task details page should open.

In the Task details page, copy the Public IP address
Open a browser or Postman, paste the IP address, and append the port and /api/ to see the magic happen!

Through this post, we saw how to easily and quickly deploy a Spring Boot Application on AWS using AWS Fargate by creating a Spring Boot REST API Docker container image, pushing it on the Docker hub repository, creating and configuring Amazon ECS Cluster and Task definition witch helped us to deploy and run our REST API container image on AWS Cloud. Thanks for the reading.