Vertx is an asynchronuous framework for high load applications, it uses Netty, new I/O and Event bus. Vertx was designed to be used for microservices and a number of vertx instances may run in a cluster for high availability.

ECS is a docker oriented AWS service, which is much simpler than Kubernetes and allows to run docker containers in a cluster.

Here we will create a Vertx cluster, which will run in an ECS cluster, using ECS cluster and service names for node discovery.

Our application will consist of three “ verticles ” (something like an Actor in akka ) — HttpVerticle , which listens to HTTP “health check” requests and sends messages to two other Verticles using the Event bus. The message content will be a string containing an IP address, so by printing our “local” IP address and the content of the received message we can see that a message was sent by another cluster member.

Let’s start, and we will use Kotlin:

Now, the tricky part — creating a Vertx cluster and asking AWS the current IP.

The code of receiving the IP is using the “http://169.254.170.2/v2/metadata" endpoint, which is works in ECS only.

For the Vertx cluster we are using hazelcast as the cluster manager

For hazelcast cluster discovery in ECS we will be using this library. It seems that there is not too much support for this library, it’s still in version 1.0-SNAPSHOT , but it’s doing its job. You can’t find it in public maven repositories, so you need compile it by yourself

ecs-cluster.xml:

Replace the cluster and service names in the example XML file

In this file there is a reference to “com.ikentoo.hazelcast.AwsEcsDiscoveryStrategy” , which must be in the classpath.

Now you need to build your docker image and publish it to your Docker registry.

Dockerfile:

Now — the important part — create an ECS Task Definition and Service. Run this service in at least 2 instances to see how they communicate with each other.

This process is described well in this article, but pay attention to the following:

1. You should assign an AIM role to the ECS task, which contains the ecs:ListTasks and ecs:DescribeTasks permissions

2. Be careful with the task health check. I recommend giving it a 100 sec start period to ensure that the server is started before ECS starts to check it

3. Open in a security group ports 5701 and 15701 (event bus port, hardcoded in sources)

4. Check again that your cluster name and service name are the same as in cluster.xml file

If everything works well, you will see in cloudwatch Logs something like

All source code including a gradle build file you can find here. Attention! It assumes that you have an artefact group: 'com.ikentoo', name: 'hazelcast-aws-ecs', version: '1.0-SNAPSHOT’ in your maven repository!

References:

1. How to get host ip and port from inside ECS container

2. Instance Metadata and User Data

3. How to set up hazelcast in a ECS (EC2 container service) environment?

4. hazelcast-aws-ecs

5. hazelcast-aws — they tell that they support ECS, but I failed to run it

6. Fargate support #86 — here I saw the a reference to hazelcast-aws-ecs

7. Gentle Introduction to How AWS ECS Works with Example Tutorial

8. Tutorial: Creating a Cluster with a Fargate Task Using the AWS CLI

9. Sources for this article