简体   繁体   English

VPC“fromLookup”导致所有私有子网和没有公共子网

[英]VPC "fromLookup" results in all Private Subnets and no Public Subnets

Using CDK v1.1.0 I'm trying to create an internet-facing Application Load Balancer.我正在尝试使用 CDK v1.1.0 创建一个面向 Internet 的应用程序负载均衡器。

When I call ec2.vpc.fromLookup using my vpcId , I get all my subnets, but all of them are marked as PRIVATE Subnets.当我使用我的vpcId调用ec2.vpc.fromLookup ,我得到了我的所有子网,但它们都被标记为PRIVATE子网。 Due to this I get an error that there are no Public Subnets Available.因此,我收到一个错误,提示没有可用的公共子网。

If I try using ec2.vpc.fromVpcAttributes using my vpcId , availabilityZones and SubnetIDs , I get the error "Cannot read property 'selectSubnets' of undefined".如果我尝试使用我的vpcIdavailabilityZonesec2.vpc.fromVpcAttributes使用SubnetIDs ,我会收到错误“无法读取未定义的属性‘selectSubnets’”。

Route Table of My Subnets: Within the Subnets, my RouteTable has two Routes:我的子网的路由表:在子网中,我的 RouteTable 有两个路由:

  • Destination: 10.140.0.0/16, Target: Local, Status Active目的地:10.140.0.0/16,目标:本地,状态活跃
  • Destination: 0.0.0.0/0, Target: internet-gateway, Status Active目的地:0.0.0.0/0,目标:internet-gateway,Status Active

I'm not sure whether my subnet/VPC configuration are incorrect or aws-cdk has a bug.我不确定是我的子网/VPC 配置不正确还是 aws-cdk 有错误。

I've manually tried creating an Application Load Balancer with the public subnets and was able to create is successfully.我已经手动尝试使用公共子网创建应用程序负载均衡器,并且能够成功创建。 So I can't say my subnet configs are incorrect.所以我不能说我的子网配置不正确。

Took me a while, but i figured it out.我花了一段时间,但我想通了。 For my Public Subnet, the attribute: Auto-assign public IPv4 address needed to be enabled (set to YES).对于我的公共子网,需要启用属性: Auto-assign public IPv4 address (设置为 YES)。

But that led to a weird requirement, I have to have the same number of public and private Subnets in all of the supported AZs.但这导致了一个奇怪的要求,我必须在所有支持的可用区中拥有相同数量的公共和私有子网。 I don't understand why.我不明白为什么。 Otherwise i get the error:否则我得到错误:
Not all subnets in VPC have the same AZs: ap-southeast-2a,ap-southeast-2b vs ap-southeast-2a,ap-southeast-2a,ap-southeast-2a,ap-southeast-2a,ap-southeast-2b,ap-southeast-2b,ap-southeast-2b,ap-southeast-2b,ap-southeast-2c,ap-southeast-2c,ap-southeast-2c

The easiest way to import and control how your subnets are categorized is to use ec2.Vpc.fromVpcAttributes :导入和控制子网分类方式的最简单方法是使用ec2.Vpc.fromVpcAttributes

// Import existing VPC
const vpc = ec2.Vpc.fromVpcAttributes(this, 'Vpc', {
  vpcId: 'vpc-xxxxxx',
  availabilityZones: ['eu-west-1a', 'eu-west-1b', 'eu-west-1c'],
  publicSubnetIds: ['subnet-xxxxxx', 'subnet-xxxxxx', 'subnet-xxxxxx'],
  privateSubnetIds: ['subnet-xxxxxx', 'subnet-xxxxxx', 'subnet-xxxxxx'],
});

Subnet order and length matters and must match availability zones.子网顺序和长度很重要,并且必须与可用区匹配。

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

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