Deploy Spring Boot Docker App Through AWS Complete CI/CD Process (Codepipeline, CodeBuild, ECR, ECS)

Priyank Agarwal
7 min readNov 3, 2021

Based on the below diagram, we will set up a complete CI/CD process.

Before that, Below I have mentioned the role of each AWS service that we will use in this article.

  1. CodePipeline: This service will create a pipeline by combining all the above services and managing the CI/CD process.
  2. CodeBuild: AWS CodeBuild is used to build the code. To build the code, AWS search for a buildspec yml file.
  3. ECR (Elastic Container Registry): Amazon Elastic Container Registry (ECR) is a fully managed container registry that makes it easy to store, manage, share, and deploy your container images and artifacts anywhere.
  4. ECS (Elastic Container Service): This service pulls the docker image from ECR and deploys the docker image in EC2 or as serverless (Fargate). Here, in this tutorial, we will use the Fargate type launch.

Here, this complete task we divided into multiple subtasks. It would be easier for us to follow.

  1. Manage your source code.
  2. Manage your docker image by ECR.
  3. Build a docker image.
  4. Docker image deployment by ECS.
  5. CodePipeline to automate a process.
  • Manage your source code

As source code, you need to store the code in a repository. Here, I am using GitHub to store the code.

If you want to clone, you can use this CloneURL: https://github.com/priyank333/spring-boot-sample-app.git

  • Manage your docker image by ECR

Go to the ECR console in AWS and create a repository. You need to specify a repository name.

ECR (Create Repository)
  • Build a docker image.

To build a Docker image, CodeBuild uses a project buildspec file. Buildspec file defines the command that needs to be followed by CodeBuild. In the Buildspec file, we specify a command for building a docker image and pushing newly image to ECR, and generating an imagedefinitions.json file. imagedefinitions.json file will pass to ECS that contains imageURI for newly created docker image.

buildspec.yml file.

version: 0.2
phases:
pre_build:
commands:
- echo Logging in to Amazon ECR....
- aws --version
- $(aws ecr get-login --no-include-email --region ap-south-1)
# Get commit hash value to tag the image
- COMMIT_HASH=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)
- IMAGE_TAG=${COMMIT_HASH:=latest}
# Replace with your application name
- APPLICATION_NAME=spring-boot-sample-app
# Replace with this 435320931996.dkr.ecr.ap-south-1.amazonaws.com to your repository URI
- REPOSITORY_URI=435320931996.dkr.ecr.ap-south-1.amazonaws.com/$APPLICATION_NAME
build:
commands:
- echo Build started on `date`
- echo Building the Docker image...
- docker build -t $APPLICATION_NAME:$IMAGE_TAG .
- docker tag $APPLICATION_NAME:$IMAGE_TAG $REPOSITORY_URI:$IMAGE_TAG
post_build:
commands:
- echo Build completed on `date`
- echo pushing to repo
- docker push $REPOSITORY_URI:$IMAGE_TAG
- echo Writing image definitions file...
# Give your container name
- printf '[{"name":"SpringBootAppContainer", "imageUri":"%s"}]' $REPOSITORY_URI:$IMAGE_TAG > imagedefinitions.json
artifacts:
files:
- imagedefinitions.json

For this, you need to create a CodeBuild project. So follow the steps.

  1. Open CodeBuild and Create Project. Give your project name in CodeBuild.
CodeBuild (Give Project Name)

2. Define your source. In this tutorial, I am taking code from GitHub.

CodeBuild (Source)

3. For environment options, choose the below options. This option tells, On which environment code is needed to build. Here we choose Amazon Linux 2 and the latest image. In the privilege section, you need to check the option. That gives extra privilege for docker image in the build process. After this, you need to choose Service Role. Service Role needs access to pull, push the image from ECR. If a role is not there, you need to create a role by taking help from IAM. Make sure that your role should have the below access. That access for ECR pulling and pushing the image.

{
"ecr:BatchGetImage",
"ecr:BatchCheckLayerAvailability",
"ecr:CompleteLayerUpload",
"ecr:GetDownloadUrlForLayer",
"ecr:InitiateLayerUpload",
"ecr:PutImage",
"ecr:UploadLayerPart",
"ecr:GetAuthorizationToken"
}
CodeBuild (Build Environment)

4. In the Last stage, choose Artifacts where your built code will store. After this create the CodeBuild project.

CodeBuild (Code Artifact)
  • Docker image deployment by ECS

ECS deploys docker images into the instances. Here, we are using a fargate type launch. Follow the steps to create an ECS cluster.

  1. Go to ECS and create a cluster. Select Networking Only cluster template for fargate type. After this give the cluster name and create it.
ECS (Cluster Type)

Each cluster has multi-tasks. Task define your application. You need to create task definitions. In task definitions you need to define all those things related to your docker configuration. Ex. Docker Image URI, Port Mapping, Memory, and CPU requirements. Follow the next step to create a task.

1. Go to Task Definitions and create a new task definition. Choose Fargate type.

ECS (Task Type)

2. Give your task name and choose the IAM role for pulling an image from ECR. If you have not created then AWS will create it for you leave it empty.

ECS (Task Definition)

3. Select task size. Here, I choose 1GB Memory and 0.5 VCPU.

ECS (CPU and Memory usage)

4. Next step, Here you need to define container definition. Define your container name, image URI (Received from ECR). Next define soft limit (Additional memory, It will use additionally based on usage). Soft Limit is optional. Last define port mapping on which you want to do a map.

ECS (Container Definition)

Up to here, We created a task. Now we need to create a service that runs in a cluster. Follow the next steps to create a service.

  1. Go to the Service section and click on create a service. Provide service details. Select the below configuration in an image for service.
ECS (Service Definition)

2. Next step, Choose your VPC and Security Group. If you want to make as public then choose public subnets. After this, If you want to create a load balancer you can create but here I am not creating any load balancer.

ECS (VPC and Security Group)

3. Last, Review your configuration and create. In a cluster, You can see the task is stopped because ECS is pulling an image from ECR but still we have not pushed any image to ECR. We will push the image through the pipeline. Next step we will create CodePipeline by combining all steps.

  • CodePipeline to automate a process.

This pipeline will automate the complete process. Follow the next steps to create a CodePipeline.

  1. Go to the AWS console and create CodePipeline. Give pipeline name and create a new service role if it doesn’t exist.
CodePipeline (Pipeline Name)

2. As source code, You can choose as per your source provider. For me source provider is GitHub. After choosing the source provider to choose the branch that needs to build.

CodePipeline (Source Provider)

3. Next stage is building. Here, We need to add the CodeBuild project that earlier in this article we created. This section will build a docker image. Choose your CodeBuild Project.

CodePipeline (Building Docker Image)

4. Next stage is for Deployment, Here choose ECS for deployment. You need to choose the cluster name and service name that you have created earlier in this article.

CodePipeline (Deploy Docker Image)

Review your CodePipeline configuration and create.

After creating CodePipeline, Pipeline will trigger and start the process for pulling code, building images, and deploying as serverless (Fargate).

As you can see pipeline is executed successfully.

CodePipeline (Successfully Executed)

Here, We have not defined a load balancer. To get access to service you need to go to ECS and under you will get a task and there you will get public IP. By public IP you can test your application.

Task Information

You can ping the service by following endpoint: http://65.2.38.215:8777/ping

Replace IP with your newly created.

We created a complete CI/CD process for docker image deployment.

For reference, You can pull code from GitHub.

Clone URL: https://github.com/priyank333/spring-boot-sample-app.git

In Code, you will get DockerFile, buildspec file and sample java spring boot app.

If you want to know how to create docker image of spring boot app, you can follow this article :> Build Docker Image Of Spring Boot

--

--