如何从CloudFormation创建一个返回固定响应的AWS LB侦听器?

Jar*_*red 3 amazon-web-services aws-cloudformation aws-load-balancer

在“负载平衡器列表器”配置页面中,您可以在AWS控制台中使用默认操作创建侦听器,如下所示:

有效的默认操作

固定响应选项使您可以指定http返回码和正文:

返回固定响应

以下是已知有效的CloudFormation示例。不确定如何编辑它以支持非转发操作。

MyServicesLoadBalancerListener:
    Type: AWS::ElasticLoadBalancingV2::Listener
    Properties:
        LoadBalancerArn: !Ref MyServicesLoadBalancer
        Port: 80
        Protocol: HTTP
        DefaultActions:
            - Type: forward
              TargetGroupArn: !Ref MyServicesTargetGroup
Run Code Online (Sandbox Code Playgroud)

如何使用CloudFormation做到这一点?此处文档似乎建议CloudFormation仅支持“转发”规则。

谢谢

tyr*_*ron 6

还不可能。已在论坛上提出要求,但未提供ETA。

根据AWS CloudFormation 的发布历史记录,此功能于2018年11月19日添加。该功能应复制您在控制台图片中显示的固定响应。

MyServicesLoadBalancerListener:
  Type: AWS::ElasticLoadBalancingV2::Listener
  Properties:
    LoadBalancerArn: !Ref MyServicesLoadBalancer
    Port: 80
    Protocol: HTTP
    DefaultActions:
      - Type: fixed-response
        FixedResponseConfig:
          ContentType: "text/plain"
          MessageBody: "You've reached the listener! Congrats!"
          StatusCode: "503"
Run Code Online (Sandbox Code Playgroud)

  • 很棒的发现。谢谢你。 (2认同)
  • @Jared cloudformation 现在支持创建这个。我已经用新代码更新了我的答案。 (2认同)