In this tutorial I will distribute a simple Java Rest application to AWS Fargate. I will use fargatecli. You can follow the video or continue reading.
Download and unpack the latest fargatecli release (0.3.2 as of this writing). Adjust the path so that fargatecli can be called from everywhere. Open a command line and test if fargatecli works:
fargate --version
fargate version 0.3.2
Clone the tutorial application and change to the created directory:
git clone https://github.com/rmortale/javalin-rest.git
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.
According to the documentation, the Security Group should be created automatically. Since that didn’t work for me, we create a group using the aws-cli.
aws2 ec2 create-security-group --group-name fargate-cli-sg --description "Group for fargatecli tutorial"
{
"GroupId": "sg-043ab03ec99b60369"
}
For our application we have to open port 7000. Use the GroupId returned from the previous command.
aws2 ec2 authorize-security-group-ingress --group-id sg-043ab03ec99b60369 --protocol tcp --port 7000 --cidr 0.0.0.0/0
Now we can distribute the application (Use the region where you want to create the resources and where the security group above is).
fargate service create --port http:7000 --region eu-west-1 --security-group-id sg-043ab03ec99b60369 javalin-app
...
[i] Created service javalin-app
The above command creates the following resources:
fargate service list --region eu-west-1
Show more information about the Service:
fargate service info javalin-app --region eu-west-1
34.244.147.151
The above command also shows the IP of the running task. To test it we use curl:
curl 34.244.147.151:7000
Hello World
Our service is deployed and responding.
As mentioned before, the AWS resources we created are not free. So to clean them up, we can run the following:
fargate service scale --region eu-west-1 javalin-app 0
Scaled service javalin-app to 0
fargate service destroy --region eu-west-1 javalin-app
Destroyed service javalin-app
In this post i demonstrated the fargatecli. With a few commands we created a Fargate Cluster and a Service with one Task. With fargatecli it is also possible to manage Load Balancers, Certificates and more. More about that in another post.