How to get the details which Instance is using particular Elastic IP (EIP)
Tags: aws, devops, linux
Published: 2020-01-24 17:24:34

<b> Scenario:</b>
How to get the details of Instance using particular Elastic IP (EIP) - How to get the details of Instance using particular Elastic IP (EIP).

<b><i> For example:
I have an EIP with me and I don't know where its located ?</b></i> 

<b>Answering to - "I don't know where its located"<b> : 
- You can use **"nslookup"** or **"dig -x"** command to check the **"aws region"** where EIP is located.

**Example:**
<p class="MsoNormal"></p><pre># <b>dig -x 52.xxxxxxxx +short<br></b>ec2-52xxxxxxxxxxx<b>us-west-2</b>.compute.amazonaws.com.</pre>

<pre>C:\Users\administrator&gt; <b>nslookup 52.xxxxxxxxxxx<br></b>Server:&nbsp; recycled-xxxxxxxxxx.xxxx<br>Address:&nbsp; xx.xx.xx.xx<br>Name:&nbsp; &nbsp; ec2-52-xxxxxxxxxxx.<b>us-west-2</b>.compute.amazonaws.com<br><b>Address:&nbsp; 52.xxxxxxxx</b></pre>

<b>This confirms the association of domain with EIP and region of the resource is</b> 

<pre><p class="MsoNormal"></p>(us-west-2)</pre>

<b>Now, Let's figure out where or to which instance it has been attached to ??</b>
**==========================================================**
**The only way to get the details of EIP address associated with your Instance in a particular account** is to login in the existing account and check it and you have two methods to do so:

**1). Using console:-**

- Go to EC2 - Navigate to **"Elastic IP"** section on left or click [here][1] 
- Then you can see the EIP details and associations with an Instance. For more details , please check :

[+] https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/elastic-ip-addresses-eip.html
[+] https://aws.amazon.com/premiumsupport/knowledge-center/vpc-find-owner-unknown-ip-addresses/


**2). Using AWS CLI:-**
You can use CLI command with API's **([describe-addresses][2] & [describe-instances][3])** to get the details of an EIP and its associations. I'm providing you an example below which you can refer.

**Note :** Please make sure you replace the **EIP** & **Region**  details according to your resources where they are located in. 

**I've tested this as follows:**

➜ Using (describe-addresses) you can get the info : ( AWS CLI command below)

    # aws ec2 describe-addresses --public-ips YourEIPAddressHere --region us-west-2 --query "Addresses[].[PublicIp,PrivateIpAddress,InstanceId,AllocationId,AssociationId]" --output table

**Example:**
<p class="MsoNormal"><br></p><pre># aws ec2 describe-addresses <b>--public-ips </b>52.xxxxxxxxx --region us-west-2 --query "Addresses[].[PublicIp,PrivateIpAddress,InstanceId,AllocationId,AssociationId]" --output table<br>----------------------------------------------------------------------------------------------------------------------<br>|&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <b>&nbsp; &nbsp; DescribeAddresses&nbsp; &nbsp; &nbsp; &nbsp; </b>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|<br>+---------------+---------------+----------------------+------------------------------+------------------------------+<br>|&nbsp; <b>52.xxxxxxxxxxxxx</b>|&nbsp; 172.xx.xx.xx |&nbsp; <b>i-0axxxxxxxxxxx</b>|&nbsp; <b>eipalloc-0fxxxxxxxxx</b>&nbsp; |&nbsp; <b>eipassoc-08xxxxxxxx</b>&nbsp; |<br>+---------------+---------------+----------------------+------------------------------+------------------------------+</pre>

Also, 
➜ Using (describe-instances) you can get the info : (AWS CLI command below)

    # aws ec2 describe-instances --region us-west-2 --filter Name="ip-address",Values="YourEIPAddressHere" |grep 'InstanceId\| YourEIPAddressHere'

**Example:**
<p class="MsoNormal"><span style="font-size: 10.5pt; line-height: 107%; font-family: Arial, sans-serif; color: rgb(68, 68, 68); background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;"></span></p><pre><font color="#444444" face="Arial, sans-serif"># aws ec2 describe-instances --region us-west-2 <b>--filter Name="ip-address",</b>Values="52.xxxxxxxxxxxxxxx" |grep 'InstanceId\|52.xxxxxxxxxxxx'<br></font><font color="#444444" face="Arial, sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <b>"InstanceId": "i-0axxxxxxxxxxx",</b><br></font><font color="#444444" face="Arial, sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "PublicDnsName": "ec2-52-xxxxxxxxxxx.us-west-2.compute.amazonaws.com",<br></font><font color="#444444" face="Arial, sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <b>&nbsp; "PublicIpAddress": "52.xxxxxxxxxxxxx",</b><br></font><font color="#444444" face="Arial, sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "PublicDnsName": "ec2-52-xxxxxxxxxxx.us-west-2.compute.amazonaws.com",<br></font><font color="#444444" face="Arial, sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "PublicIp": "52.xxxxxxxxxxxxxxxxxx"<br></font><font color="#444444" face="Arial, sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "PublicDnsName": "ec2-52-xxxxxxxxxxx.us-west-2.compute.amazonaws.com",<br></font><font color="#444444" face="Arial, sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "PublicIp": "52.xxxxxxxxxxxxxx"</font></pre>


To know more on filters , you can refer to our CLI reference guide on these APIs.

**[+] describe-instances** :- https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instances.html

**[+] describe-addresses** :- https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-addresses.html


**Thanks**
**Dinesh Rathee**


  [1]: https://console.aws.amazon.com/ec2/v2/home?#Addresses:sort=PublicIp
  [2]: https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-addresses.html
  [3]: https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instances.html