Search code examples
amazon-web-servicesamazon-ec2ip-address

What is best way to get internal IP address of Amazon EC2 instances?


Please check following scenario:

  • I am using a CloudFormation template to create 3 Amazon EC2 instances.
  • In the template I am using an Auto Scaling group for launching 3 instances.
  • I am not assigning a Public IP address - only a private IP address will assigned to instances.

I want to make a cluster of 3 instances, so the 3 instances need to know the private IP addresses of each other.

What is the best way to get the IP addresses of other 2 instances in 3rd instance? Shall I keep a small script inside the AMI which will throw the IP address outside?


Solution

  • In cloudformation, you can make use of the GetAtt intrinsic function, which returns the value of an attribute from a resource in the template.

    To get the PrivateIp, it would look like this:

    { "Fn::GetAtt" : [ "Ec2Instance", "PrivateIp" ] }