Amelio Vazquez-Reina
Amelio Vazquez-Reina

Reputation: 96468

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"
                    }
                ]
            }
        }
    ]
}

Upvotes: 5

Views: 3270

Answers (3)

Thomas Crowley
Thomas Crowley

Reputation: 1108

You can do this using the query option.

aws ec2 describe-instances --instance-ids ${INSTANCE_ID} --query Reservations[].Instances[].NetworkInterfaces[].PrivateIpAddress

This will just return the private IPAddress and no other output information

Upvotes: 0

LPby
LPby

Reputation: 529

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

Upvotes: 5

Anthony Neace
Anthony Neace

Reputation: 26031

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>

Upvotes: 6

Related Questions