简体   繁体   中英

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. But I'm unable to add that EC2 instance directly to that Load balancer.

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:

this.Elb.addEc2Instance(this.Instance)

but there isn't any such property available.

You can't do this with 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 :

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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