AWS ECS 服务和 ELBv2 在不同的堆栈中

AWS ECS service and ELBv2 in separate stacks

是否可以为 ECS 服务(例如 blue/green)提供一个 CF 堆栈,为前端 ELBv2 使用单独的堆栈?我试过在 ECS 堆栈中使用 ELB TargetGroup,在另一个堆栈中使用负载均衡器和侦听器,但是在构建目标组时出现 Cloud Formation 报告和错误

"The target group with targetGroupArn xxxxxx does not have an associated load balancer."

然而,目标组资源中没有任何内容定义任何负载均衡器以供其依赖:

 ELB2TargetGroup:
    Type: AWS::ElasticLoadBalancingV2::TargetGroup
    Properties:
      HealthCheckIntervalSeconds: 60
      UnhealthyThresholdCount: 10
      HealthCheckPath: /
      Port:     !Ref ALBPort
      Protocol: !Ref ALBProtocol
      VpcId:
        Fn::ImportValue: !Sub "${ECSClusterStack}-ClusterVPC"

希望您已经解决了这个问题。但是,以防万一其他人来这里寻找同样的问题。

Essentially, you have done nothing wrong. Just that, you need a Listener Rule to associate the Target Group with the Load Balancer Listener.

这是一个 TargetGroup 资源示例:

...
ExampleTG:
  Type: AWS::ElasticLoadBalancingV2::TargetGroup
  Properties:
    HealthCheckIntervalSeconds: 30
    HealthCheckProtocol: HTTP
    HealthCheckTimeoutSeconds: 5
    HealthyThresholdCount: 2
    HealthCheckPath: /healthcheck
    HealthCheckPort: 80
    Matcher:
      HttpCode: '200'
    Name: !Join [ "-", [ Example, !Ref SomeParameter] ]
    Port: 80
    Protocol: HTTP
    TargetGroupAttributes:
    - Key: deregistration_delay.timeout_seconds
      Value: '15'
    UnhealthyThresholdCount: 3
    VpcId:
      'Fn::ImportValue': !Sub '${ParentVPCStack}-vpc'

我想你已经知道了。您现在只需要一个 ListernerRule:

...
ExampleListernerRule:
  Type: AWS::ElasticLoadBalancingV2::ListenerRule
  Properties:
    Actions:
      - Type: forward
        TargetGroupArn: !Ref ExampleTG
    Conditions:
      - Field: host-header
        Values:
        - "example.*"
    ListenerArn:
      'Fn::ImportValue': !Sub '${ParentLBStack}-existing-listener'
    Priority: 1

您可以轻松地从现有堆栈 [在您的情况下为 ELBv2] 导入侦听器,它将负载均衡器与目标组相关联。