Mastering AWS EKS and Add-ons Deployment with Terraform
- sarat chandra
- Oct 5
- 4 min read
Managing Kubernetes clusters can feel overwhelming, especially when scaling and maintaining them. Fortunately, Amazon Elastic Kubernetes Service (EKS) streamlines this process. It provides a managed Kubernetes service that allows you to run Kubernetes without the need to install and operate your own control plane or nodes. This blog post will guide you through creating an AWS EKS cluster and deploying add-ons using Terraform, an effective Infrastructure as Code (IaC) tool.
Understanding AWS EKS
Amazon EKS is a fully managed service that simplifies running Kubernetes on AWS. You do not need to install and operate your Kubernetes control plane. Instead, EKS takes care of availability and scalability for you. For example, in 2022, Amazon reported that EKS can automatically handle over 1 million requests per second with minimal setup.
EKS integrates seamlessly with other AWS services, creating a solid platform for deploying containerized applications. You can leverage Kubernetes's power while enjoying AWS's top-notch security, scalability, and reliability features.
Why Use Terraform for EKS?
Terraform is an open-source tool for defining and provisioning infrastructure using a straightforward configuration language. It helps you manage infrastructure as code, which makes it easier to track changes and collaborate within teams.
Using Terraform with AWS EKS offers several advantages:
Consistency: Infrastructure can be provisioned consistently across multiple environments, which is especially useful when scaling from development to production.
Version Control: Changes can be tracked and managed within version control systems, such as Git. For instance, you can review and roll back changes if necessary.
Automation: Terraform automates the entire provisioning process. This automation helps reduce human error, lowering the chances of misconfigurations that could lead to downtime.
Modularity: You can create reusable components with Terraform modules, simplifying the management of complex infrastructures. Many organizations report reducing deployment time by up to 60% by using modular configurations.
Prerequisites
Before creating an EKS cluster with Terraform, ensure you have the following prerequisites:
AWS Account: You must have an active AWS account to create and manage resources.
Terraform Installed: Download and install Terraform from the official website.
AWS CLI Installed: Install the AWS Command Line Interface (CLI) to interact with AWS services.
kubectl Installed: Install `kubectl`, the command-line tool for managing Kubernetes clusters.
IAM Permissions: Ensure your AWS user has permissions to create EKS clusters and associated resources, such as network interfaces and security groups.
Setting Up Your Terraform Configuration
To begin, create a new directory for your Terraform configuration files. Within this directory, create a file named `main.tf`. This file will contain the configuration for your EKS cluster.
Provider Configuration
Start by configuring the AWS provider in your `main.tf` file:
```hcl
provider "aws" {
region = "us-west-2" # Change to your desired region
}
```
Creating the EKS Cluster
Next, add the configuration for the EKS cluster:
```hcl
resource "aws_eks_cluster" "my_cluster" {
name = "my-eks-cluster"
role_arn = aws_iam_role.eks_cluster_role.arn
vpc_config {
subnet_ids = aws_subnet.my_subnet.*.id
}
depends_on = [aws_iam_role_policy_attachment.eks_cluster_policy]
}
```
IAM Roles and Policies
You need to create IAM roles and policies for the EKS cluster. Add the following code to your `main.tf` file:
```hcl
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"
}
},
]
})
}
resource "aws_iam_role_policy_attachment" "eks_cluster_policy" {
policy_arn = "arn:aws:iam::aws:policy/AmazonEKSClusterPolicy"
role = aws_iam_role.eks_cluster_role.name
}
```
VPC and Subnets
EKS requires a Virtual Private Cloud (VPC) and subnets to operate. You can create a new VPC and subnets in your `main.tf` file:
```hcl
resource "aws_vpc" "my_vpc" {
cidr_block = "10.0.0.0/16"
}
resource "aws_subnet" "my_subnet" {
count = 2
vpc_id = aws_vpc.my_vpc.id
cidr_block = "10.0.${count.index}.0/24"
availability_zone = element(data.aws_availability_zones.available.names, count.index)
}
```
Deploying the EKS Cluster
With your configuration in place, it's time to deploy the EKS cluster. Open your terminal, navigate to your Terraform configuration directory, and run the following commands:
```bash
terraform init
terraform apply
```
Terraform will prompt you to confirm the changes. Type `yes` to proceed. Terraform will create the EKS cluster and associated resources.
Configuring kubectl
Once the EKS cluster is created, configure `kubectl` to interact with your new cluster. You can do this by running:
```bash
aws eks --region us-west-2 update-kubeconfig --name my-eks-cluster
```
Replace `us-west-2` and `my-eks-cluster` with your region and cluster name.
Adding Add-ons to Your EKS Cluster
EKS supports various add-ons that enhance the functionality of your Kubernetes cluster. Some popular add-ons include:
Amazon VPC CNI: This provides networking capabilities for your pods, essential for smooth communication.
CoreDNS: A DNS server crucial for name resolution within your Kubernetes services. This is especially helpful since 85% of enterprise applications rely on effective DNS management.
Kube-proxy: Manages network routing for your services, ensuring efficient traffic flow.
Deploying Add-ons with Terraform
You can deploy add-ons using Terraform by adding the following resources to your `main.tf` file:
```hcl
resource "aws_eks_addon" "vpc_cni" {
cluster_name = aws_eks_cluster.my_cluster.name
addon_name = "vpc-cni"
service_account_role_arn = aws_iam_role.eks_cluster_role.arn
}
resource "aws_eks_addon" "coredns" {
cluster_name = aws_eks_cluster.my_cluster.name
addon_name = "coredns"
service_account_role_arn = aws_iam_role.eks_cluster_role.arn
}
```
Applying the Add-ons
After adding the add-ons to your configuration, run the following command again:
```bash
terraform apply
```
This will deploy the specified add-ons to your EKS cluster.
Verifying Your EKS Cluster and Add-ons
To verify that your EKS cluster and add-ons are running correctly, use these `kubectl` commands:
```bash
kubectl get nodes
kubectl get pods -n kube-system
```
These commands show the status of your nodes and the pods running in the `kube-system` namespace, where most add-ons are deployed.
Final Thoughts
In this post, we covered how to create an AWS EKS cluster and deploy add-ons using Terraform. By utilizing Terraform's Infrastructure as Code capabilities, you can manage your Kubernetes infrastructure in an optimized, automated way.
As you navigate your journey with AWS EKS and Terraform, consider exploring features like scaling your cluster, integrating CI/CD pipelines, and implementing monitoring solutions. With the right tools and practices in place, you can excel in deploying and managing Kubernetes clusters on AWS.




Comments