node communication and automatic discovery on ec2

by David Weldon

So you’ve signed up for ec2 and figured out how to create instances with erlang installed. Sweet. I’m going to show you how to get your erlang nodes to communicate – first if we know the IP addresses of both machines, and second if we want the nodes to automatically discover each other.

I’m assuming at this point that you have two instances: I1 and I2, which are both members of the security group S. The first thing you need to do is edit the security group settings to allow full port access between all members of S. If you are using the management console, just add a new entry, leave everything blank but add S as the source and hit save. After a refresh it will show that icmp, tcp, and udp are all open for all members of S.

Open ssh connections to I1 and I2 in two terminals. Note the private DNS names of both instances – it’s the hostname followed by “.ec2.internal” (on us-east). For example: ip-10-200-100-50.ec2.internal

On both instances:

$ erl -name test@`hostname`.ec2.internal -setcookie test

If you don’t know what these parameters do, see the distributed programming section from this doc.

Assuming the private DNS name of I2 is ip-10-200-100-50.ec2.internal, on I1 we can do this:

> net_adm:ping('test@ip-10-200-100-50.ec2.internal').

If you received a pong message, congratulations – your nodes know about each other and can now communicate! Try running nodes() on each.

Next let’s have a look at doing this in a more automated way. Leave I2 running, but quit out of the erl shell on I1 and install Eric Cestari’s ec2nodefinder. As his README points out, you will need to either set the environment variables AMAZON_ACCESS_KEY_ID and AMAZON_SECRET_ACCESS_KEY, or you will need to edit the ec2nodefinder.app file in the ebin directory. Be careful of the commas in the app file if you go that route – if the file isn’t syntactically correct, the app won’t start. I like Eric’s version of ec2nodefinder better than the original version from the dukes, because it’s easier to install and it doesn’t rely on ec2-describe-instances.

Start your node on I1 again:

erl -name test@`hostname`.ec2.internal -setcookie test

You can now do:

> ec2nodefinder:start().

> ec2nodefinder:discover().

Which, if successful, will return something like:

{ok,[{'test@ip-10-200-100-50.ec2.internal',pong}]}

Calling nodes() on either node should again show they are connected. Awesome. Now you can do all kinds of crazy stuff like have nodes which automatically become part of an mnesia cluster when they start.