https://pulumi.com logo
#typescript
Title
# typescript
a

alert-monitor-93874

08/27/2019, 9:09 AM
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

glamorous-printer-66548

08/27/2019, 7:53 PM
how are u creating the subnets, i.e. where’s the relevant code?
a

alert-monitor-93874

08/27/2019, 8:51 PM
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

chilly-crayon-57653

08/28/2019, 9:06 PM
a

alert-monitor-93874

08/28/2019, 9:09 PM
Looks promising. I'll try that. Thanks @chilly-crayon-57653
2 Views