使用CloudCustodian筛选器Key进行EC2 AMI的JMESPath查询

huangapple go评论78阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年7月18日 04:12:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76707793.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定