Create Cluster

Create Cluster

In this section, we will deploy our first EKS cluster using the eks-blueprints package. Blueprints published as npm module

You can learn more about Amazon EKS Blueprints for CDK

  1. We edit the main file of lib/my-eks-blueprints-stack.ts:

    • Open the file lib/my-eks-blueprints-stack.ts
    • See the sample code in the file

Deployment Pipeline

  1. Complete the lib/my-eks-blueprints-stack.ts file by pasting (replacing) the following code into the file:
// lib/my-eks-blueprints-stack.ts
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as blueprints from '@aws-quickstart/eks-blueprints';
import { KubernetesVersion } from 'aws-cdk-lib/aws-eks';

export default class ClusterConstruct extends Construct {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id);

    const account = props?.env?.account!;
    const region = props?.env?.region!;

    const blueprint = blueprints.EksBlueprint.builder()
      .account(account)
      .region(region)
      .clusterProvider(
        new blueprints.GenericClusterProvider({
          version: 'auto'
        })
      )
      .addOns()
      .teams()
      .build(scope, id + "-stack");
  }
}

Deployment Pipeline

  1. Open the file bin/my-eks-blueprints.ts to review the sample code.

Deployment Pipeline

  1. In this file, we create a CDK Construct, which is a building block of CDK representing what is necessary to create components of AWS Cloud.

    • In our case, the component is an EKS cluster blueprint placed in provided account, region, add-ons, teams (which we haven’t assigned yet) and all other resources necessary to create the blueprint (e.g., VPC, subnet, etc.). The build() command at the end initializes the cluster blueprint.

    • To actually make a construct usable in a CDK project, we need to add it to our entrypoint.

    • Replace the contents of bin/my-eks-blueprints.ts with the following code block.

// bin/my-eks-blueprints.ts
import * as cdk from 'aws-cdk-lib';
import ClusterConstruct from '../lib/my-eks-blueprints-stack';
import * as dotenv from 'dotenv';

const app = new cdk.App();
const account = process.env.CDK_DEFAULT_ACCOUNT!;
const region = process.env.CDK_DEFAULT_REGION;
const env = { account, region }

new ClusterConstruct(app, 'cluster', { env });

Deployment Pipeline

  1. Create a new .env file.

Deployment Pipeline

  1. Add environment variables:
CDK_DEFAULT_ACCOUNT=XXXXX
CDK_DEFAULT_REGION=XXXX

Deployment Pipeline

Please replace CDK_DEFAULT_ACCOUNT and CDK_DEFAULT_REGION with your own values.

  1. Import Construct to make it available, then use the CDK app to initialize a new object of the CDK Construct we imported. Check CDK:
cdk list
  • If there are no issues, you should see the following result:
cluster-stack

Deployment Pipeline

As you can see, we can leverage EksBlueprint to define our cluster easily using CDK.

Instead of deploying a single cluster, we will utilize the blueprint generator to add a deployment pipeline that can handle all updates for our infrastructure across different environments.