AWS未正确标记资源检测:如何通过Resource Groups Tagging API筛选仅保留指定标签的JSON输出
我来帮你修正这个命令,让它既能精准筛选出符合你要求的资源(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
命令细节解释
- 修正参数笔误:把原命令里的
--tags-er-page改成了正确的--tags-per-page,确保分页参数正常生效。 - 精准筛选资源:
any(.Tags[]; .Key == "Name" and .Value == "Unused"):检查资源是否包含Name标签且值严格为Unusedand any(.Tags[]; .Key == "Name in Resolve" and .Value == "false"):同时验证Name in Resolve标签值为false
- 保留指定标签:
- 通过
{ 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
相关产品推荐
相关产品推荐

