Post

Getting Started with Amazon EKS using Terraform

More resources:

Terraform provider for AWS here

Amazon CLI

You can get the Amazon CLI on Docker-Hub We’ll need the Amazon CLI to gather information so we can build our Terraform file.

1
2
3
4
5
# Run Amazon CLI
docker run -it --rm -v ${PWD}:/work -w /work --entrypoint /bin/sh amazon/aws-cli:2.0.43

# some handy tools :)
yum install -y jq gzip nano tar git unzip wget

Login to AWS

1
2
3
4
5
6
7
8
9
10
11
aws configure

Default region name: 
Default output format: json

Or

export AWS_ACCESS_KEY_ID=<AWS_ACCESS_KEY_ID>
export AWS_SECRET_ACCESS_KEY=<AWS_SECRET_ACCESS_KEY>

aws sts get-caller-identity

Find out more about the login here

Install Terraform CLI (Linux 64-bit)

1
2
3
4
5
6
curl -o /tmp/terraform.zip -LO https://releases.hashicorp.com/terraform/1.5.0/terraform_1.5.0_linux_amd64.zip
unzip /tmp/terraform.zip

chmod +x terraform && mv terraform /usr/local/bin/

terraform

Terraform Amazon Kubernetes Provider

Documentation on all the Kubernetes fields for Terraform here

Terraform will allow us to describe everything as a file or multiple files and then piece them together. So what we need to do next is create a Terraform file. We’re going to describe a bunch of variables as inputs, describe a security group for our infrastructure. This Terraform code provisions an EKS cluster in AWS with associated networking resources such as VPC, subnets, and NAT gateways. It also retrieves the kubeconfig for the cluster and saves it as a local file.

Clone the repository from GitHub.

1
2
3
4
terraform init

terraform plan
terraform apply

What we deployed

1
2
3
4
5
6
7
8
9
10
11
12
# grab our EKS config
aws eks update-kubeconfig --name learnk8s --region eu-west-1

# Get kubectl
curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.23.6/bin/linux/amd64/kubectl
chmod +x ./kubectl
mv ./kubectl /usr/local/bin/kubectl

kubectl get nodes
kubectl get deploy
kubectl get pods
kubectl get svc

Clean up

1
terraform destroy
This post is licensed under CC BY 4.0 by the author.

Comments powered by Disqus.