In this blog, we describe How to Build and Push the Spring-boot docker image to AWS ECR and deployed it to an ECS container using AWS CodePipeline. We will build a sample spring-boot Application, push the image to AWS ECR and then deploy it to AWS ECS.
Prerequisites:
1. AWS Account
2. GitHub Account
Create an AWS ECR repository
Login to your AWS account and create an Amazon Elastic Container Registry repository with a name.
Create a cluster inside AWS ECS and select the cluster template as “Networking Only” because we use AWS FARGATE here.
Create a Task Definition
Select launch type as FARGATE, select task role as “ecsTaskExecutionRole”, Select your operating system, select Task memory and CPU, and Add container with container name and the image should be the ECR repository URI.
Create a service inside the cluster
Click on Clusters, then select our cluster, create a service with a service name, and select launch type as FARGATE.
Create a “buildspec.yml” file on the project repository
version: 0.2
phases:
pre_build:
commands:
- mvn clean install
- echo Logging in to Amazon ECR...
- aws --version
- $(aws ecr get-login --region $AWS_DEFAULT_REGION --no-include-email)
- REPOSITORY_URI=788155875213.dkr.ecr.us-east-2.amazonaws.com/demospringboot
- 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 Jar file
- echo Building the Docker image...
- 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":"spring-container","imageUri":"%s"}]' $REPOSITORY_URI:$IMAGE_TAG > imagedefinitions.json
- cat imagedefinitions.json
artifacts:
files:
- imagedefinitions.json
- target/spring-boot-ecs.ja
Edit pom.xml file and add the line <finalName>spring-boot-ecs</finalName>
It indicates that the name of the generated artifact will be spring-boot-ecs. This name is often used to create a JAR or WAR file, which can then be deployed to a server or container, such as Amazon Elastic Container Service (ECS), to run a Spring Boot application.
Create Dockerfile on the project repository
FROM openjdk:18-jdk-slim
EXPOSE 8080
ADD target/spring-boot-ecs.jar spring-boot-ecs.jar
ENTRYPOINT ["java","-jar","/spring-boot-ecs.jar"]
Go to AWS CodePipeline and create a Pipeline
Click on create new pipeline → Enter the pipeline name → Next
Select source provider as GitHub → Connect to GitHub → Select the repository of our project → Select branch → Next
Select the build provider as AWS CodeBuild → Create a project with the project name, operating system and also tick the below field
Select the Deploy provider as AWS ECS → Select cluster name and service name.
Create Pipeline.
Click on the pipeline and we can see the process
Here the image is pushed into the AWS ECR repository and deployed this image into AWS ECS. After the deployment is completed move to the AWS service inside our cluster and click on the task we get a public IP, copy this IP, and search into a browser.