Deploy a Spring boot Application on AWS Fargate.

Nimesh Rupasinghage
4 min readJul 3, 2022

What is AWS Fargate ?

First, Lets see what is AWS Fargate. AWS Fargate is a technology which we can use with AWS ECS to run containers. It is Serverless : which we don’t need to maintain servers or clusters of Amazon EC2 instances. The serverless architecture has eliminated the functionalities such as provisioning, scaling, configuring and optimization of clusters.

With the march of time, information technology has evolved with so many innovations , latest technologies and is transforming to a digital era. Amazon Web Services has done a huge job being a part of this BIG transformation.

Ok! Enough Talking, Lets dive into the today’s topic : AWS Fargate !

We all know that, When it comes to the developers’ perspective We have multiple ways to deploy our services using AWS managed services such as Amazon ECS, Amazon Elastic Beanstalk and Amazon EC2.

Today We are going to see how we can deploy a Spring boot Application on Amazon ECS using Amazon Fargate. Fargate is a compute engine which focuses on launching and running containers without the involvement of EC2 instances.

Prerequisites

  1. Should have an AWS account.
  2. Should have a Docker Repository Account.
  3. Should have installed Docker on local machine.
  4. A Spring boot REST API

Here, I’m not going to explain how to create a Spring boot REST API. Considering that you all have a basic Spring boot REST API, lets continue.

Creating a Docker Container Image for the Spring boot Application.

Create a Docker File.

We must create a file named : Dockerfile with no extension in the root of the project. Include the following code snippet to the created Dockerfile.

FROM openjdk:17
COPY ./target/fargate-demo-0.0.1-SNAPSHOT.jar fargate-demo-
0.0.1-SNAPSHOT.jar
CMD ["java","-jar","fargate-demo-0.0.1-SNAPSHOT.jar"]
EXPOSE 8080

Line 1: Instruct to build the docker image using openjdk:17 image.

Line 2: Copy the jar file into the Docker container file system on build.

Line3: Command to launch the jar while executing the container.

Line4: Inform Docker that the container listens on port 8080.

Build the Docker Image.

In the terminal, run the following command.

docker image build -t fargate-demo .

Verify the Docker image creation by executing the command:

docker images

Test the Docker Image locally.

Execute the following command on the terminal.

docker container run --name fargateDemoApp -p 8080:8080 -d 
fargate-demo

Verify the Docker container creation by executing the command:

docker container ls

Also, you can verify using http://localhost:8080/api. Remember to use your own endpoints to test this.

Push Docker image to the Docker Hub Repository.

Execute the following command to push your docker image to the Docker Hub.

docker push username/fargate-demo-app:latest

Note: Replace username with your username.

  • Here : latest is the image tag.

Now, We have come to the most interesting part of our process :

Deploying the Spring boot Application to AWS Fargate.

Il take you through step by step.

# Create Amazon ECS Cluster

  1. First, Sign in to AWS Console.
  2. Then, Search for ECS and Go to the ECS Dashboard.
  3. Click on Create Cluster.
  4. Select Networking Only option and click next.
  5. Provide a cluster Name : Ex : fargate-cluster.
  6. Click on Create VPC and leave the rest as default.
  7. View the created cluster and you will be able to see services, tasks, scheduled tasks, tags etc associated with the cluster.

# Create the Task Definition

  1. On ECS Dashboard, navigate to Task Definitions : to create new Task Definition.
  2. Select launch type compatibility : FARGATE.
  3. Provide a Task Definition name : Ex: fargate-task.
  4. For Task Role: Can use your own Role if you have already created one OR you can leave it as it is: so an IAM Role will be created on your behalf.
  5. Select LINUX for Operating system family.
  6. For Task Execution Role : use your own Role if you have already created one OR you can leave Create New Role option.
  7. Provide Task Memory : Ex: 1GB.
  8. Provide Task CPU: Ex: 0.5 vCPU.
  9. Next Add Container.
  10. Provide Container Name, Image, Memory Limits and Post Mapping details.
  11. Click Add and navigate Back to Create Task Definition.
  12. Leave all other configs as default and click Create.
  13. Click View Task Definition to see summary of the created Task Definition.

# Run the Task Definition

Now We are ready to run our Docker Container Image.

  1. Next On Task Definitions page, Select a specific Task Definition.
  2. On the Selected Task Definition page, Click Run Task under Actions dropdown.
  • Launch type : FARGATE
  • Operating system family : Linux
  • Task Definition : Familly -> fargate-task / Revision -> 1
  • Platform version : LATEST
  • Cluster : fargate-cluster
  • Number of tasks : 1
  • Cluster VPC : select your VPC
  • Subnets : Choose a subnet
  • Security groups : Create new Security Group as needed.
  • Auto-assign public IP : ENABLED
  • Leave all other default options and click on Run Task.

Access Spring Boot REST API

So, To access the Spring boot Rest API, we are using public IP address.

  1. On the selected Cluster Page, Go to Tasks tab.
  2. Select the required Task definition from the list and navigate to the relevant Task Page.
  3. There you will be able to find the Public IP Address of the Task : Make sure to copy the Public IP Address.
  4. Open your favorite browser and paste the Public IP address appending the port 8080 with /{relevant path}.

Here We get the expected results from our Spring boot REST API.

Hope you learned How We can easily deploy our Spring boot REST API on AWS ECS using FARGATE.

Happy Exploring ! :)

--

--