You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS未正确标记资源检测:如何通过Resource Groups Tagging API筛选仅保留指定标签的JSON输出

How to Filter AWS Resources by Specific Tags and Retain Only Target Tags in JSON Output

我来帮你修正这个命令,让它既能精准筛选出符合你要求的资源(Name标签值为Unused且Name in Resolve标签值为false),又能只保留这两个标签和资源ARN信息:

修改后的完整命令

aws resourcegroupstaggingapi get-resources --tags-per-page 100 | jq '
.ResourceTagMappingList[] 
| select(
    any(.Tags[]; .Key == "Name" and .Value == "Unused") 
    and any(.Tags[]; .Key == "Name in Resolve" and .Value == "false")
)
| {
    ResourceARN: .ResourceARN,
    Tags: [.Tags[] | select(.Key == "Name" or .Key == "Name in Resolve")]
}
' > tag.json

命令细节解释

  1. 修正参数笔误:把原命令里的--tags-er-page改成了正确的--tags-per-page,确保分页参数正常生效。
  2. 精准筛选资源:
    • any(.Tags[]; .Key == "Name" and .Value == "Unused"):检查资源是否包含Name标签且值严格为Unused
    • and any(.Tags[]; .Key == "Name in Resolve" and .Value == "false"):同时验证Name in Resolve标签值为false
  3. 保留指定标签:
    • 通过{ ResourceARN: .ResourceARN, Tags: [...] }构造新的输出结构,只保留你需要的资源ARN和目标标签
    • .Tags[] | select(.Key == "Name" or .Key == "Name in Resolve"):从原标签列表中过滤出仅这两个标签,组成精简后的Tags数组

预期输出效果

执行后你会得到完全符合需求的JSON结果:

{
  "ResourceARN": "arn:aws:backup:$Region:$AccountId:recovery-point:xxxxxxxx",
  "Tags": [
    { "Key": "Name", "Value": "Unused" },
    { "Key": "Name in Resolve", "Value": "false" }
  ]
}

内容的提问来源于stack exchange,提问作者Simply Anks

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 19:02:37