Skip to content

Instantly share code, notes, and snippets.

@jaypipes
Created June 25, 2019 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaypipes/66f8e740a66d5cd43fc40f27c676baa4 to your computer and use it in GitHub Desktop.
Save jaypipes/66f8e740a66d5cd43fc40f27c676baa4 to your computer and use it in GitHub Desktop.
Grab instance ID, name, public and private IP address for EC2 instances into array of JSON objects
#!/usr/bin/env bash
aws ec2 describe-instances \
| jq '.Reservations[].Instances[] | { id: .InstanceId, name: .Tags[] | select(.Key == "Name").Value, publicip: .PublicIpAddress, privateip: .PrivateIpAddress }'\
| jq -s 'sort_by(.name)'
@jaypipes
Copy link
Author

output looks like the following:

[
  {
    "id": "i-0921c8dff8a6f0d3c",
    "name": "controller-0",
    "publicip": "34.208.252.252",
    "privateip": "10.240.0.10"
  },
  {
    "id": "i-0f8a1a6fd0fc34761",
    "name": "controller-1",
    "publicip": "34.208.231.48",
    "privateip": "10.240.0.11"
  },
  {
    "id": "i-0c79be3bd8c77f1f6",
    "name": "controller-2",
    "publicip": "54.244.200.254",
    "privateip": "10.240.0.12"
  },
  {
    "id": "i-0af236486a23af653",
    "name": "worker-0",
    "publicip": "34.222.46.224",
    "privateip": "10.240.0.20"
  },
  {
    "id": "i-0cf51707e9ed38368",
    "name": "worker-1",
    "publicip": "34.219.10.120",
    "privateip": "10.240.0.21"
  },
  {
    "id": "i-00024039f73bbc63d",
    "name": "worker-2",
    "publicip": "34.220.237.3",
    "privateip": "10.240.0.22"
  }
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment