In this tutorial I will deploy a simple Java Rest application to AWS ECS. I will use the new command line tool ecs-cli-v2. You can follow the video or continue reading.
Download and unpack the ecs cli (v0.0.7 as of this writing). Put it on the path. Open a command line and test if the tool works:
ecs-preview --version
ecs-preview version: v0.0.7
Clone the tutorial application and change to the created directory. Checkout the ecs-preview-tut branch.
git clone https://github.com/rmortale/javalin-rest.git
cd javalin-rest
git checkout ecs-preview-tut
This is a simple Java REST Service:
package ch.dulce;
import io.javalin.Javalin;
public class HelloWorld {
public static void main(String[] args) {
Javalin app = Javalin.create().start(7000);
app.get("/", ctx -> ctx.result("Hello World"));
}
}
To build and test it localy use:
docker build -t javalinrest .
docker run -it -p 7000:7000 javalinrest
curl localhost:7000
Hello World
The following steps create multiple resources in AWS that generate costs. Please follow the steps at the end to delete all resources.
ecs-preview init --project javalin \
--app api \
--app-type 'Load Balanced Web App' \
--dockerfile './Dockerfile' \
--port 7000 \
--deploy
This will create a VPC, Application Load Balancer, an Amazon ECS Service with the sample app running on AWS Fargate. At the end you’ll get a URL for the sample app running!
Deployed api, you can access it at http://javal-Publi-DVHYFO5SU3DF-175675924.eu-west-1.elb.amazonaws.com
curl http://javal-Publi-DVHYFO5SU3DF-175675924.eu-west-1.elb.amazonaws.com
Hello World
Our service is up and running.
As mentioned before, the AWS resources we created are not free. So to clean them up, we can run the following:
ecs-preview project delete
In this post i demonstrated the new ecs-cli-v2. With one command we created a Fargate Cluster and deployed our sample app to it.