Image for post
Image for post

Create EKS with an Existing VPC

Provision Amazon EKS cluster with Existing VPC using Eksctl

Now that we have an existing VPC infrastructure, we can provision Amazon EKS. In this article I will cover two main topic take-aways:

  • Using Terraform templatefile as craft the eksctl configuration.

Previous Article

This code will create a EKS-ready VPC cluster: private and public subnets per availability zone and tag appropriately for EKS.

Tools

  • AWS CLI: this allow programmatic access to AWS cloud.
  • eksctl: this is provisioning tool we’ll use to create EKS cluster.
  • Terraform: this is provisioning and templating tool used to create eksctl configuration based on existing infrastructure.

Method 1: The Labor Intensive Way

The eksctl command line tool can create a cluster by either command-line options or using a eksctl config file to define our infrastructure. The default method to provision EKS with this tool is to create both the VPC and EKS that uses that VPC, but this is not as flexible. You can create a configuration that uses the existing VPC, provided that you tell EKS all the private and public subnets for EKS to use.

Part 1: Lovingly Handcraft the Config

Below is an example eksctl config file with fictitious subnet ids. You will make some edits before using this.

  • Subnet IDs to Match Availability Zones: you’ll need to look these up and cross reference the availability zone, if it is private or public, and copy/paste the subnet id
  • Instance Type: the m5.2xlarge has 8 vCPUs and 32 GiB memory, so you may want to change this to something that matches the desired workload.
  • KeyPair: Occasionally you may need to update the worker nodes (EC2 instances) to troubleshoot or apply security patches.

Provision the Cluster

Once finished, assuming you used the same name, you can provision EKS using this command:

eksctl create cluster --config-file ./cluster_config.yaml

Method 2: Terraform Creates the Config

Wouldn’t it be cool if we could have a tool that can fetch the subnets created earlier, match them to the corresponding avialability zone, and then use these to automatically create a configuration file we can use?!?

.
├── main.tf
├── provider.tf
├── terraform.tfvars
└── vpc
├── locals.tf
├── main.tf
├── variables.tf
└── versions.tf

Part 2.0: Create Config Module

Now we’ll create the eksctl_config module. In bash, we can create the module with the following:

mkdir eksctl_config
touch eksctl_config/{data,locals,main,variables}.tf
touch eksctl_config/cluster_config.yaml.tmpl
.
├── eksctl_config
│ ├── cluster_config.yaml.tmpl
│ ├── data.tf
│ ├── locals.tf
│ ├── main.tf
│ └── variables.tf

├── main.tf
├── provider.tf
└── vpc
├── locals.tf
├── main.tf
├── variables.tf
└── versions.tf

Part 2.1 Variables

Let’s populate the variables we’ll use:

Part 2.2 Main

The main will have one single resource local_file to create the file we want.

Part 2.3 Data Sources

Given a list of private and public subnet ids from variables, we need to find out the corresponding availability zone where these lives. We can do that with a data source.

Part 2.4 Local Variables

We want to create a map of variables cluster_config_vars that we can pass to templatefile(), which will use this along with our template file to render a final result as the string cluster_config_values, the content of the file we’ll create.

Part 2.5 The Template File

The final part of this puzzle is the actual template file that templatefile() will ingest.

Part 3: Add Output Variables from VPC Module

In the previous article, we created vpc module that will stand-up the configuration. You’ll want to reuse or copy this code to add a new file called vpc/output.tf that will have the following content:

.
├── eksctl_config
│ ├── cluster_config.yaml.tmpl
│ ├── data.tf
│ ├── locals.tf
│ ├── main.tf
│ └── variables.tf
├── main.tf
├── provider.tf
├── terraform.tfvars
└── vpc
├── locals.tf
├── main.tf
├── output.tf
├── variables.tf
└── versions.tf

Part 4: Update Main Config

In the previous article, we had a main.tf that used a vpc module. We’ll update this to use the module we just created.

Part 5: Create the EKS Cluster

The final result of all of this is cluster_config.yaml file after running terraform apply. From here we can create the EKS cluster with the following command:

eksctl create cluster --config-file ./cluster_config.yaml

Method 2.1: But Wait There’s More

Some might have spotted immediately that the eksctl_config module requires passing in two lists (private and public subnets). This would be cumbersome to manually specify this if the VPC was not created with same code.

Variables

We remove the variables for two subnet lists and replace this with a single vpc_id.

Data

We’ll have four data sources now:

eksctl_config/data.tf

Locals

We can use the feature in Terraform 0.12 language to dynamically build a map from the data sources.

# create cluster_config.yaml
terraform
apply
# provision using cluster_config.yaml
eksctl
create cluster --config-file ./cluster_config.yaml

Clean Up

Before deleting a cluster, you should remove any persistent storage by deleting pvc resources, or these will become orphaned and eat up costs. Also, it’s good to delete any ingresses or services that use ELB.

Delete the Cluster

When ready, you can remove EKS with this command:

eksctl delete cluster --config-file ./cluster_config.yaml

Resources

These are some links I used to reference material to create this blog:

Blog Source Code

Terraform

Terraform Bugs

Eksctl

Conclusion

There you have it, whether you use the hand crafted static version or dynamically generated this automatically with Terraform, you can now quickly provision and de-provision EKS clusters while reusing existing VPC infrastructure.

  • How to use Terraform templating system to create files such as these

Written by

Linux NinjaPants Automation Engineering Mutant — exploring DevOps, Kubernetes, CNI, IAC

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store