在AWS CDK中,如果您尝试创建没有子网的VPC Endpoint,则会收到上述错误。为了解决此问题,您需要确保在创建VPC Endpoint时至少有一个子网可用。
以下是一个示例代码片段,用于创建带有子网的VPC Endpoint:
const vpc = ec2.Vpc.fromLookup(this, "VPC", { vpcName: "my-vpc" });
const subnet = vpc.privateSubnets[0];
const endpoint = new ec2.InterfaceVpcEndpoint(this, "Endpoint", {
vpc: vpc,
service: ec2.InterfaceVpcEndpointService.SAGEMAKER_NOTEBOOK,
privateDnsEnabled: true,
subnets: [subnet]
});
在上面的代码中,我们从名称为“my-vpc”的现有VPC中查找VPC,选择第一个私有子网,并使用该子网创建Internet VPC Endpoint。这将避免“Cannot create a VPC Endpoint with no subnets”错误。