1
votes

We have a product running on AWS and it works on master-slave model. One instance will be acting as a Master/Server and multiple slave instances talking to them.

To run some automated scripts, I need to connect to the Master Server and from there retrieve all other Slave instances IP address.

The catch here is that Master Server Instance has the list of all the Slave Instance's Private IP address. But I need to get their equivalent public IP address.(All master and slave instances do have public IP address)

Question: Given that I have the private IP address of an Instance, is there a way to retrieve the corresponding public IP address from within the Master Server Instance

P.S. I do have these commands to retrieve the local instances IP address but looking for remote instance version

http://instance-data/latest/meta-data/local-ipv4

http://instance-data/latest/meta-data/public-ipv4

2
Does master instance have access to the credentials or IAM role to execute describe-instances?helloV
yes.it does have aws ec2 describe-instances --instance-ids i-xyz --query "Reservations[].Instances[][PublicIpAddress]" [ [ "1.2.3.4" ] ]Siva
See my answer using filters with describe-instanceshelloV

2 Answers

2
votes

One option is to grant the master EC2 privileges via an instance role to ec2:DescribeInstances, and call it with the private-ip-address filter.

From the results the publicIp field can be read to find the slave public IPv4 address.

Since ec2:DescribeInstances doesn't support resource-level permissions you should consider carefully whether this (i.e. the ability to describe properties of all your EC2 instances) is an acceptable level of privileges for the master EC2 to have. Finer-grained permission checking could be implemented in a Lambda function.

1
votes

Use describe-instances with filter private-ip-address

    aws ec2 describe-instances --filters "Name=private-ip-address,Values=10.1.1.100,10.10.0.101"
        --query "Reservations[].Instances[][PrivateIpAddress,PublicIpAddress]" --output text

Output

10.10.0.101 52.123.123.215
10.1.1.100  54.123.123.49