Managing EKS Add-ons with Terraform
Tags: aws, terraform, devops
Published: 2024-03-07 07:05:34

In the ever-evolving landscape of cloud computing, managing Kubernetes clusters efficiently is crucial for developers and operations teams alike. Amazon Elastic Kubernetes Service (EKS) simplifies the process of running Kubernetes on AWS without needing to install, operate, and maintain your own Kubernetes control plane. However, setting up EKS clusters and configuring addons can be complex and time-consuming when done manually. Here’s where Terraform, an open-source infrastructure as code software tool, becomes invaluable. Terraform allows you to automate the deployment of your EKS clusters along with necessary addons, making your infrastructure easily reproducible, scalable, and manageable.
Prerequisites

Before we dive into the details, ensure you have the following prerequisites:

-     An AWS account
-     Terraform installed on your local machine
-     Basic understanding of AWS services and Terraform

**Setting Up Your Terraform Configuration**

To automate the EKS cluster and addons creation, we will use Terraform to define our infrastructure as code. Below is a step-by-step guide to configuring your Terraform files for creating an EKS cluster along with its addons like kube-proxy, vpc-cni, coredns, and aws-ebs-csi-driver.

**Defining EKS Cluster Addons**

First, we define our EKS addons using the aws_eks_addon resource. This resource allows us to specify each addon we want to install on our cluster. We utilize a for_each loop to iterate over a list of addons defined in a variable, creating an addon configuration for each:

_main.tf_

```
resource "aws_eks_addon" "addons" {
  for_each                = { for addon in var.addons : addon.name => addon }
  cluster_name            = var.cluster_name
  addon_name              = each.value.name
  addon_version           = each.value.version
  resolve_conflicts_on_update = "OVERWRITE"
}
```

_variables.tf_

```

variable "aws_region" {
  type        = string
  description = "The AWS region for the provider to deploy resources into."
}

variable "cluster_name" {
  type        = string
  description = "The name of the EKS cluster."
}

variable "addons" {
  type = list(object({
    name    = string
    version = string
  }))
  default = [
    {
      name    = "kube-proxy"
      version = "v1.27.1-eksbuild.1"
    },
    {
      name    = "vpc-cni"
      version = "v1.12.6-eksbuild.2"
    },
    {
      name    = "coredns"
      version = "v1.10.1-eksbuild.1"
    },
    {
      name    = "aws-ebs-csi-driver"
      version = "v1.25.0-eksbuild.1"
    }
  ]
}
```

**Deploying Your Infrastructure**

With your Terraform configuration defined, you’re now ready to deploy your EKS cluster and addons. Follow these steps:

-     Initialize Terraform: Run terraform init to initialize your Terraform workspace. This command will download the necessary Terraform providers.
-     Plan your deployment: Execute terraform plan to review the actions Terraform will perform. This step helps ensure your configuration is correct before applying changes.
-     Apply your configuration: Use terraform apply to create your EKS cluster along with the specified addons. Confirm the action when prompted, and Terraform will begin the deployment process.

**Conclusion**

By leveraging Terraform, you’ve automated the creation of an EKS cluster and efficiently managed its addons, saving time and reducing the potential for human error. This approach not only streamlines your Kubernetes operations in AWS but also ensures your infrastructure is reproducible and easily manageable, paving the way for a more reliable and scalable cloud ecosystem.