Following our setup of the foundational AWS infrastructure using Terraform, we now turn our attention to the heart of our cloud-native microservices architecture: Amazon Elastic Kubernetes Service (EKS). Amazon EKS simplifies the process of running Kubernetes, a powerful open-source system for automating the deployment, scaling, and management of containerized applications. In this article, we’ll demonstrate how to use Terraform to automate the creation of an EKS cluster, integrating it seamlessly with the infrastructure we’ve already provisioned.

Why Amazon EKS?

Amazon EKS offers a managed Kubernetes service that takes care of the heavy lifting involved in setting up a Kubernetes cluster. It automatically manages the Kubernetes control plane, a critical component that manages cluster state and configuration. With EKS, you get the scalability and flexibility of Kubernetes without the operational overhead, making it an ideal choice for deploying microservices at scale.

Prerequisites

Before proceeding, ensure you’ve completed the infrastructure setup from Part 1 of this series, as the EKS cluster will be deployed within that infrastructure. You should have Terraform and the AWS CLI installed and configured on your system. Additionally, you’ll need kubectl, the Kubernetes command-line tool, to interact with your EKS cluster once it’s up and running.

Creating the EKS Cluster with Terraform

Step 1: Define the EKS Cluster in Terraform

First, we need to extend our Terraform configuration to include the EKS cluster. Open your main.tf file (or create a new Terraform configuration file dedicated to EKS) and add the following configurations:

EKS Cluster

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.eks_subnet1.id, aws_subnet.eks_subnet2.id]
  }

  depends_on = [
    aws_iam_role_policy_attachment.eks_cluster_policy,
  ]
}

EKS Node Group

resource "aws_eks_node_group" "my_node_group" {
  cluster_name    = aws_eks_cluster.my_cluster.name
  node_group_name = "my-eks-node-group"
  node_role_arn   = aws_iam_role.eks_node_group_role.arn
  subnet_ids      = [aws_subnet.eks_subnet1.id, aws_subnet.eks_subnet2.id]

  scaling_config {
    desired_size = 2
    max_size     = 3
    min_size     = 1
  }
}

Step 2: Apply the Terraform Configuration

After defining your EKS cluster and node group in Terraform, apply the configuration:

terraform apply

Terraform will show you the plan and prompt for confirmation. Type yes to proceed. Terraform will then provision the EKS cluster and node group according to your specifications.

Step 3: Configure kubectl to Connect to Your EKS Cluster

After the cluster is created, you’ll need to configure kubectl to interact with it. You can do this by updating your kubeconfig file with the following command:

aws eks --region <your-region> update-kubeconfig --name my-eks-cluster

Replace <your-region> with your AWS region. This command updates your kubeconfig file with the necessary details to connect to your EKS cluster.

Conclusion

You’ve now successfully automated the deployment of an Amazon EKS cluster using Terraform, laying down the infrastructure for running containerized microservices. With the cluster and node group in place, you’re ready to deploy applications into this managed Kubernetes environment.

Gotchas and Tips

  • IAM Roles: Ensure the IAM roles for your EKS cluster and node group have the correct policies attached for EKS to function properly.
  • Subnet Selection: Use subnets across different availability zones to ensure high availability of your EKS cluster.
  • Version Compatibility: Keep an eye on the Kubernetes version supported by EKS and ensure your kubectl tool is compatible.
  • Security Groups: Review and adjust the security group rules for your node group to match your application’s network access requirements.

In the next part of our series, we’ll explore how to manage microservices deployments using Helm, further enhancing our cloud-native infrastructure. Stay tuned to continue building a robust, scalable, and observable microservices architecture on Amazon EKS.