Prash
Prash

Reputation: 546

How to get all security groups through AWS CLI of an EC2 to show in a table

I am trying to show all instances along with attached security groups, block device name and their deleteontermination status. I need to show this data in table format but I am continuously getting the below error.

Kindly help me to understand what I am missing here.

PR-MacBook-Pro:~ pr$ aws ec2 describe-instances --output table --query 'Reservations[*].Instances[*].[InstanceId,SecurityGroups[].GroupName,Placement.AvailabilityZone,BlockDeviceMappings[].DeviceName, BlockDeviceMappings[].Ebs.DeleteOnTermination]'

Row should have 1 elements, instead it has 2
PR-MacBook-Pro:~ pr$

Upvotes: 2

Views: 2671

Answers (2)

Abey
Abey

Reputation: 165

I came across this while searching in google.

After I did a bit of research I found that you have to use | join(`, `, @)

So the command will be

aws ec2 describe-instances --output table --query 'Reservations[*].Instances[*].[InstanceId,SecurityGroups[].GroupName | join(`, `, @),Placement.AvailabilityZone,BlockDeviceMappings[].DeviceName | join(`, `, @), BlockDeviceMappings[].Ebs.DeleteOnTermination | join(`, `, to_array(to_string(@)))]'

Upvotes: 3

Madhan S
Madhan S

Reputation: 937

modify your command as below to make it work

aws ec2 describe-instances  --output table  --region "us-east-1"  --query 'Reservations[*].Instances[*].[InstanceId,SecurityGroups[].GroupName |[0],Placement.AvailabilityZone,BlockDeviceMappings[].DeviceName |[0], BlockDeviceMappings[].Ebs.DeleteOnTermination|[0] ]'

the response was in the format of

[
    [
        "i-xxxxxxxx", 
        [
            "xxxxxx"
        ], 
        "us-east-1a", 
        [
            "/dev/xvda"
        ], 
        [
            true
        ]
    ]
]

so you need parse the array element using | [0]

Upvotes: 1

Related Questions