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

sho*_*sho 1 amazon-web-services aws-cli

有谁知道如何使用名称和特定标签导出 AWS 账户中的所有实例?我拥有的标签名为 Billing by Role。尝试使用 aws ec2 describe-instances 来实现这一点。

我尝试使用这个:

aws ec2 describe-instances --query 'Reservations[].Instances[].[Tags[?Key==`Name`].Value[] | [0], [Tags[?Key==`Billing by Role`].Value[]]' --output text
Run Code Online (Sandbox Code Playgroud)

但我收到一个错误:

ValueError: --query Reservations[].Instances[].[Tags[?Key== Name] .Value[] 的错误值| [0], [Tags[?Key== Billing by Role].Value[]]: Invalid token.: 在靠近 token "" (EOF) 的第 101 列解析错误表达:“Reservations[].Instances[].[Tags[? Key== Name].Value[] | [0], [Tags[?Key== Billing by Role].Value[]]"

谢谢。

sho*_*sho 7

得到它的工作:

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


Mar*_*k B 6

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=*"
Run Code Online (Sandbox Code Playgroud)

  • 然后使用 --query 选项来控制输出。 (2认同)