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

AWS CloudFormation:EventBridge规则事件模式detail格式不符问题

问题

我有一段用于AWS EventBridge规则的CloudFormation代码片段:

AutoscalingRule:
  Type: AWS::Events::Rule
  Properties:
    Description: "AutoscalingRule"
    EventPattern:
      source:
        - aws.autoscaling
      detail:
        - !Sub "AutoScalingGroupName: ${AutoscalingGroup}"
      detail-type:
        - "EC2 Instance Launch Successful"
        - "EC2 Instance Terminate Successful"
    State: "ENABLED"
    Targets:
      -
        Arn:
          Fn::GetAtt:
            - "AutoscalingMonitoringLambda"
            - "Arn"
        Id: "autoscaling-monitoring"

问题在于Event Pattern的detail部分格式与控制台创建的样式不符。控制台创建的detail部分如下:

{
  "source": ["aws.autoscaling"],
  "detail-type": ["EC2 Instance Launch Successful", "EC2 Instance Terminate Successful"],
  "detail": {
    "AutoScalingGroupName": ["test"]
  }
}

但通过CloudFormation部署后,格式变成:

{
  "detail-type": ["EC2 Instance Launch Successful", "EC2 Instance Terminate Successful"],
  "source": ["aws.autoscaling"],
  "detail": ["AutoScalingGroupName: test"]
}

我尝试过以JSON形式插入或手动格式化,但都无效。请问应如何修改detail部分,使其与控制台创建的样式一致?

解决方法

你当前的detail字段写法错误,将其写成了字符串数组,正确的写法应为嵌套键值对结构,把AutoScalingGroupName作为键,对应的值设为包含自动扩缩组名称的数组,这样就能和控制台格式匹配。

修改后的CloudFormation代码片段如下:

AutoscalingRule:
  Type: AWS::Events::Rule
  Properties:
    Description: "AutoscalingRule"
    EventPattern:
      source:
        - aws.autoscaling
      detail:
        AutoScalingGroupName:
          - !Sub "${AutoscalingGroup}"
      detail-type:
        - "EC2 Instance Launch Successful"
        - "EC2 Instance Terminate Successful"
    State: "ENABLED"
    Targets:
      -
        Arn:
          Fn::GetAtt:
            - "AutoscalingMonitoringLambda"
            - "Arn"
        Id: "autoscaling-monitoring"

说明

  • 原写法中detail是包含"AutoScalingGroupName: ${AutoscalingGroup}"字符串的数组,会被解析为字符串列表,不符合EventBridge的事件匹配格式要求。
  • 修改后detail是一个对象,AutoScalingGroupName对应的值为数组,数组元素通过!Sub引用自动扩缩组名称,部署后的Event Pattern结构将和控制台创建的完全一致。

内容的提问来源于stack exchange,提问作者Alexandru Constantin Arvinte

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 15:15:27