【问题标题】:How do i apply sql like query on the aws cli table output如何在 aws cli 表输出上应用类似 sql 的查询
【发布时间】:2021-09-19 21:14:00
【问题描述】:

如何在 aws cli 表输出上应用类似 sql 的查询。 我的 aws cli 命令如下:

aws ec2 describe-instances --query 'Reservations[*].Instances[*].{ImageId: ImageId, InstanceId: InstanceId, InstanceType: InstanceType,State: State.Name, KeyName : KeyName,AZ: Placement.AvailabilityZone,Tenancy: Placement.Tenancy,LaunchTime: LaunchTime, PrivateIpAddress: PrivateIpAddress, PublicIpAddress: PublicIpAddress, VpcId: VpcId, SubnetId: SubnetId, Architecture: Architecture,SG: SecurityGroups[0].GroupName,SG_ID: SecurityGroups[0].GroupId,OwnerID: NetworkInterfaces[0].OwnerId}' --region us-east-1 --output table

我想对上面的 aws cli 查询表输出做如下操作:

我想按实例类型对所有 InstanceId 进行分组,如下所示:


---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|                                                                                                                                                                                     DescribeInstances                                                                                                                                                                                     |
+------------+---------------+------------------------+----------------------+---------------+-------------+---------------------------+---------------+-------------------+------------------+--------------------------------------------------------------------------------------------------------+-----------------------+-------------+------------------+----------+----------------+
|     AZ     | Architecture  |        ImageId         |     InstanceId       | InstanceType  |   KeyName   |        LaunchTime         |    OwnerID    | PrivateIpAddress  | PublicIpAddress  |                                                  SG                                                    |         SG_ID         |    State    |    SubnetId      | Tenancy  |     VpcId      |
+------------+---------------+------------------------+----------------------+---------------+-------------+---------------------------+---------------+-------------------+------------------+--------------------------------------------------------------------------------------------------------+-----------------------+-------------+------------------+----------+----------------+
|  us-east-1b|  x86_64       |  ami-1ecae776          |  i-dad4180f          |  t2.micro     |  TruCentric |  2015-09-08T15:27:59.000Z |  401331319872 |  10.51.1.232      |  None            |  build-vpc-BastionSecurityGroup-1RRONL28DLJAM                                                          |  sg-6d476b0a          |  stopped    |  subnet-1d323d6a |  default |  vpc-4976b12d  |
|  us-east-1a|  x86_64       |  ami-1ecae776          |  i-e8c2614b          |               |  TruCentric |  2015-09-08T15:27:59.000Z |  401331319872 |  10.51.0.209      |  None            |  build-vpc-BastionSecurityGroup-1RRONL28DLJAM                                                          |  sg-6d476b0a          |  stopped    |  subnet-b8a3f793 |  default |  vpc-4976b12d  |
|  us-east-1a|  x86_64       |  ami-d05e75b8          |  i-d4cc6f77          |               |  acquia_ops |  2019-07-15T15:34:59.000Z |  401331319872 |  10.51.0.220      |  52.91.20.69     |  build-vpc-ApplicationSecurityGroup-81J8RJLV25JH                                                       |  sg-6e476b09          |  running    |  subnet-b8a3f793 |  default |  vpc-4976b12d  |
|  us-east-1a|  x86_64       |  ami-d05e75b8          |  i-3c43ef9f          |               |  acquia_ops |  2016-04-23T23:13:14.000Z |  401331319872 |  10.52.0.85       |  54.165.202.171  |  dev-vpn-SecurityGroup-U7UONIX6LIB9                                                                    |  sg-67f9d100          |  running    |  subnet-48124563 |  default |  vpc-21d71145  |

【问题讨论】:

  • 我打算建议您考虑使用lebinh/aq,它允许您使用 SQL 查询 AWS 资源,但似乎 broken at present 尽管您可能能够使其与某些 Python 包版本一起使用管理调整。
  • 抱歉,您的问题不清楚。 “按实例类型对所有 InstanceId 进行分组”是什么意思?

标签: amazon-web-services amazon-ec2 aws-cli


【解决方案1】:

假设“分组依据”是指将特定 InstanceType 的条目一起列出,即按以下顺序排序,下面的命令应该能够为您提供所需的输出,尽管 InstanceType 是重复的,并且每个输出都不是空白您在问题中的表现。

它使用 AWS CLI 命令的--query 中的sort_by 函数。

aws ec2 describe-instances --query 'sort_by(Reservations[].Instances[], &InstanceType)[:].{ImageId: ImageId, InstanceId: InstanceId, InstanceType: InstanceType,State: State.Name, KeyName : KeyName,AZ: Placement.AvailabilityZone,Tenancy: Placement.Tenancy,LaunchTime: LaunchTime, PrivateIpAddress: PrivateIpAddress, PublicIpAddress: PublicIpAddress, VpcId: VpcId, SubnetId: SubnetId, Architecture: Architecture,SG: SecurityGroups[0].GroupName,SG_ID: SecurityGroups[0].GroupId,OwnerID: NetworkInterfaces[0].OwnerId}' --region us-east-1 --output table

这有帮助吗?

【讨论】:

  • 这只是对结果进行排序,我们不能用实例类型对实例进行分组。查了很多,在aws cli中没有找到直接的方法,于是尝试通过json python库解析得到的结果,达到了要求。
  • 明白了,分组的时候一般会加一个汇总字段,比如sum、avg。想知道您是否有这样的聚合,我在显示的输出中没有看到。或者您是否试图在输出表中的所有字段中模拟“选择不同”结果?用 Python 解析结果当然是解决这个问题的另一种方法。
猜你喜欢
  • 2018-08-16
  • 2014-01-09
  • 1970-01-01
  • 2016-01-30
  • 1970-01-01
  • 2010-11-07
  • 2019-12-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多