【问题标题】:Unable to attach EC2 instance to a classic load balancer in AWS CDK无法将 EC2 实例附加到 AWS CDK 中的经典负载均衡器
【发布时间】:2021-08-03 15:47:08
【问题描述】:

我使用 typescript 在 AWS CDK 中创建了一个 EC2 实例和一个 Classic Load Balancer。但我无法将该 EC2 实例直接添加到该负载均衡器。

this.Instance= new ec2.Instance(this, 'my-Instance', {
  vpc,
  instanceType: new InstanceType(instanceType),
  ...});

和负载均衡器

this.Elb = new LoadBalancer(this, 'my-ELB', {
..
crossZone: true,
internetFacing: false,
...});

我希望使用以下方式将此 ec2 实例添加到此负载均衡器:

this.Elb.addEc2Instance(this.Instance)

但没有任何此类可用的属性。

【问题讨论】:

    标签: typescript amazon-web-services amazon-ec2 amazon-cloudformation aws-cdk


    【解决方案1】:

    LoadBalancer 无法做到这一点。您必须首先将您的实例放在 自动缩放组 中。然后您将 ASG 附加到您的 LB,如 example 所示:

    const lb = new elb.LoadBalancer(this, 'LB', {
        vpc,
        internetFacing: true,
        healthCheck: {
            port: 80
        },
    });
    
    lb.addTarget(myAutoScalingGroup);
    lb.addListener({
        externalPort: 80,
    });
    

    【讨论】:

      【解决方案2】:

      这终于对我有用了。把它放在这里,这样就没有其他人像我一样浪费时间来解决这个问题了。

      elbObj.instances 需要一个实例 ID 的字符串数组。 (read here)

       const elbObj = this.elb.node.defaultChild as CfnLoadBalancer;
          if (elbObj) {
            elbObj.instances = [(this.jenkinsInstance.instanceId).toString()];
          }
      

      【讨论】:

        猜你喜欢
        • 2015-07-03
        • 2020-02-12
        • 1970-01-01
        • 1970-01-01
        • 2021-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-14
        相关资源
        最近更新 更多