简体   繁体   English

如何在 AWS cli 中获取所有实例和特定标签的列表

[英]How to get a list of all instances and a specific tag in AWS cli

Does anyone know how to export all instances in an AWS account with their names and a specific tag?有谁知道如何使用名称和特定标签导出 AWS 帐户中的所有实例? The tag I have is named Billing by Role.我的标签名为 Billing by Role。 Trying to get this working with aws ec2 describe-instances.试图让它与 aws ec2 描述实例一起工作。

I tried using this:我试过用这个:

aws ec2 describe-instances --query 'Reservations[].Instances[].[Tags[?Key==`Name`].Value[] | [0], [Tags[?Key==`Billing by Role`].Value[]]' --output text

but I get an error:但我得到一个错误:

ValueError: Bad value for --query Reservations[].Instances[].[Tags[?Key== Name ].Value[] | ValueError:--query Reservations[].Instances[].[Tags[?Key== Name ].Value[] 的错误值 | [0], [Tags[?Key== Billing by Role ].Value[]]: Invalid token.: Parse error at column 101 near token "" (EOF) for expression: "Reservations[].Instances[].[Tags[?Key== Name ].Value[] | [0], [Tags[?Key== Billing by Role ].Value[]]" [0],[Tags[?Key== Billing by Role ].Value[]]:无效令牌。:在表达式“Reservations[].Instances[].[ Tags[?Key== Name ].Value[] | [0], [Tags[?Key== Billing by Role ].Value[]]"

Thanks.谢谢。

得到了它的工作:

aws ec2 describe-instances --query 'Reservations[].Instances[].{Name: Tags[?Key==`Name`].Value | [0], Role: Tags[?Key==`Billing by Role`].Value | [0]}' --output text

This command lists all instances in a region that have a tag named "Billing by Role". 此命令列出区域中所有具有名为“按角色计费”标签的实例。 Replace the * with a specific value to only get those instances with a specific value. 用特定值替换*只能使那些实例具有特定值。

aws ec2 describe-instances --filter "Name=tag:Billing by Role,Values=*"

To round out Mark B 's answer and fully answer your question, you can combine both --query and --filter .要完善Mark B的答案并完整回答您的问题,您可以结合使用--query--filter The difference is filter is done server side, and query is done client side.不同之处在于过滤器是在服务器端完成的,而查询是在客户端完成的。 If you have a lot of instances, it makes sense to avoid returning lots of data you're going to hide with --query .如果您有很多实例,避免返回大量您要使用--query隐藏的数据是有意义的。

If you only want instances where the Billing by Role tag is set, but want to return "Name Role" for each of them, you can use:如果您想要设置了Billing by Role标签的实例,但想为每个实例返回"Name Role" ,您可以使用:

aws ec2 describe-instances --filter "Name=tag:Billing by Role,Values=*" --query 'Reservations[].Instances[].{Name: Tags[?Key==`Name`].Value | [0], Role: Tags[?Key==`Billing by Role`].Value | [0]}' --output text

ORing each property to [0] is a nice trick for doing something like a full outer join, it makes less sense with a server-side filter but is nice when you can't guarantee which tag keys are set: ORing 每个属性到[0]是一个很好的技巧,可以做一些像完全外部连接这样的事情,它对服务器端过滤器没有多大意义,但是当你不能保证设置了哪些标签键时很好:

aws ec2 describe-instances --query 'Reservations[].Instances[].{Foo: Tags[?Key==`foo`].Value | [0], Bar: Tags[?Key==`bar`].Value | [0]}' --output table

| Foo  | Bar  |
|------|------|
| None | blue |
| red  | None |
| None | None |
| pink | grey |

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取具有特定标签的所有AWS EC2实例的列表及其对CPU和RAM的利用率 - How to get list of all AWS EC2 instances with specific tag and their utilisation of CPU and RAM AWS EC2 CLI - >如何获取可用实例列表 - AWS EC2 CLI -> How to get list of avaliable instances 用于从 aws 工作区获取实例详细信息列表的 Aws cli 命令 - Aws cli command to get the list of instances detail from aws workspace 如何在aws cli describe-instances输出中获取标签的值? - How do I get the value of a tag in the aws cli describe-instances output? 如何使用 AWS CLI 获取特定实例及其外部 IP 的列表? - How to get list of specific instance and their external IP using AWS CLI? 如何使用 aws cli 获取发电机表中所有项目的列表 - How to get list of all items in a dynamo Table using aws cli 如何使用AWS CLI在同一行中列出名称,状态,实例大小和AZ的所有实例 - How do I use AWS CLI to list all instances with name, state, instance size and AZ in the same line 如何使用 AWS CLI 获取具有特定标签的 Amazon EC2 自动扩展组中的实例? - How to get the instances in the Amazon EC2 autoscaling group with specific tags using AWS CLI? 使用AWS CLI获取实例的名称和ARN列表 - Get a list of names and ARNs of instances using AWS CLI AWS EC2 CLI 如何获取 EC2 实例列表 - 限制 output - AWS EC2 CLI How to get list EC2 Instances - limit output
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM