running aws ec2 describe-instances will return a json text similar to the following:
{
"Reservations": [
{
"Instances": [
"PublicDnsName": "ec2..."
"VpcId": "vpc-...",
...
"Instances": [
I know for each "Instance" I can extract the contents of a single field, for example PublicDnsName, using jq as follows:
jq '.Reservations[].Instances[].PublicDnsName'
which will list the dns names for my instances
But how do I extract two or more fields and separate them by a space or comma or something? I want PublicDnsName and VpcId to be listed side-by-side for each Instance.
Specifically what I'm looking for is a list of instances where VpcId is null, undefined, or non-existent. In other words I'd like a list of my Classic instances and I need this through api so I can process the results.
Here is an approach using some sample code and data from my answer to a similar question on Stack Overflow.
To choose multiple fields you can use Object Construction. E.g this filter makes an object containing just
PublicDnsName
andVpcId
from each instance:If this filter is in
filter.jq
and the sample data from that other answer is indata.json
then runningproduces
Once you have objects containing what you want getting the data into another format (e.g. csv) is easy. With this filter
and the
-r
optionjq produces
To add a filtering condition, e.g. VpcId is not null, add a select.