How to Use the AWS CLI to List IP Addresses and Other Instance Information

A common request from security officers (ISSO's), managers, or other stakeholders is a high-level summary of instances in their hosting infrastructure. Sometimes this is best delivered as a visualization, but often it's necessary to export the raw data. The AWS CLI has a tool to do this: describe-instances.

The basic usage is well documented, it goes like this:

aws ec2 describe-instances

By default this will output all your EC2 instance data in JSON format. This is great, but what if we only want to see the IP addresses, for example?

aws ec2 describe-instances \
  --query "Reservations[*].Instances[*].[InstanceId,PublicIpAddress,PrivateIpAddress]" \

This would output:

It's also possible to output more fields, and as text:

aws ec2 describe-instances \
  --query "Reservations[*].Instances[*].[InstanceId,PublicIpAddress,PrivateIpAddress,Tags[?Key=='Name'].Value[]]" \

The “query” option is very flexible and can be extended to many other applications.