Skip to main content

Search the Dev Center

Search tutorials, templates, examples, and more.

Kubernetes Cluster on AWS

The AWS Kubernetes Cluster template scaffolds a Pulumi project that provisions a managed Amazon EKS cluster inside a new VPC with public and private subnets. Worker nodes run in the private subnets for improved security; load balancers created by cluster workloads are automatically placed in the public subnets.

Architecture diagram of the AWS Kubernetes Cluster template AWS VPC Amazon EKS Public Subnet Private Subnet EKS API Endpoint KubernetesResources Worker Nodes

Before you begin#

Make sure you’ve installed Pulumi and configured your AWS credentials.

Create a new project#

Create a new project using the template in the language of your choice:

Terminal window
mkdir my-k8s-cluster && cd my-k8s-cluster
pulumi new kubernetes-aws-typescript

pulumi new walks you through creating and configuring a new project and stack. A stack is an instance of a project and you can have many of them — dev, staging, prod — each with its own configuration settings. The template also includes some additional settings:

KeyDescriptionDefault
aws:regionThe AWS region to deploy intous-west-2
minClusterSizeMinimum size (number of nodes) of cluster3
maxClusterSizeMaximum size (number of nodes) of cluster6
desiredClusterSizeDesired number of nodes in the cluster3
eksNodeInstanceTypeInstance type to use for worker nodest3.medium
vpcNetworkCidrNetwork CIDR to use for new VPC10.0.0.0/16
You can change these settings later with pulumi config set:
Terminal window
pulumi config set aws:region <value>
pulumi config set minClusterSize <value>
pulumi config set maxClusterSize <value>
pulumi config set desiredClusterSize <value>
pulumi config set eksNodeInstanceType <value>
pulumi config set vpcNetworkCidr <value>

When setup completes, you’ll see a few key files:

  • Pulumi.yaml names the project and the language it’s written in.
  • Pulumi.dev.yaml holds the dev stack’s settings, including the values you just entered.
  • index.ts is the program: the code that declares your cloud resources.

Deploy the project#

Deploy with pulumi up. It starts with a preview of what it’s about to create and asks before it changes anything. Choose yes to deploy. An EKS cluster takes 10 to 20 minutes to create, so expect pulumi up to run that long.

Terminal window
pulumi up

Pulumi adds a random suffix to most resource names, so the cluster declared as eks-cluster gets a name like eks-cluster-eksCluster-4f8a2c1. This is autonaming. It lets you deploy the same program to more than one stack, and lets Pulumi create a replacement before it deletes the old resource, without names colliding.

When it finishes, Pulumi prints the stack’s outputs, values it only knows once the resources exist:

kubeconfig

The cluster’s kubeconfig file, which you can use with kubectl to access and communicate with your cluster.

vpcId

The ID of the VPC that your cluster is running in.

Read one any time with pulumi stack output <name>, or pass it to another command. Save the kubeconfig to a file and point kubectl at it to list the cluster’s nodes:

Terminal window
pulumi stack output kubeconfig --show-secrets > kubeconfig
KUBECONFIG=./kubeconfig kubectl get nodes

You should see three nodes with the status Ready. The kubeconfig gets its login token from the AWS CLI, so aws needs to be installed.

Customize the project#

Projects created with the Kubernetes template expose the following configuration settings:

minClusterSize

The minimum number of nodes to allow in the cluster. Defaults to 3.

maxClusterSize

The maximum number of nodes to allow in the cluster. Defaults to 6.

desiredClusterSize

The desired number of nodes in the cluster. Defaults to 3.

eksNodeInstanceType

The EC2 instance type to use for the nodes. Defaults to t3.medium.

vpcNetworkCidr

The network CIDR to use for the VPC. Defaults to 10.0.0.0/16.

All of these settings are optional and may be adjusted either by editing the stack configuration file directly (by default, Pulumi.dev.yaml) or by changing their values with pulumi config set. For example, add a fourth node:

Terminal window
pulumi config set desiredClusterSize 4
pulumi up

The new node takes a few minutes to start and join the cluster. Then list the nodes again; there are four:

Terminal window
KUBECONFIG=./kubeconfig kubectl get nodes

To see what else you can add, browse the AWS provider in the Pulumi Registry.

Cleaning up#

You can cleanly destroy the stack and all of its infrastructure with pulumi destroy:

Terminal window
pulumi destroy

Learn more#

Related

The infrastructure as code platform for any cloud.