AWS CDK Getting Error when try to initialize a new VPC with private isolated subnet used in Fargate Cluster

Viewed 25

AWS CDK Getting Error when try to initialize a new VPC with private isolated subnet used in Fargate Cluster. (@aws-cdk/ -- @1.174.0 - Version).

this.vpc = new ec2.Vpc(this, `horizonCloudVpc`, {
    cidr: '10.0.0.0/16',
    vpcName: `horizonCloudVpc-${envName}`,
    enableDnsHostnames: true,
    enableDnsSupport: true,
    maxAzs: 2,
    subnetConfiguration: [
      {
        name: 'public-subnet',
        subnetType: ec2.SubnetType.PUBLIC,
        cidrMask: 24,
      },
      {
        name: 'isolated-subnet',
        subnetType: ec2.SubnetType.PRIVATE_ISOLATED,
        cidrMask: 24,
      },
    ]
});

const clusterAdmin = new Role(this, 'eksClusterMasterRole', {
    roleName: `clusterMasterRole-${envName}`,
    assumedBy: new AccountRootPrincipal(),
});

const cluster = new eks.FargateCluster(this, 'horizonCloudEks', {
  version: eks.KubernetesVersion.V1_21,
  vpc: vpc,
  clusterName: `horizonCloudEks-${envName}`,
  endpointAccess: eks.EndpointAccess.PUBLIC,
  mastersRole: clusterAdmin,
});

Error from Deploy -

/home/runner/work/horizon/horizon/cdk/node_modules/@aws-cdk/aws-ec2/lib/vpc.ts:606
      throw new Error(`There are no '${subnetType}' subnet groups in this VPC. Available types: ${availableTypes}`);
            ^
Error: There are no 'Private' subnet groups in this VPC. Available types: Isolated,Deprecated_Isolated,Public

I might think it requires a PRIVATE_WITH_NAT subnet also.

Thanks!

1 Answers

This is happening because EKS is trying to make the cluster use Private and Public subnets in the VPC, and there are no Private subnets in it.

From the eks.FargateCluster docs:

vpcSubnets? Type: SubnetSelection[] (optional, default: All public and private subnets)

To change this default behavior, change the vpcSubnets constructor prop to the appropriate value. For example, to make the cluster only use Public subnets:

vpcSubnets: [{ subnetType: ec2.SubnetType.PUBLIC }]
Related