This message was deleted.
# typescript
s
This message was deleted.
g
how are u creating the subnets, i.e. where’s the relevant code?
a
Copy code
import { VpcSubnetArgs } from '@pulumi/awsx/ec2';
import { Netmask } from 'netmask';

interface Zone {
  id: string;
  privateSubnet: VpcSubnetArgs;
  publicSubnet: VpcSubnetArgs;
}

function privateSubnet(zoneId: string, cidr: Netmask): VpcSubnetArgs {
  return {
    location: {
      availabilityZoneId: zoneId,
      cidrBlock: cidr.toString(),
    },
    mapPublicIpOnLaunch: false,
    name: `${zoneId}-private`,
    tags: {
      '<http://kubernetes.io/role/internal-elb|kubernetes.io/role/internal-elb>': '1',
    },
    type: 'private',
  };
}

function publicSubnet(zoneId: string, cidr: Netmask): VpcSubnetArgs {
  return {
    location: {
      availabilityZoneId: zoneId,
      cidrBlock: cidr.toString(),
    },
    mapPublicIpOnLaunch: true,
    name: `${zoneId}-public`,
    tags: {
      '<http://kubernetes.io/role/elb|kubernetes.io/role/elb>': '1',
    },
    type: 'public',
  };
}

export function createSubnets(zoneIds: string[], cidr: Netmask): VpcSubnetArgs[] {
  // define a public & private subnet for each zone
  const { zones } = zoneIds.reduce((acc, id) => {
    const { privateBlock, publicBlock } = acc;
    return {
      privateBlock: privateBlock.next(),
      publicBlock: publicBlock.next(),
      zones: [...acc.zones, {
        id,
        privateSubnet: privateSubnet(id, privateBlock),
        publicSubnet: publicSubnet(id, publicBlock),
      }],
    };
  }, {
    privateBlock: new Netmask(`${cidr.base}/20`).next(),
    publicBlock: new Netmask(`${cidr.base}/24`).next(),
    zones: [],
  } as { privateBlock: Netmask, publicBlock: Netmask, zones: Zone[] });

  // return array of public and private subnets
  return zones.reduce((all, zone) => ([
    ...all,
    zone.privateSubnet,
    zone.publicSubnet,
  ]), [] as VpcSubnetArgs[]);
}
and being called
Copy code
export function createVpc(name: string, options: VpcOptions) {
  const { networkBlock, tags } = options;
  const cidr = networkBlock.toString();

  const { zoneIds } = aws.getAvailabilityZones();
  zoneIds.sort();

  const subnets = createSubnets(zoneIds, networkBlock);
  const vpc = new awsx.ec2.Vpc(name, {
    cidrBlock: cidr,
    numberOfAvailabilityZones: 'all',
    subnets,
    tags: {
      ...tags,
      Name: name,
    },
  });

  return { cidr, subnets, vpc };
}
But the point being that (it seems) the subnets need to be actually created before creating the EKS cluster, and I only know the tags that need to be applied to the subnets after I have created the cluster.
Maybe the easiest way would be if I could set a specific name for the EKS cluster when I create it. But I don't see any way of doing that in Pulumi.
c
a
Looks promising. I'll try that. Thanks @chilly-crayon-57653