Skip to main content
How To Create An AWS EKS Cluster Using Pulumi And Golang
  1. Blog/

How To Create An AWS EKS Cluster Using Pulumi And Golang

·2 mins·
Infrastructure as Code with Pulumi and Go - This article is part of a series.
Part 3: This Article

Building a Kubernetes cluster from scratch is hard, which is why managed services exist. In the previous post I added subnets to a VPC. This post uses that VPC to create an AWS EKS cluster.

The complete project is available on GitHub.

Configuration
#

At minimum, you need a cluster name, a Kubernetes version, and an IAM role. Specifying which log types to send to CloudWatch is optional but helpful for debugging. Add this to the YAML file from the previous post:

eks:cluster-name: myEKSCluster
eks:k8s-version: "1.14"
eks:cluster-role-arn: "arn:aws:iam::ACCOUNTID:role/ServiceRoleForAmazonEKS"
eks:cluster-log-types: "api,audit,authenticator,scheduler,controllerManager"

You can use the command line (e.g., pulumi config set eks:cluster-name "myEKSCluster") or edit the YAML file directly. The file is named Pulumi.<name of your project>.yaml.

Creating the cluster
#

This code extends the previous post. It reads the cluster name and log types from the YAML file, uses the subnets created earlier, and calls eks.NewCluster() to create the EKS cluster in your existing VPC.

// Create an EKS cluster
clusterName := getEnv(ctx, "eks:cluster-name", "unknown")
enabledClusterLogTypes := strings.Split(getEnv(ctx, "eks:cluster-log-types", "unknown"), ",")

clusterArgs := &eks.ClusterArgs{
    Name:                   clusterName,
    Version:                getEnv(ctx, "eks:k8s-version", "unknown"),
    RoleArn:                getEnv(ctx, "eks:cluster-role-arn", "unknown"),
    Tags:                   tags,
    VpcConfig:              subnets,
    EnabledClusterLogTypes: enabledClusterLogTypes,
}

cluster, err := eks.NewCluster(ctx, clusterName, clusterArgs)
if err != nil {
    fmt.Println(err.Error())
    return err
}

ctx.Export("CLUSTER-ID", cluster.ID())

Running the code
#

Run pulumi up to create the cluster. If you’re using the same project and stack, Pulumi knows the VPC already exists and will only create the EKS cluster. Fair warning: this can take a while. In my case it was almost 10 minutes.

$ pulumi up
Previewing update (builderstack):

     Type                 Name                  Plan
     pulumi:pulumi:Stack  builder-builderstack
 +   └─ aws:eks:Cluster   myEKSCluster          create

Outputs:
  + CLUSTER-ID: output<string>

Resources:
    + 1 to create
    4 unchanged

Do you want to perform this update? yes
Updating (builderstack):

     Type                 Name                  Status
     pulumi:pulumi:Stack  builder-builderstack
 +   └─ aws:eks:Cluster   myEKSCluster          created

Outputs:
  + CLUSTER-ID: "myEKSCluster"
    SUBNET-IDS: [
        [0]: "subnet-<id>"
        [1]: "subnet-<id>"
    ]
    VPC-ID    : "vpc-<id>"

Resources:
    + 1 created
    4 unchanged

Duration: 9m55s

Permalink: https://app.pulumi.com/retgits/builder/builderstack/updates/3

The permalink at the bottom takes you to the Pulumi console where you can see all the details of the execution and the resources that were created.

Cover image by Gerd Altmann from Pixabay

Infrastructure as Code with Pulumi and Go - This article is part of a series.
Part 3: This Article

Related

How To Create a VPC In AWS Using Pulumi And Golang

·4 mins
Your source code is only one piece of what goes into production. You also need API gateways, S3 buckets, VPCs, and other infrastructure. Configuring those by hand is tedious and error-prone. Pulumi lets you define all of that in the same language you build your app in.

Serverless - From Microservice to Functions

·1 min
Using serverless requires us to change our mindset on how we build apps and requires us to unlearn things we learned building apps in the past. At AWS re:Invent I got a chance to do a VMware Code session and talk about how we took part of our ACME Fitness Shop and transformed it into serverless functions with AWS Lambda.