![](/img/trans.png)
[英]How to import existing private subnets in a VPC in AWS CDK using TypeScript?
[英]VPC "fromLookup" results in all Private Subnets and no Public Subnets
我正在尝试使用 CDK v1.1.0 创建一个面向 Internet 的应用程序负载均衡器。
当我使用我的vpcId
调用ec2.vpc.fromLookup
,我得到了我的所有子网,但它们都被标记为PRIVATE
子网。 因此,我收到一个错误,提示没有可用的公共子网。
如果我尝试使用我的vpcId
、 availabilityZones
和ec2.vpc.fromVpcAttributes
使用SubnetIDs
,我会收到错误“无法读取未定义的属性‘selectSubnets’”。
我的子网的路由表:在子网中,我的 RouteTable 有两个路由:
我不确定是我的子网/VPC 配置不正确还是 aws-cdk 有错误。
我已经手动尝试使用公共子网创建应用程序负载均衡器,并且能够成功创建。 所以我不能说我的子网配置不正确。
我花了一段时间,但我想通了。 对于我的公共子网,需要启用属性: Auto-assign public IPv4 address
(设置为 YES)。
但这导致了一个奇怪的要求,我必须在所有支持的可用区中拥有相同数量的公共和私有子网。 我不明白为什么。 否则我得到错误:
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
导入和控制子网分类方式的最简单方法是使用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'],
});
子网顺序和长度很重要,并且必须与可用区匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.