简体   繁体   English

无法将 EC2 实例附加到 AWS CDK 中的经典负载均衡器

[英]Unable to attach EC2 instance to a classic load balancer in AWS CDK

I have created an EC2 instance and a Classic Load Balancer in AWS CDK using typescript.我使用 typescript 在 AWS CDK 中创建了一个 EC2 实例和一个 Classic Load Balancer。 But I'm unable to add that EC2 instance directly to that Load balancer.但我无法将该 EC2 实例直接添加到该负载均衡器。

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

and load Balancer和负载均衡器

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

I'm looking to add this ec2 instance to this load balancer using something like this:我正在寻找使用以下内容将此 ec2 实例添加到此负载均衡器:

this.Elb.addEc2Instance(this.Instance)

but there isn't any such property available.但没有任何这样的财产可用。

You can't do this with LoadBalancer .你不能用LoadBalancer做到这一点。 You have to place your instance in autoscaling group first.您必须首先将您的实例放在自动缩放组中 And then you attach the ASG to your LB as shown in the example :然后您将 ASG 附加到您的 LB,如示例所示:

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

lb.addTarget(myAutoScalingGroup);
lb.addListener({
    externalPort: 80,
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM