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

使用CloudFormation创建ALB监听器规则时遇验证异常求助

CloudFormation创建ALB监听器规则验证错误排查

问题场景

使用CloudFormation创建ALB监听器规则时,收到模糊的验证错误提示:

Resource handler returned message: "Invalid request provided: AWS::ElasticLoadBalancingV2::ListenerRule Validation exception" (RequestToken: 00000000-0000-0000-0000-000000000000, HandlerErrorCode: InvalidRequest)

原模板内容如下:

AWSTemplateFormatVersion: '2010-09-09'
Description: CloudFormation template for creating a listener

Resources:
  TargetGroup:
    Type: AWS::ElasticLoadBalancingV2::TargetGroup
    Properties:
      Name: "dummy-target-group-deleteme"
      Port: 8080
      Protocol: HTTP
      TargetType: ip
      VpcId: vpc-00000000000000000

  ListenerRule:
    Type: AWS::ElasticLoadBalancingV2::ListenerRule
    Properties:
      Actions:
        - Type: forward
          ForwardConfig:
            TargetGroups:
              - TargetGroupArn: !Ref TargetGroup
                Weight: 1
      Conditions:
        - Field: http-header
          HostHeaderConfig:
            Values:
              - "deleteme.example.com"
      ListenerArn: "arn:aws:elasticloadbalancing:us-east-2:account_id:listener/app/hosting/.../..."
      Priority: 3000

错误原因

监听器规则的Conditions配置存在字段不匹配:

  • 当Field指定为http-header时,对应的配置块应为HttpHeaderConfig,而非HostHeaderConfig
  • 若要匹配HTTP请求的Host头部,Field需设置为host-header,此时才对应HostHeaderConfig配置块

原模板混用了http-header字段和HostHeaderConfig配置,导致CloudFormation验证失败。

修复方案

根据实际需求选择以下两种修复方式之一:

方式1:匹配Host头部(修正Field字段)

将Field改为host-header,保留HostHeaderConfig配置:

AWSTemplateFormatVersion: '2010-09-09'
Description: CloudFormation template for creating a listener

Resources:
  TargetGroup:
    Type: AWS::ElasticLoadBalancingV2::TargetGroup
    Properties:
      Name: "dummy-target-group-deleteme"
      Port: 8080
      Protocol: HTTP
      TargetType: ip
      VpcId: vpc-00000000000000000

  ListenerRule:
    Type: AWS::ElasticLoadBalancingV2::ListenerRule
    Properties:
      Actions:
        - Type: forward
          ForwardConfig:
            TargetGroups:
              - TargetGroupArn: !Ref TargetGroup
                Weight: 1
      Conditions:
        - Field: host-header  # 修正此处字段
          HostHeaderConfig:
            Values:
              - "deleteme.example.com"
      ListenerArn: "arn:aws:elasticloadbalancing:us-east-2:account_id:listener/app/hosting/.../..."
      Priority: 3000

方式2:匹配自定义HTTP头部(修正配置块)

若需匹配特定自定义HTTP头部,将HostHeaderConfig改为HttpHeaderConfig并指定头部名称:

AWSTemplateFormatVersion: '2010-09-09'
Description: CloudFormation template for creating a listener

Resources:
  TargetGroup:
    Type: AWS::ElasticLoadBalancingV2::TargetGroup
    Properties:
      Name: "dummy-target-group-deleteme"
      Port: 8080
      Protocol: HTTP
      TargetType: ip
      VpcId: vpc-00000000000000000

  ListenerRule:
    Type: AWS::ElasticLoadBalancingV2::ListenerRule
    Properties:
      Actions:
        - Type: forward
          ForwardConfig:
            TargetGroups:
              - TargetGroupArn: !Ref TargetGroup
                Weight: 1
      Conditions:
        - Field: http-header
          HttpHeaderConfig:  # 修正此处配置块
            HttpHeaderName: "X-Custom-Header"  # 指定要匹配的HTTP头部名称
            Values:
              - "deleteme.example.com"
      ListenerArn: "arn:aws:elasticloadbalancing:us-east-2:account_id:listener/app/hosting/.../..."
      Priority: 3000

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 06:55:23