Sunday, March 22, 2009

Amazon EC2 – Communicating amoung instances

These days I am doing some cool work with Amazon EC2, so there were a lot to learn and a lot to find out. One of the major challenge I faced was to communicating between instances. For my application I need to do “ssh” and “scp” among the nodes, and most of those taking place automatically. So I had hard time figuring out this, the main reason is in EC2 if you want to connect to an instance you have to give your key-pair. Which is not possible for my application. So what I did was following.
  • Run an instance with the your operating system (in my case I choose Fedora 8), when you run select the key-pair and security group.
  • Next you need to generate SSH key, you can do that using following command (Do not enter a passphrase)
  • ssh-keygen -t rsa
  • Now you need to go and edit the ~/.ssh/authorized_keys , For that copy the content of .ssh/id_rsa.pub and paste
  • Now open the ~/.ssh/authorized_keys, and change the tail to be “root@domU” , basically you need to remove root@ec2-.. with new one
  • Then you need to edit the “~/.ssh/know_hosts” file and add the following,
  • *.amazon.com ssh-rsa Asasdas....
  • (Asasdas.... = content of rsa.pub)

Now you are all set (unless you need to install some other packages), next step is to bundle your image and register that with EC2 and S3. Then try running more than one instances, now if you try to do ssh among those you will find that you do not need to type the password, or you do not need to specify the key-pairs.

2 comments:

Scott said...

Could you give more thorough directions? What if the servers had different hostnames?

LaWFulEviL said...

How will this work if the instances are spawned dynamically for scaling?

KeyPair is the only way.