Listing AWS EC2 instance information ℹ️ with AWS CLI v2

For example, to get list of AWS EC2 instances with their private IP address, instance ID and their name, do this -

$ aws ec2 describe-instances \
  --output json \
  | jq '.Reservations[].Instances[] | { Id: .InstanceId, IP: .PrivateIpAddress, Name: .Tags[] | select(.Key == "Name") | .Value }' \
  | jq -s

The output will look like this -

[
  {
    "Id": "i-11223344556677889",
    "IP": "10.59.96.57",
    "Name": "webserver-01"
  },
  {
    "Id": "i-11223344556677880",
    "IP": "10.59.25.149",
    "Name": "webserver-02"
  },
  {
    "Id": "i-11223344556677881",
    "IP": "10.59.30.131",
    "Name": "webserver-03"
  },
  {
    "Id": "i-11223344556677882",
    "IP": "10.59.95.108",
    "Name": "webserver-04"
  },
  {
    "Id": "i-11223344556677883",
    "IP": "10.59.43.236",
    "Name": "webserver-05"
  },
  {
    "Id": "i-11223344556677884",
    "IP": "10.59.99.39",
    "Name": "webserver-06"
  },
  {
    "Id": "i-11223344556677885",
    "IP": "10.59.99.12",
    "Name": "webserver-07"
  }
]

You can also use the following to get more values from tags - as object and as array

Tags as object will look like this -

$ aws ec2 describe-instances \
  --output json \
  | jq '.Reservations[].Instances[] | { Id: .InstanceId, IP: .PrivateIpAddress, Tags: .Tags[] | select(.Key == "Name") }' \
  | jq -s

The output will look like this -

[
  {
    "Id": "i-11223344556677889",
    "IP": "10.59.96.57",
    "Tags": {
      "Key": "Name",
      "Value": "webserver-01"
    }
  },
  {
    "Id": "i-11223344556677880",
    "IP": "10.59.25.149",
    "Tags": {
      "Key": "Name",
      "Value": "webserver-02"
    }
  },
  {
    "Id": "i-11223344556677881",
    "IP": "10.59.30.131",
    "Tags": {
      "Key": "Name",
      "Value": "webserver-03"
    }
  },
  {
    "Id": "i-11223344556677882",
    "IP": "10.59.95.108",
    "Tags": {
      "Key": "Name",
      "Value": "webserver-04"
    }
  },
  {
    "Id": "i-11223344556677883",
    "IP": "10.59.43.236",
    "Tags": {
      "Key": "Name",
      "Value": "webserver-05"
    }
  },
  {
    "Id": "i-11223344556677884",
    "IP": "10.59.99.39",
    "Tags": {
      "Key": "Name",
      "Value": "webserver-06"
    }
  },
  {
    "Id": "i-11223344556677885",
    "IP": "10.59.99.12",
    "Tags": {
      "Key": "Name",
      "Value": "webserver-07"
    }
  }
]

Tags as array will look like this -

$ aws ec2 describe-instances \
  --output json \
  | jq '.Reservations[].Instances[] | { Id: .InstanceId, IP: .PrivateIpAddress, Tags: [.Tags[] | select(.Key == "Name")] }' \
  | jq -s

The output will look like this -

[
  {
    "Id": "i-11223344556677889",
    "IP": "10.59.96.57",
    "Tags": [
      {
        "Key": "Name",
        "Value": "webserver-01"
      }
    ]
  },
  {
    "Id": "i-11223344556677880",
    "IP": "10.59.25.149",
    "Tags": [
      {
        "Key": "Name",
        "Value": "webserver-02"
      }
    ]
  },
  {
    "Id": "i-11223344556677881",
    "IP": "10.59.30.131",
    "Tags": [
      {
        "Key": "Name",
        "Value": "webserver-03"
      }
    ]
  },
  {
    "Id": "i-11223344556677882",
    "IP": "10.59.95.108",
    "Tags": [
      {
        "Key": "Name",
        "Value": "webserver-04"
      }
    ]
  },
  {
    "Id": "i-11223344556677883",
    "IP": "10.59.43.236",
    "Tags": [
      {
        "Key": "Name",
        "Value": "webserver-05"
      }
    ]
  },
  {
    "Id": "i-11223344556677884",
    "IP": "10.59.99.39",
    "Tags": [
      {
        "Key": "Name",
        "Value": "webserver-06"
      }
    ]
  },
  {
    "Id": "i-11223344556677885",
    "IP": "10.59.99.12",
    "Tags": [
      {
        "Key": "Name",
        "Value": "webserver-07"
      }
    ]
  }
]

Annexure

Versions of all the softwares used -

$ aws --version
aws-cli/2.15.40 Python/3.11.9 Darwin/23.4.0 source/x86_64 prompt/off

$ jq --version
jq-1.7