Skip to content

Getting Started with Amazon EKS Auto Mode in Pulumi ​

2024-12-16 · florian-stadler

AWS recently announced Amazon EKS Auto Mode, a significant enhancement to Amazon EKS that streamlines cluster management by automating infrastructure decisions and operations. Today, we are excited to announce that Pulumi fully supports EKS Auto Mode across our AWS provider ecosystem, enabling you to leverage this powerful feature through infrastructure as code.

What is EKS Auto Mode? ​

EKS Auto Mode extends AWS management beyond the Kubernetes control plane to the underlying infrastructure that powers your workloads. While traditional EKS requires you to make numerous infrastructure decisions and maintain various components, Auto Mode simplifies this by automating these choices based on AWS's operational expertise.

Instead of manually configuring and managing cluster add-ons, node groups, and infrastructure components, Auto Mode handles:

  • Compute Management: Automatically provisions and scales EC2 instances based on your workload demands while selecting optimal instance types and sizes. It also handles node lifecycle management, including OS updates and security patches.
  • Storage Automation: Eliminates the need to manually set up and configure the EBS CSI driver for block storage. Auto Mode manages volume provisioning, attachment, and cleanup for your persistent workloads.
  • Networking Simplification: Automatically configures and manages core networking components like Load Balancers.

This significantly reduces the operational overhead of running Kubernetes clusters. Instead of spending time on infrastructure decisions and maintenance, teams can now focus on their applications. For example, deploying a stateful application with load balancing no longer requires setting up and maintaining multiple add-ons and configurations - Auto Mode handles this automatically while following AWS best practices.

Common tasks that previously required significant expertise and time are now managed by AWS, making it simpler to run production-grade Kubernetes workloads. This includes:

  • Setting up and maintaining cluster autoscaling
  • Configuring and updating networking plugins
  • Managing storage drivers and volume provisioning
  • Handling node updates and security patches
  • Optimizing instance selection and placement

Pulumi Support for EKS Auto Mode ​

We have added EKS Auto Mode support across our AWS provider ecosystem:

Creating an EKS Auto Mode Cluster ​

Let's explore how to leverage EKS Auto Mode with Pulumi by deploying an example. You can enable EKS Auto Mode by setting autoMode.enabled to true.

First, we'll create a new EKS cluster with Auto Mode enabled. This requires properly configured networking and authentication settings:

  • VPC subnets are tagged appropriately for EKS Auto Mode load balancer discovery
  • Authentication Mode supports Access Entries (Api or ApiAndConfigMap mode) as required by EKS Auto Mode
  • Default node groups and security groups are skipped as Auto Mode manages these
typescript
import * as pulumi from "@pulumi/pulumi";
import * as awsx from "@pulumi/awsx";
import * as eks from "@pulumi/eks";
import * as k8s from "@pulumi/kubernetes";
import { SubnetType } from "@pulumi/awsx/ec2";

const config = new pulumi.Config();
const clusterName = config.require("clusterName");

const eksVpc = new awsx.ec2.Vpc("eks-auto-mode", {
    enableDnsHostnames: true,
    cidrBlock: "10.0.0.0/16",
    subnetSpecs: [
        // Necessary tags for EKS Auto Mode to identify the subnets for the load balancers.
        // See: https://kubernetes-sigs.github.io/aws-load-balancer-controller/v2.1/deploy/subnet_discovery/
        {type: SubnetType.Public, tags: {[`kubernetes.io/cluster/${clusterName}`]: "shared", "kubernetes.io/role/elb": "1"}},
        {type: SubnetType.Private, tags: {[`kubernetes.io/cluster/${clusterName}`]: "shared", "kubernetes.io/role/internal-elb": "1"}},
    ],
    subnetStrategy: "Auto"
});

const cluster = new eks.Cluster("eks-auto-mode", {
    name: clusterName,
    // EKS Auto Mode requires Access Entries, use either the `Api` or `ApiAndConfigMap` authentication mode.
    authenticationMode: eks.AuthenticationMode.Api,
    vpcId: eksVpc.vpcId,
    publicSubnetIds: eksVpc.publicSubnetIds,
    privateSubnetIds: eksVpc.privateSubnetIds,
    // Enables compute, storage and load balancing for the cluster.
    autoMode: {
        enabled: true,
    }
});

Deploying a Load-Balanced Application ​

Now that we have provisioned an EKS cluster with Auto Mode enabled, let's deploy a web application fronted by an Application Load Balancer. EKS Auto Mode will automatically provision appropriately sized nodes for your workload, set up the Application Load Balancer according to the Ingress configuration, and manage ongoing maintenance and updates.

typescript
//...
const appName = "nginx";
const ns = new k8s.core.v1.Namespace(appName, {
    metadata: { name: appName },
}, { provider: cluster.provider });

const configMap = new k8s.core.v1.ConfigMap(appName, {
    metadata: {
        namespace: ns.metadata.name,
    },
    data: {
        "index.html": "<html><body><h1>Hello, Pulumi!</h1></body></html>",
    },
}, { provider: cluster.provider });

const deployment = new k8s.apps.v1.Deployment(appName, {
    metadata: {
        namespace: ns.metadata.name
    },
    spec: {
        selector: { matchLabels: { app: appName } },
        replicas: 3,
        template: {
            metadata: { labels: { app: appName } },
            spec: {
                containers: [{
                    name: appName,
                    image: appName,
                    ports: [{ containerPort: 80 }],
                    volumeMounts: [{ name: "nginx-index", mountPath: "/usr/share/nginx/html" }],
                }],
                volumes: [{
                    name: "nginx-index",
                    configMap: { name: configMap.metadata.name },
                }],
            },
        },
    },
}, { provider: cluster.provider });

const service = new k8s.core.v1.Service(appName, {
    metadata: {
        name: appName,
        namespace: ns.metadata.name
    },
    spec: {
        selector: { app: appName },
        ports: [{ port: 80, targetPort: 80 }],
    },
}, { provider: cluster.provider, dependsOn: [deployment] });

const ingressClass = new k8s.networking.v1.IngressClass("alb", {
    metadata: {
        namespace: ns.metadata.name,
        labels: {
            "app.kubernetes.io/name": "LoadBalancerController",
        },
        name: "alb",
    },
    spec: {
        controller: "eks.amazonaws.com/alb",
    }
}, { provider: cluster.provider });

const ingress = new k8s.networking.v1.Ingress(appName, {
    metadata: {
        namespace: ns.metadata.name,
        // Annotations for EKS Auto Mode to identify the Ingress as internet-facing and target-type as IP.
        annotations: {
            "alb.ingress.kubernetes.io/scheme": "internet-facing",
            "alb.ingress.kubernetes.io/target-type": "ip",
        }
    },
    spec: {
        ingressClassName: ingressClass.metadata.name,
        rules: [{
            http: {
                paths: [{
                    path: "/",
                    pathType: "Prefix",
                    backend: {
                        service: {
                            name: service.metadata.name,
                            port: {
                                number: 80,
                            },
                        },
                    },
                }],
            },
        }],
    }
}, { provider: cluster.provider });

export const url = ingress.status.apply(status => status?.loadBalancer?.ingress?.[0]?.hostname);

After deploying this with pulumi up, you can access your application with curl once the Load Balancer has been provisioned:

bash
~ curl $(pulumi stack output url)
Hello, Pulumi!

Deploying a Stateful Workload ​

EKS Auto Mode simplifies persistent storage management by automatically handling EBS volume provisioning for your stateful workloads. To leverage this feature, you can create a StorageClass that uses the EBS CSI driver (ebs.csi.eks.amazonaws.com) as its provisioner. EKS Auto Mode will then automatically manage the lifecycle of your persistent volumes, including provisioning, attaching, and cleanup.

This is how you'd configure persistent storage for a sample application:

typescript
// ...
const storageClass = new k8s.storage.v1.StorageClass("storage-class", {
    metadata: {
        name: "auto-ebs-sc",
        annotations: {
            "storageclass.kubernetes.io/is-default-class": "true",
        },
    },
    provisioner: "ebs.csi.eks.amazonaws.com",
    volumeBindingMode: "WaitForFirstConsumer",
    parameters: {
        type: "gp3",
        encrypted: "true",
    },
}, {
    provider: cluster.provider,
});
const pvc = new k8s.core.v1.PersistentVolumeClaim("pvc", {
    metadata: {
        name: "auto-ebs-claim",
        namespace: ns.metadata.apply(metadata => metadata.name),
    },
    spec: {
        accessModes: ["ReadWriteOnce"],
        storageClassName: storageClass.metadata.apply(metadata => metadata.name),
        resources: {
            requests: {
                storage: "8Gi",
            },
        },
    },
}, {
    provider: cluster.provider,
});
const statefulWorkload = new k8s.apps.v1.Deployment("stateful-workload", {
    metadata: {
        name: "inflate-stateful",
        namespace: ns.metadata.apply(metadata => metadata.name),
    },
    spec: {
        replicas: 1,
        selector: {
            matchLabels: {
                app: "inflate-stateful",
            },
        },
        template: {
            metadata: {
                labels: {
                    app: "inflate-stateful",
                },
            },
            spec: {
                terminationGracePeriodSeconds: 0,
                nodeSelector: {
                    "eks.amazonaws.com/compute-type": "auto",
                },
                containers: [{
                    name: "bash",
                    image: "public.ecr.aws/docker/library/bash:4.4",
                    command: ["/usr/local/bin/bash"],
                    args: [
                        "-c",
                        "while true; do echo $(date -u) >> /data/out.txt; sleep 60; done",
                    ],
                    resources: {
                        requests: {
                            cpu: "1",
                        },
                    },
                    volumeMounts: [{
                        name: "persistent-storage",
                        mountPath: "/data",
                    }],
                }],
                volumes: [{
                    name: "persistent-storage",
                    persistentVolumeClaim: {
                        claimName: pvc.metadata.name,
                    },
                }],
            },
        },
    },
}, {
    provider: cluster.provider,
});

export const url = ingress.status.loadBalancer.ingress[0].hostname.apply(h => `http://${h}:80`);

Summary ​

EKS Auto Mode represents a significant step forward in simplifying Kubernetes operations on AWS. Pulumi's support for this feature allows you to define EKS Auto Mode clusters using your favorite programming language and integrate them with your existing infrastructure code.

To get started with EKS Auto Mode in Pulumi:

  1. Update your AWS providers to the latest versions
  2. Check out our EKS documentation
  3. Join our Community Slack for support and discussions

We are excited to see what you build with EKS Auto Mode and Pulumi!

Content from the official Pulumi documentation (github.com/pulumi/docs).