英文:
JMESPath query with CloudCustodian filter Key for ec2 ami
问题
我想使用cloudcustodian找到并停止所有运行早于特定日期的AMI并具有特定标签的EC2实例。这些AMI上的标签将如下所示:
"Tags": [
{
"Key": "tag-1",
"Value": "value-1"
},
{
"Key": "special-tag",
"Value": "special-value"
}
]
我想要查找运行带有"special-tag"
的EC2实例。
我尝试了以下策略,但过滤器未选择任何具有这种AMI的EC2实例:
policies:
- name: ec2_with_expired_ami
resource: aws.ec2
filters:
- type: image-age
days: 110
op: gte
- type: image
key: "Tags"
op: contains
value: "special-tag"
actions:
- type: stop
我可能在使用错误的JMESPath语法。
英文:
I would like to find and stop, using cloudcustodian, all ec2s that are running an ami older than a certain date and that have a certain tag. The tags on such ami would look like
"Tags": [
{
"Key": "tag-1",
"Value": "value-1"
},
{
"Key": "special-tag",
"Value": "special-value"
}
]
and I want to find ec2 running amis with the "special-tag"
I tried
policies:
-name: ec2_with_expired_ami
resource: aws.ec2
filters:
- type: image-age
days: 110
op: gte
- type: image
key: "Tags"
op: contains
value: "special-tag"
actions:
- type: stop
but the filter doesnt pick any ec2 with such ami
I am probably using the wrong JMESPath syntax
答案1
得分: 1
你的示例策略非常接近!因为标签过滤非常常见,Custodian支持一种特殊的方便语法来针对标签进行key
的目标定位。您可以在文档中查看一个带注释的示例链接。
在您的情况下,将过滤器从以下内容更改为:
- type: image
key: tag:special-tag
value: present
听起来会起作用。值过滤文档的特殊值部分解释了特殊的present
值在那里的功能。
英文:
Your example policy is very close! Because tag filters are so common, Custodian supports a special convenience syntax for key
which targets tags. You can see an annotated example in the docs here.
In your case, changing the filter from:
- type: image
key: "Tags"
op: contains
value: "special-tag"
to:
- type: image
key: tag:special-tag
value: present
sounds like it will do the trick. The special values section of the value filter documentation explains how the special present
value functions there.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论