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

核能气质少年 提交于 2021-01-28 05:06:32

问题


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$

回答1:


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(@)))]'




回答2:


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]



来源:https://stackoverflow.com/questions/44314898/how-to-get-all-security-groups-through-aws-cli-of-an-ec2-to-show-in-a-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!