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#

For this walkthrough, you'll need:

Create a new project#

Create a new project from the template:

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

The pulumi new command walks you through creating and configuring the 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. Depending on the language, you may also be asked for your preferred package manager or toolchain.

This template’s settings include:

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 values like these 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>

Review the code#

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

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

Take a moment to open them up and have a look. Here’s what’s in index.ts:

index.ts
import * as pulumi from "@pulumi/pulumi";
import * as awsx from "@pulumi/awsx";
import * as eks from "@pulumi/eks";
// Grab some values from the Pulumi configuration (or use default values)
const config = new pulumi.Config();
const minClusterSize = config.getNumber("minClusterSize") || 3;
const maxClusterSize = config.getNumber("maxClusterSize") || 6;
const desiredClusterSize = config.getNumber("desiredClusterSize") || 3;
const eksNodeInstanceType = config.get("eksNodeInstanceType") || "t3.medium";
const vpcNetworkCidr = config.get("vpcNetworkCidr") || "10.0.0.0/16";
// Create a new VPC
const eksVpc = new awsx.ec2.Vpc("eks-vpc", {
enableDnsHostnames: true,
cidrBlock: vpcNetworkCidr,
});
// Create the EKS cluster
const eksCluster = new eks.Cluster("eks-cluster", {
// Put the cluster in the new VPC created earlier
vpcId: eksVpc.vpcId,
// Use the "API" authentication mode to support access entries
authenticationMode: eks.AuthenticationMode.Api,
// Public subnets will be used for load balancers
publicSubnetIds: eksVpc.publicSubnetIds,
// Private subnets will be used for cluster nodes
privateSubnetIds: eksVpc.privateSubnetIds,
// Change configuration values to change any of the following settings
instanceType: eksNodeInstanceType,
desiredCapacity: desiredClusterSize,
minSize: minClusterSize,
maxSize: maxClusterSize,
// Do not give the worker nodes public IP addresses
nodeAssociatePublicIpAddress: false,
// Change these values for a private cluster (VPN access required)
endpointPrivateAccess: false,
endpointPublicAccess: true,
});
// Export some values for use elsewhere
export const kubeconfig = eksCluster.kubeconfig;
export const vpcId = eksVpc.vpcId;

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 15 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
kubectl --kubeconfig kubeconfig 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.

Need help?

If pulumi up reports No valid credential sources found or ExpiredToken, sign in again:

Terminal window
aws login

Still stuck? Let us know.

Customize the project#

Projects created with this 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 couple of minutes to start and join the cluster. Then list the nodes again; there are four:

Terminal window
kubectl --kubeconfig kubeconfig 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 platform for humans and agents.