如何在具有超过 16 个子网的 VPC 中使用 AWS CDK 创建 ECS 服务

Ant*_*ony 2 aws-cdk

使用 AWS CDK 创建 FargateService 时,我收到以下错误:

子网最多可以有 16 个项目。

我有这个代码来创建服务:

 var ecsService = new FargateService(this, $"{serviceNameHyphen}-service", new FargateServiceProps
 {
    TaskDefinition = taskDefinition,
    AssignPublicIp = false,
    Cluster = infrastructureStack.EcsCluster,
    CloudMapOptions = new CloudMapOptions
    {
          Name = serviceName,
          DnsRecordType = DnsRecordType.A,
          DnsTtl = Duration.Seconds(60),
          FailureThreshold = 2d
    },
    DesiredCount = 1,
    HealthCheckGracePeriod = Duration.Seconds(60),
    MaxHealthyPercent = 200,
    MinHealthyPercent = 100,
    PlatformVersion = FargatePlatformVersion.LATEST,
    ServiceName = $"{serviceNameHyphen}-service",
    SecurityGroup = albSecurityGroup,
    VpcSubnets = new SubnetSelection
    {
         OnePerAz = true,
         SubnetType = SubnetType.PUBLIC,
    }
});
Run Code Online (Sandbox Code Playgroud)

可以过滤子网吗?

或者,该类SubnetSelection有一个SubnetGroup属性 - 我知道某些 AWS 服务允许创建子网组,但我看不到如何创建用于 Fargate 服务的任意子网组。

更新 我现在 VPC 中只有 15 个子网,但我仍然收到相同的错误消息。

jog*_*old 5

ec2.Vpc.fromVpcAttributes()如果您要导入现有 VPC,您可以通过使用而不是使用导入子网子集来解决此问题ec2.Vpc.fromLookup()

const vpc = ec2.Vpc.fromVpcAttributes(this, 'Vpc', {
  vpcId: 'vpc-xxxxxxxx',
  availabilityZones: ['eu-west-1a', 'eu-west-1b', 'eu-west-1c'],
  publicSubnetIds: ['subnet-xxxxxxxx', 'subnet-xxxxxxxx', 'subnet-xxxxxxxx'],
  privateSubnetIds: ['subnet-xxxxxxxx', 'subnet-xxxxxxxx', 'subnet-xxxxxxxx']
});
Run Code Online (Sandbox Code Playgroud)

这样,从 CDK 应用程序的角度来看,您的 VPC 将只有 3 个公有子网。