【问题标题】:How to add a target to a Network Load Balancer in CloudFormation如何将目标添加到 CloudFormation 中的网络负载均衡器
【发布时间】:2019-03-06 19:17:51
【问题描述】:

我有一些用于网络负载均衡器的 CloudFormation。

  PrivateNetworkLoadBalancerSG:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: Access to the internal network load balancer
      VpcId: !Ref 'VPC'
  PrivateNetworkLoadBalancerIngressFromECS:
    Type: AWS::EC2::SecurityGroupIngress
    Properties:
      Description: Only accept traffic from a container in the container host security group
      GroupId: !Ref 'PrivateNetworkLoadBalancerSG'
      IpProtocol: -1
      SourceSecurityGroupId: !Ref 'EcsHostSecurityGroup'
  PrivateNetworkLoadBalancer:
    Type: AWS::ElasticLoadBalancingV2::LoadBalancer
    Properties:
      Type: network
      Scheme: internal
      Subnets:
        - !Ref PrivateSubnetOne
        - !Ref PrivateSubnetTwo
  DummyTargetGroupPrivateNetwork:
    Type: AWS::ElasticLoadBalancingV2::TargetGroup
    Properties:
      Name: !Join ['-', [!Ref 'AWS::StackName', 'drop-3']]
      Port: 6379
      Protocol: TCP
      # UnhealthyThresholdCount: 2
      VpcId: !Ref 'VPC'

还有一些用于在 ECS 中设置 Redis docker 容器。

  RedisService:
    Type: AWS::ECS::Service
    Properties:
      Cluster: !ImportValue "privatevpc:ClusterName"
      DesiredCount: 1
      TaskDefinition: !Ref RedisTaskDefinition

  RedisTaskDefinition:
    Type: AWS::ECS::TaskDefinition
    Properties:
      Family: redis
      ContainerDefinitions:
        - Name: redis
          Essential: true
          Image: "redis:latest"
          Memory: 512
          PortMappings:
            - ContainerPort: 6379
              HostPort: 6379
          LogConfiguration:
            LogDriver: awslogs
            Options:
              awslogs-group: !Ref CloudWatchLogsGroup
              awslogs-region: !Ref AWS::Region

  RedisTargetGroup:
    Type: AWS::ElasticLoadBalancingV2::TargetGroup
    Properties:
      VpcId: !ImportValue "privatevpc:VPCId"
      Port: 6379
      Protocol: TCP
      HealthCheckProtocol: TCP

  RedisLoadBalancerListener:
    Type: AWS::ElasticLoadBalancingV2::Listener
    Properties:
      DefaultActions:
      - Type: forward
        TargetGroupArn: !Ref RedisTargetGroup
      LoadBalancerArn: !ImportValue "privatevpc:PrivateNetworkLoadBalancer"
      Port: 6379
      Protocol: TCP

但我必须通过 AWS Web 控制台手动添加部署我的 RedisService 的 EC2 实例作为 RedisTargetGroup 的目标。知道如何让 CloudFormation 为我做这件事吗?

【问题讨论】:

  • 我认为您可以通过一些示例来指导您创建多个 Cloudformation 资源,例如您需要的资源:@​​987654321@link2link3
  • 这就是我开始的地方。
  • 您需要添加自动缩放。你试过吗?

标签: amazon-web-services amazon-cloudformation aws-load-balancer


【解决方案1】:

我认为您需要将LoadBalancers 属性添加到RedisService。 ECS 应自动将正确的 EC2 实例添加到指定的目标组。

例如:

  RedisService:
    Type: AWS::ECS::Service
    Properties:
      Cluster: !ImportValue "privatevpc:ClusterName"
      DesiredCount: 1
      TaskDefinition: !Ref RedisTaskDefinition
      LoadBalancers:
        - ContainerName: redis
          ContainerPort: 6379
          TargetGroupArn: !Ref RedisTargetGroup

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ecs-service-loadbalancers.html

【讨论】:

    猜你喜欢
    • 2019-12-30
    • 1970-01-01
    • 2021-11-17
    • 2018-09-03
    • 1970-01-01
    • 1970-01-01
    • 2020-07-28
    • 2021-12-04
    • 1970-01-01
    相关资源
    最近更新 更多