'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 1:[1]
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" ] }
Solution 2:[2]
In general, it is best to avoid referring to other resources by their IP address. For example, an instance might fail and you might then replace it with a new instance, that would receive a different IP address.
It would be advisable, instead, to assign a DNS name to an instance and refer to it via that DNS name. You could define an Amazon Route 53 private Hosted Zone within the CloudFormation template, and create Record Sets to assign a name to each of the 3 EC2 instances.
This way, the instances themselves don't need to know the IP addresses and there is no need to 'send' the IP addresses to the other instances, nor any need to store the IP addresses on each instance.
See:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ThomasVdBerge |
| Solution 2 | John Rotenstein |
