Michael Martinez
Michael Martinez

Reputation: 2853

How to tell jq to print multiples fields(keys) from aws ec2 cli

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.

Upvotes: 0

Views: 1906

Answers (1)

Frederic Henri
Frederic Henri

Reputation: 53753

Working with AWS CLI alone

I love jq and it's really really helpful to combine with aws cli but you can already do a lot simply with aws cli in your case.

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.

aws ec2 describe-instances --query '
  Reservations[*].Instances[].{PublicDnsName:PublicDnsName,VpcId:VpcId}' --output text

Specifically what I'm looking for is a list of instances where VpcId is null, undefined, or non-existent

If you search for empty VpcId, the following will work:

aws ec2 describe-instances --query '
  Reservations[].Instances[?VpcId==``].[PublicDnsName]' --output text

Working with jq

As the original question is

How to tell jq to print multiples fields(keys) from aws ec2 cli

sure you can work with jq, you can get multiple fields with the following by separating by a comma

aws ec2 describe-instances |\
  jq '.Reservations[].Instances[].PublicDnsName, .Reservations[].Instances[].VpcId'

You can better write it by factoring Reservations[].Instances:

aws ec2 describe-instances |\
  jq '.Reservations[].Instances[] | .PublicDnsName, .VpcId'

and jq supports transformation into csv but it needs to take an array and to avoid issues with double-quotes, you can work with raw data:

aws ec2 describe-instances |\
  jq -r '.Reservations[].Instances[]
         | [.PublicDnsName, .VpcId]
         | @csv'

The last will just product the expected result:

"ec2-some_id_region.amazonaws.com","vpc-some_other_id"

Upvotes: 4

Related Questions