Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving the IP address of an EC2 instance given an instance ID

Using the aws CLI, how can I retrieve the private IP address of an EC2 instance given its instanceID?

When I do:

aws ec2 describe-instance-status --instance-ids <instance_ID>

I get other information, but not the private IP addresses such as:

{
    "InstanceStatuses": [
        {
            "InstanceId": "XXXXX", 
            "InstanceState": {
                "Code": 16, 
                "Name": "running"
            }, 
            "AvailabilityZone": "us-east-1a", 
            "SystemStatus": {
                "Status": "ok", 
                "Details": [
                    {
                        "Status": "passed", 
                        "Name": "reachability"
                    }
                ]
            }, 
            "InstanceStatus": {
                "Status": "ok", 
                "Details": [
                    {
                        "Status": "passed", 
                        "Name": "reachability"
                    }
                ]
            }
        }
    ]
}
like image 374
Amelio Vazquez-Reina Avatar asked Oct 07 '14 19:10

Amelio Vazquez-Reina


People also ask

How do I find the instance ID of an EC2 instance?

Simply check the var/lib/cloud/instance symlink, it should point to /var/lib/cloud/instances/{instance-id} where {instance_id} is your instance-id.


2 Answers

Try describe-instances instead. Private IP Address isn't returned with describe-instance-status because that command describes system and instance status, primarily concerning itself with hardware/issues or scheduled events.

Per the "Output" section of the describe-instances documentation, part of the output of describe-instances is a string PrivateIpAddress.

Example usage:

aws ec2 describe-instances --instance-ids <instance_ID>
like image 184
Anthony Neace Avatar answered Nov 02 '22 05:11

Anthony Neace


To get ALL private IP addresses:

aws ec2 describe-instances --instance-ids ${INSTANCE_ID} |\
jq -r '.Reservations[].Instances[].NetworkInterfaces[].PrivateIpAddress'

or

aws ec2 describe-instances --instance-ids ${INSTANCE_ID} |\
jq -r ".Reservations[]" | grep PrivateIpAddress |\
egrep -o "([0-9]{1,3}\.){3}[0-9]{1,3}" | sort -u
like image 34
LPby Avatar answered Nov 02 '22 03:11

LPby