Deploy Spring Boot Application to AWS Fargate Part 1

Sajith vijesekara
Towards AWS
Published in
3 min readMay 26, 2021

--

In this tutorial, We are going to discuss how to Deploy docker images to AWS ECR Registry. For That, we are going to use AWS code Build.

Step 1

Create Docker Image for Deploy to ECR. For This one, I have written a sample spring boot project which needs to deploy to ECR. For This scenario, We are going to write a spring boot application that connects through dynamoDb & return some data.

As the first Step, We are going to deploy the Spring boot application in to Fargate.

Create the Following two files in Spring Boot Application.

Dockerfile

FROM openjdk:11.0.4-jre-slim

ADD target/medium-fargate-demo-0.0.1-SNAPSHOT.jar medium-fargate-demo.jar

ENTRYPOINT ["java","-jar","medium-fargate-demo.jar"]
EXPOSE 8080

The Next Step Is to add the buildspec.yml file to the root directory.

version: 0.2


phases:
pre_build:
commands:
- echo Logging in to Amazon ECR...
- aws --version
- $(aws ecr get-login --region $AWS_DEFAULT_REGION --no-include-email)
- REPOSITORY_URI={REPO_URL}
- COMMIT_HASH=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)
- IMAGE_TAG=build-$(echo $CODEBUILD_BUILD_ID | awk -F":" '{print $2}')

build:
commands:
- echo Build started on `date`
- echo Building the Docker image...
- mvn package
- docker build -t $REPOSITORY_URI:latest .
- docker tag $REPOSITORY_URI:latest $REPOSITORY_URI:$IMAGE_TAG
post_build:
commands:
- echo Build completed on `date`
- echo Pushing the Docker images...
- docker push $REPOSITORY_URI:latest
- docker push $REPOSITORY_URI:$IMAGE_TAG
- echo Writing image definitions file...
- printf '[{"name":"nodeapp","imageUri":"%s"}]' $REPOSITORY_URI:$IMAGE_TAG > imagedefinitions.json

cache:
paths:
- '/root/.m2/**/*'

Now We have to create an Instance in ECR. And Need to update REPO_URL value in the above yml file.

Create Repository in ECR
Copy URl in Here

Step 2

The next part is to configure AWS CodeBuild with your git repository. So We can start CI/CD pipeline which will automatically deploy your Docker Image to ECR.

AWS CodeBuild is expensive so you have to minimize the code build time. So to prevent this issue I will build Image in the release branch only.

Sync with Github project Account
make sure to add HEAD_REF as release branch

And Next step is enable build docker image

And finally, Add this one.

we make sure add buildspec.yml file in project root directory

And It will create CodeBuild as this way.

And final Step is add this step.

Go to IAM -> Roles

You can see now these is new role created.

And You have to attach following policies to this role.

AmazonEC2ContainerRegistryFullAccess

AmazonEC2ContainerRegistryPowerUser

Step 3

Here You have to Commit your changes & push it to release branch.

push docker image to ECR

Now our first task is finished you can commit your changes to the release branch & it will build a docker image & deploy it to AWS ECR.

Next thread we will discuss how to create a Fargate instance which uses the AWS ECR images.

--

--

Technical Lead. Passionate about cloud computing & web security | Freelance Mobile Developer| CKAD | AWS Community Builder 🇱🇰