Deploy Java microservices on Amazon ECS using AWS Fargate and Code Pipeline — Part 1

Shylesh Giri
3 min readJul 19, 2022

--

In this article we will see how to deploy your spring boot application on Amazon ECS using Fargate and code pipeline and expose it through an application load balancer.

First in your spring boot project and add a docker file to the root folder. This is required to build a docker image.

Test the docker file using docker build and run command.

docker build -t demo-service:latest .
docker run -p8080:8080 demo-service:latest

Next you need to add a buildspec.yml file. This is required to build your project on AWS using Code Build

The buildspec.yml has instructions to build your java project, build the docker image and push it to your ECR repository. Before we build the application let’s create the docker image repository first.

Amazon ECR is a fully managed container registry offering high-performance hosting, so you can reliably deploy application images and artifacts anywhere.

Got to Elastic Container Registry from your control panel and then click on create repository.

Give it a name and keep other settings as default and then click on ‘Create repository’ button.

Next go to AWS Code Build and create a new build project. Enter the build project name and its description which is optional.

Next configure your source repository. You can connect to your GitHub repository or any other source listed in the dropdown.

Next select the environment for your build project. Select Amazon Linux 2 operating system. This will have pre-installed AWS cli which is required to execute ECR login commands.

Next select the privileged flag to get elevated privileges to your build. Also make note of the role name. We need the role name to give additional permission to build to access ECR repository to push the build image.

Next for buildspec select the option use a buildspec file. This will refer to the build spec file from the repository.

Click on create build project. Before you start the build go to your IAM role and search for the build role name you just created and attach the policy ‘AmazonEC2ContainerRegistryFullAccess’ and ‘AmazonEC2ContainerRegistryPowerUser

Finally go back to your code build project and click on start build. Verify all the build stages are successful and then go to the ECR repository to validate if the image with the latest tag is pushed.

In Part 2 we will see how to deploy your image to ECS using AWS Code Pipeline.

References

--

--

Shylesh Giri

Senior Software Engineer, Java Full Stack developer and an AWS and Google certified professional. https://in.linkedin.com/in/shyleshgiri