AWS CloudFormation堆栈:根据条件添加负载平衡器侦听器

时间:2019-03-05 16:42:36

标签: amazon-web-services automation amazon-cloudformation amazon-elb

我正在尝试使用CloudFormation来构建负载均衡器侦听器的列表,并且将可选参数TCPPort1设置为仅 (如果已设置)的侦听器。

我在其他地方使用过Condition,但是如果我在侦听器级别使用它,则会出现错误: The following resource(s) failed to create: [LoadBalancer]. . Rollback requested by user. Encountered unsupported property Condition

LoadBalancer:
    Type: 'AWS::ElasticLoadBalancing::LoadBalancer'
    Properties:
      HealthCheck:
        HealthyThreshold: '2'
        Interval: '15'
        Target: 'HTTP:9000/'
        Timeout: '5'
        UnhealthyThreshold: '10'
      Listeners:
        - InstancePort: TCPPort1
          Condition: HasTCPPort1
          InstanceProtocol: HTTP
          LoadBalancerPort: TCPPort1
          SSLCertificateId: !If
            - HasLoadBalancerCertificateARN
            - !Ref LoadBalancerCertificateARN
            - !Ref 'AWS::NoValue'
          Protocol: !If
            - HasLoadBalancerCertificateARN
            - HTTPS
            - HTTP
      Scheme: internet-facing
      Subnets:
        - 'Fn::ImportValue':
              !Sub '${ParentVPCStack}-SubnetAPublic'
        - 'Fn::ImportValue':
              !Sub '${ParentVPCStack}-SubnetBPublic'
        - 'Fn::ImportValue':
              !Sub '${ParentVPCStack}-SubnetCPublic'
      SecurityGroups:
        - !Ref SecurityGroup
      Instances:
        - !Ref Server

1 个答案:

答案 0 :(得分:0)

编辑:使用经典的负载均衡器...

LoadBalancer:
  Type: 'AWS::ElasticLoadBalancing::LoadBalancer'
  Properties:
    HealthCheck:
      HealthyThreshold: '2'
      Interval: '15'
      Target: 'HTTP:9000/'
      Timeout: '5'
      UnhealthyThreshold: '10'
    Listeners: !If 'HasTCPPort1'
      -
        - InstancePort: TCPPort1
          Condition: HasTCPPort1
          InstanceProtocol: HTTP
          LoadBalancerPort: TCPPort1
          SSLCertificateId: !If
            - HasLoadBalancerCertificateARN
            - !Ref LoadBalancerCertificateARN
            - !Ref 'AWS::NoValue'
          Protocol: !If
            - HasLoadBalancerCertificateARN
            - HTTPS
            - HTTP
      - !Ref 'AWS::NoValue' 
    Scheme: internet-facing
    Subnets:
      - 'Fn::ImportValue':
            !Sub '${ParentVPCStack}-SubnetAPublic'
      - 'Fn::ImportValue':
            !Sub '${ParentVPCStack}-SubnetBPublic'
      - 'Fn::ImportValue':
            !Sub '${ParentVPCStack}-SubnetCPublic'
    SecurityGroups:
      - !Ref SecurityGroup
    Instances:
      - !Ref Server