I'm using Pulumi to create an AWS VPC with explici...
# typescript
a
I'm using Pulumi to create an AWS VPC with explicitly created subnets. I'm using these to create an EKS cluster
Copy code
const subnetIds = [
    ...vpc.privateSubnetIds,
    ...vpc.publicSubnetIds,
  ];

  const cluster = new eks.Cluster(name, {
    deployDashboard: false,
    enabledClusterLogTypes: ['api', 'audit', 'authenticator', 'controllerManager', 'scheduler'],
    instanceRoles: [role],
    skipDefaultNodeGroup: true,
    subnetIds,
    vpcId: vpc.id,
  }, { dependsOn: [vpc] });
I then need to go back through all the subnets and add a tag
<http://kubernetes.io/cluster/<cluster-name>:shared|kubernetes.io/cluster/<cluster-name>:shared>
, but I don't see how I can go back over the existing subnets to add the tags. Does anyone know the correct way to do this?
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