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
Code can be downloaded from github
Go to SPRING INITIALIZR and create a simple spring boot app. For this demo, I choose
Generate project and download.
./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
http://localhost:8080/greet
You should see the message “hello”curl localhost:8080/greet
Also check health endpoint
curl localhost:8080/actuator/health
Dockerfile
with following contentFROM 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"]
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.
build.gradle
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/]
Go to aws console and create a new registry spring-boot-demo
Get login to 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>
Please check the screencast for demo.
Create container definition
Create task definition
Create Service
Configure cluster and run it.
Health endpoint configuration is
CMD-SHELL, curl localhost:8080/actuator/health || exit 1
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.