Kickstart Your Kubernetes Journey: Deploy AWS EKS with Terraform
Introduction
Deploying and managing containerized applications at scale can be a daunting task, but Kubernetes has made it easier than ever. And when it comes to setting up Kubernetes clusters in the cloud, AWS Elastic Kubernetes Service (EKS) is a powerful option. In this post, we’ll walk through a hands-on example of deploying Kubernetes on AWS EKS using Terraform.
But first, let’s make one thing clear: Kubernetes is not a secret Greek island where all your servers vacation; it’s much cooler!
Why Choose AWS EKS?
AWS EKS simplifies running Kubernetes on AWS without the need to operate your own Kubernetes control plane or nodes. This managed service takes care of all the grunt work, from control plane provisioning to automatic upgrades and patches. This allows teams to focus more on their application logic and less on the infrastructure.
Prerequisites
Before we dive into the deployment steps, you will need:
- An AWS account with the necessary IAM permissions to create EKS clusters and associated resources.
- Terraform installed on your local machine. If not, follow the official Terraform installation guide.
- A basic understanding of Terraform and Kubernetes concepts.
Installing Terraform
Let’s kick things off by installing Terraform. Follow the steps below to install Terraform on your operating system:
For Windows:
choco install terraform
For MacOS:
brew install terraform
For Linux (Ubuntu, apt):
sudo apt-get update && sudo apt-get install -y software-properties-common
wget -O- https://apt.releases.hashicorp.com/gpg | gpg --dearmor | sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg
gpg --batch --yes --import /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt-get update
sudo apt-get install terraform
Terraform Configuration for AWS EKS
Now let’s move on to the fun part—actual deployment. We’ll start by creating a new directory for our project and setting up the main configuration files.
Step 1: Set up Your Terraform Directory
Create a directory for your Terraform project:
mkdir terraform-eks
cd terraform-eks
Step 2: Create a Provider Configuration File
Create a new file named `provider.tf` and add the following content:
provider "aws" {
region = "us-west-2"
profile = "default"
}
Make sure to replace `us-west-2` with your desired AWS region and `default` with your AWS profile name.
Step 3: Define the EKS Cluster
Create a new file named `eks-cluster.tf` and add the configuration for the EKS cluster:
resource "aws_eks_cluster" "my_eks_cluster" {
name = "my-eks-cluster"
role_arn = aws_iam_role.eks_cluster_role.arn
vpc_config {
subnet_ids = aws_subnet.eks_subnet[*].id
}
}
resource "aws_iam_role" "eks_cluster_role" {
name = "eks-cluster-role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "eks.amazonaws.com"
}
},
]
})
}
Step 4: Define Node Groups
Now, let’s define the worker nodes. Create a file named `node-group.tf`:
resource "aws_eks_node_group" "my_eks_node_group" {
cluster_name = aws_eks_cluster.my_eks_cluster.name
node_group_name = "my-eks-node-group"
node_role_arn = aws_iam_role.eks_node_role.arn
subnet_ids = aws_subnet.eks_subnet[*].id
scaling_config {
desired_size = 2
max_size = 5
min_size = 1
}
}
resource "aws_iam_role" "eks_node_role" {
name = "eks-node-role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "ec2.amazonaws.com"
}
},
]
})
}
Step 5: Add Networking
Create a file named `networking.tf` and set up the required VPC and subnets:
resource "aws_vpc" "my_vpc" {
cidr_block = "10.0.0.0/16"
}
resource "aws_subnet" "eks_subnet" {
count = 2
vpc_id = aws_vpc.my_vpc.id
cidr_block = cidrsubnet(aws_vpc.my_vpc.cidr_block, 8, count.index)
availability_zone = element(["us-west-2a", "us-west-2b"], count.index)
}
Deploying the Configuration
Step 1: Initialize Terraform
First, initialize your Terraform workspace to download the required providers and modules:
terraform init
Step 2: Plan your Deployment
Run the following command to see an execution plan:
terraform plan
This command will display the actions Terraform will perform to achieve the desired state.
Step 3: Apply the Configuration
Finally, deploy your resources with:
terraform apply
Type `yes` when prompted to confirm.
Making Sure Everything is Up and Running
Once the deployment is complete, you can check your EKS cluster from the AWS Management Console or use AWS CLI to verify:
aws eks --region us-west-2 describe-cluster --name my-eks-cluster --query "cluster.status"
Final Thoughts
Deploying Kubernetes on AWS EKS using Terraform provides a repeatable and modular approach to infrastructure management. Whether you’re just getting started or looking to fine-tune your existing setup, Terraform makes it easy to define, provision, and manage your EKS clusters. Remember, even Kubernetes’ pods sometimes need personal space—so don’t crowd them too much!
Looking for more detailed instructions and best practices? Check out the official AWS EKS Documentation (opens in a new tab).
Continuing the Journey
If you made it this far, you must be pretty excited to see your EKS cluster live and buzzing! Now that you have a foundational cluster set up, it’s time to layer in some advanced configurations and delve deeper into the Kubernetes ecosystem.
Monitoring and Logging
Ensuring the health and performance of your cluster is crucial. Tools like Prometheus and Grafana can be incredibly helpful. For logging, consider integrating with Amazon CloudWatch to capture logs from your containers.
Security Best Practices
Use AWS IAM roles for service accounts (IRSA) to securely grant Kubernetes workloads permissions to AWS services. Enable role-based access control (RBAC) within Kubernetes to manage who has access to your cluster and what they can do.
Scaling and Updates
Leverage Kubernetes’ Horizontal Pod Autoscaler (HPA) to automatically scale your application based on CPU utilization or other select metrics. Keep your cluster updated by applying regular patches and upgrades to your AWS EKS nodes and Kubernetes version.
Backup and Disaster Recovery
Implement regular backups of your Kubernetes resources. Tools like Velero can help you back up and restore your Kubernetes cluster resources and persistent volumes.
Conclusion
Deploying Kubernetes with Terraform on AWS EKS is a powerful way to manage your cloud-native applications. This guide provides a solid starting point, but there’s always more to learn and explore. Keep an eye out for future posts where we’ll dive into advanced Kubernetes features, best practices for running stateful applications, and more!
Remember: Technology may be complex, but your approach to it doesn’t have to be. Happy deploying, and as always, stay curious!