无法将 EC2 实例附加到 AWS CDK 中的经典负载均衡器
Unable to attach EC2 instance to a classic load balancer in AWS CDK
我使用打字稿在 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)
但是没有这样的 属性 可用。
您无法使用 LoadBalancer
执行此操作。您必须首先将您的实例放入 自动缩放组 中。然后将 ASG 附加到 LB,如 example:
所示
const lb = new elb.LoadBalancer(this, 'LB', {
vpc,
internetFacing: true,
healthCheck: {
port: 80
},
});
lb.addTarget(myAutoScalingGroup);
lb.addListener({
externalPort: 80,
});
这终于对我有用了。把它放在这里,这样其他人就不会像我一样浪费时间来解决这个问题了。
elbObj.instances
需要实例 ID 的字符串数组。 (read here)
const elbObj = this.elb.node.defaultChild as CfnLoadBalancer;
if (elbObj) {
elbObj.instances = [(this.jenkinsInstance.instanceId).toString()];
}
我使用打字稿在 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)
但是没有这样的 属性 可用。
您无法使用 LoadBalancer
执行此操作。您必须首先将您的实例放入 自动缩放组 中。然后将 ASG 附加到 LB,如 example:
const lb = new elb.LoadBalancer(this, 'LB', {
vpc,
internetFacing: true,
healthCheck: {
port: 80
},
});
lb.addTarget(myAutoScalingGroup);
lb.addListener({
externalPort: 80,
});
这终于对我有用了。把它放在这里,这样其他人就不会像我一样浪费时间来解决这个问题了。
elbObj.instances
需要实例 ID 的字符串数组。 (read here)
const elbObj = this.elb.node.defaultChild as CfnLoadBalancer;
if (elbObj) {
elbObj.instances = [(this.jenkinsInstance.instanceId).toString()];
}