constructor()

in cdk/lib/cdk-stack.ts [29:88]


  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    // Add EKS cluster
    const cluster = new eks.Cluster(this, 'PlacementGroupDemoEKS', {
      version: eks.KubernetesVersion.V1_21,
      defaultCapacity: 0,
    });

    // Shared node group configuration
    const sharedNgConfig = {
      instanceTypes: [
        ec2.InstanceType.of(ec2.InstanceClass.C5, ec2.InstanceSize.LARGE),
      ],
      maxSize: NUMBER_OF_INSTANCES_PER_GROUP,
      minSize: NUMBER_OF_INSTANCES_PER_GROUP,
      desiredSize: NUMBER_OF_INSTANCES_PER_GROUP,
      subnets: {
        subnets: [
          cluster.vpc.publicSubnets[0], // Force all worker nodes to be in same AZ for fair performance comparison.
        ],
      },
    };

    /**
     * Add a node group with all instances in the same placement group of `cluster` type
     */
    const pg = new ec2.CfnPlacementGroup(this, 'PlacementGroup', {
      strategy: 'cluster',
    });
    const lt = new ec2.LaunchTemplate(this, 'PlacementGroupLaunchTemplate');
    const cfnLt = lt.node.defaultChild as ec2.CfnLaunchTemplate;
    cfnLt.addOverride('Properties.LaunchTemplateData.Placement.GroupName', pg.ref);

    cluster.addNodegroupCapacity('NgTruePlacementGroup', {
      ...sharedNgConfig,
      labels: {
        placementGroup: 'true',
      },
      launchTemplateSpec: {
        id: lt.launchTemplateId!,
        version: lt.latestVersionNumber,
      },
    });

    /**
     * Add another node group with same configurations except not in a placement group but in same AZ
     */
    cluster.addNodegroupCapacity('NgFalsePlacementGroup', {
      ...sharedNgConfig,
      labels: {
        placementGroup: 'false',
      },
    });

    /**
     * Enable cluster logging
     */
    setupClusterLogging(this, cluster);
  }