This sample project deploys a Java 21 Spring Boot application on Amazon ECS using AWS Fargate with Application Load Balancer to route traffic between two ECS Tasks.
- Create an AWS account if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources.
- AWS CLI installed and configured
- Git Installed
- AWS Serverless Application Model (AWS SAM) installed
- Java 21 or above installed
- Maven 3.8.6 or above installed
- Docker installed
- jq installed
-
From the command line, execute the below command to build the project.
mvn clean package
-
(Optional) Run the application on local box using the below command:
mvn spring-boot:run
-
Get the right settings in place:
- Set your AWS account # here
aws_account=YOUR AWS ACCOUNT
-
Please ensure that docker is up and running before executing this step. From the command line, run the below command
-
to create ECR repository, build the docker image and push it into the ECR repository.
-
and also to create common cloudformation stacks:
- vpc (vcp.yml)
- iam (iam.yml)
- ecs cluster (cluster.yml)
bash build.sh
if you get this error (can happen w/ Apple M1-M3):
ERROR: Multi-platform build is not supported for the docker driver. Switch to a different driver, or turn on the containerd image store, and try again. Learn more at https://docs.docker.com/go/build-multi-platform/
please run first:
docker buildx create --name multiarch --driver docker-container --use
-
-
Run the below command to deploy the AWS resources for the pattern as specified in the template.yml file:
sam deploy --guided
or just...
./deploy.sh
After the first run SAM will save your preferences in a .toml file by default with the name samconfig.toml
-
During the prompts:
- Enter a stack name
- Enter the same as AWS Region that you provided while building the image.
ImageURI
Enter the image URI obtained from the previous output (from build.sh).- Allow SAM CLI to create IAM roles with the required permissions.
- Keep default values to the rest of the parameters.
If you have run
sam deploy --guided
mode once and saved arguments to a configuration file (samconfig.toml), you can usesam deploy
in future to use these defaults. -
Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for next step as well as testing. (More about SAM config file here)
This sample project deploys a Java 21 Spring Boot application on Amazon ECS Fargate with Application Load Balancer to route traffic between two ECS Tasks. The application exposed REST APIs which can be accessed over HTTP.
ECS Architecture zoom in diagram below:
Here's a breakdown of the steps:
-
Amazon Elastic Container Registry: The repository of the container image of the Spring Boot Application.
-
Application Load Balancer: Receives the HTTP request which routes the traffic to one of the Amazon ECS Tasks.
-
Amazon Elastic Container Service: Amazon ECS Cluster is configured with one service having 2 Tasks. The 2 Tasks runs the Spring Boot application as container. The container image is retrieved from the Amazon Elastic Container Registry.