AWS EKS provides managed Kubernetes clusters as a service. In this guide, you will learn how to provision an AWS EKS Kubernetes cluster with Terraform.
Company Mentioned
AWS EKS provides managed Kubernetes clusters as a service. You’re on AWS and want to avoid getting into the details of setting up a Kubernetes cluster from scratch? EKS is the way to go!In this guide, you will learn how to provision an AWS EKS Kubernetes cluster with Terraform. Let’s start with the basics.
Step 1 - Install Terraform Locally
First, locally install Terraform:
brew install terraform
as well as the AWS CLI:
brew install awscli
and kubectl:
brew install kubernetes-cli
If you’re on a different operating system, please find the respective installation instructions here:
Terraform:
awscli:
kubectl:
Step 2 - Configure the AWS CLI
Now, you’ll need to configure your AWS CLI with access credentials to your AWS account. You can do this by running
aws configure
and providing your Access Key ID and Secret Access Key. You will also need to add the region. For the purposes of this guide, we will use us-east-2. Terraform will later use these credentials to provision your AWS resources.
Step 3 - Get the Code
You can now clone a repository which contains everything you need to set up EKS:
It uses the EKS Terraform module to set up an EKS cluster with 2 worker groups (the actual nodes running your workloads): one with a single medium machine, and one with two small machines.
Step 4 - Run Terraform
You can now create all of those resources using Terraform. First, run
terraform init -upgrade
to initialize the Terraform workspace and download any modules and providers which are used.
In order to do a dry run of the changes to be made, run
terraform plan -out terraform.plan
This will show you that 51 resources will be added, as well as their relevant details. You can then run terraform apply with the resulting plan, in order to actually provision the resources:
terraform apply terraform.plan
This may take a few minutes to finish. You might get a “timed out” error, in which case just repeat both the terraform plan and terraform apply steps.
In the end you will get a list of outputs with their respective values printed out. Make note of your cluster_name.
Step 5 - Connect with Kubectl
In order to use kubectl, which is the main tool to interact with a Kubernetes cluster, you have to give it credentials to your EKS Kubernetes cluster. You can do this by running