我正试图设置一对弹性ip作为NetworkLoadBalancer对象的公共面向地址并遇到问题。下面代码中的console.log("CFN NLB");
行永远不会执行,因为负载平衡器定义抛出以下错误:
There are no 'Public' subnet groups in this VPC. Available types:
Subprocess exited with error 1
我这样做是因为没有高级别的方法可以将现有的弹性ip分配给负载均衡器,而不像这里讨论的那样使用Cfn逃生舱口。
如果我在NetworkLoadBalancer定义中启用注释代码,堆栈合成成功,但在部署时我得到以下结果:
You can specify either subnets or subnet mappings, not both (Service: AmazonElasticLoadBalancing; Status Code: 400; E
rror Code: ValidationError; Request ID: e4b90830-xxxx-4f13-8777-bcf56946781a; Proxy: null)
代码:
const pubSubnet1ID = 'subnet-xxxxxfa6d669cd496';
const pubSubnet2ID = 'subnet-xxxxxbaf8d2d77afb';
const pubSubnet1 = Subnet.fromSubnetId(this, 'pubSubnet1', pubSubnet1ID);
const pubSubnet2 = Subnet.fromSubnetId(this, 'pubSubnet2', pubSubnet2ID);
console.log("Tagging.");
Tags.of(pubSubnet1).add('aws-cdk:subnet-type', 'Public');
Tags.of(pubSubnet2).add('aws-cdk:subnet-type', 'Public');
console.log("Load Balancer...");
this.loadBalancer = new NetworkLoadBalancer(this, 'dnsLB', {
vpc: assets.vpc,
internetFacing: true,
crossZoneEnabled: true,
// vpcSubnets: {
// subnets: [pubSubnet1, pubSubnet2],
// },
});
console.log("CFN NLB");
this.cfnNLB = this.loadBalancer.node.defaultChild as CfnLoadBalancer;
console.log("Mappings");
const subnetMapping1: CfnLoadBalancer.SubnetMappingProperty = {
subnetId: pubSubnet1ID,
allocationId: assets.elasticIp1.attrAllocationId,
}
const subnetMapping2: CfnLoadBalancer.SubnetMappingProperty = {
subnetId: pubSubnet2ID,
allocationId: assets.elasticIp2.attrAllocationId,
}
console.log("Mapping assignment");
this.cfnNLB.subnetMappings = [subnetMapping1, subnetMapping2];
我发现CDK想要一个aws-cdk:subnet-type
的标签,其值为Public
,并将该标签添加到我们的公共子网(手动和编程),但错误保持不变。
我找到了解决方案。取消对loadBalancer定义的vpcSubnets:
部分的注释使我能够跳过第一条错误消息。要绕过"您可以指定子网或子网映射,而不是同时指定"消息,我添加了
this.cfnNLB.addDeletionOverride('Properties.Subnets');
设置subnetMappings
属性前。