Containerize and deploy spring boot app to aws in 10 mins

In this tutorial, we will create a simple spring boot app, containarize it and deploy to aws. Goal was to create in 10 mins but it took slightly more than 10 mins :smiley:

Demo screencast

Download code

Code can be downloaded from github

Pre-requisites

Create a simple spring boot app

Go to SPRING INITIALIZR and create a simple spring boot app. For this demo, I choose

Generate project and download.

Compile and run the app

 ./gradlew clean build
package org.bpt.demo;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {

    @RequestMapping("/greet")
    public String greet() {
        return "hello";
    }
}

./gradlew clean build
curl localhost:8080/greet

Also check health endpoint

curl localhost:8080/actuator/health

Containerize the spring boot app

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

Build docker images

You can directly build the above dockerfile but we will use Docker Gradle Plugin so that creating java artefacts and building docker image can be done in one single step.

buildscript {
....
	repositories {
        ....
		maven {
			url "https://plugins.gradle.org/m2/"
		}
	}
	dependencies {
        .....
		classpath('gradle.plugin.com.palantir.gradle.docker:gradle-docker:0.13.0')
        .....
	}
}

....
apply plugin: 'com.palantir.docker'
....


docker {
	dependsOn build
	name "${project.group}/${bootJar.baseName}"
	files bootJar.archivePath
	buildArgs(['JAR_FILE': "${bootJar.archiveName}"])
}


./gradlew build docker
docker images -a
docker run -p 8080:8080 -t org.bpt/demo

Use curl commands above and make sure that application is running successfully. You have successfully created docker image of your application. Please see more information on dockerizing spring boot application (here)[https://spring.io/guides/gs/spring-boot-docker/]

Push image to Amazon Elastic Container Registry(ECR)

$(aws ecr get-login --no-include-email --region ap-southeast-2)

If you get below error, add permission policy “AmazonEC2ContainerRegistryFullAccess” for the user.

An error occurred (AccessDeniedException) when calling the GetAuthorizationToken operation: User: arn:aws:iam::708464146667:user/Developer1 is not authorized to perform: ecr:GetAuthorizationToken on resource: *
docker tag org.bpt/demo:latest <repo-url>
docker push <repo-url>

Run the new image in aws

Please check the screencast for demo.

Health endpoint configuration is

CMD-SHELL, curl localhost:8080/actuator/health || exit 1

Conclusion

In this demo, we created, dockerized and deployed a simple spring boot application. In a more realistic example, we will probbaly have an API gateway in front of the ECS cluster for authentication/authorization/api management/ throttling/caching etc. This is to show how easy it is to deploy a spring-boot app to aws.