[英]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.