To cluster your Elixir application, you need to tell each node how to find the other nodes.
If you use AWS Auto Scaling Groups, this is a little tricky, as you can’t just hard code IPs - they’ll change any time an instance gets launched.
This post demonstrates one way to work with this.
We’ll use the AWS command line tools to fetch the IPs of other instances in the same Auto Scaling Group
and write them to a
When our application starts, we’ll call
:net_adm.world(), which will use that file to connect our node to the cluster.
We assume distillery is used to build the application and use it’s hook system to update the
before the application starts.
General cluster setup
First, let’s set up the AWS Security Group for Erlang clustering. Open ports 4369 (for the Erlang port mapper daemon) and 9100-9155 (for actual inter node communication). Traffic on those ports should be allowed inside the Security Group.
Next, you need to create a custom
In this, we tell Erlang to actually use the ports we just opened.
Create a file
rel/templates/vm.args.eex with the following contents:
If you already use a custom
vm.args, just add the last line.
rel/config.exs make distillery actually use that file:
Fetching IPs via AWS command line tools
Next we’ll create a script to pipe the IPs of different instances in our Auto Scaling Group into the
Copy the following into
rel/hooks/pre_start and set correct values for REGION and APP_PATH:
Now configure distillery to use that script as
pre_start hook. In
Connect to the cluster on application start
In your applications
start callback in
:net_adm.world() to actually connect to the other nodes:
After all those changes, you should be ready to go.
You can test by running
Node.list() inside a
remote_console on one of your instances.
If it returns a non-empty list, your nodes are connected.