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

时间:2019-10-19 10:07:28

标签: 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,
    }
});

可以过滤子网吗?

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

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

1 个答案:

答案 0 :(得分:1)

如果要导入现有的VPC,则可以通过使用ec2.Vpc.fromVpcAttributes()而不是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']
});

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