keepcalculator

06 Numbers and Powers

Kubernetes Terraform AWS: How They Work Together

Card No. 06·37862 · Updated · Standard formula, shown below

Average pace 5.00 min/km
Average speed
12.00 km/h
Result
Finish time
50 min
You entered
Distance
10 km
You entered
You enter
Finish time, Distance
You get
Average pace, Average speed
Rounding
2 dp

Kubernetes, Terraform, and AWS work together as a layered automation stack: Terraform provisions and manages the AWS infrastructure (networks, clusters, storage), Kubernetes orchestrates the containerized applications that run on that infrastructure, and AWS provides the underlying compute, networking, and managed services. In practice, you use Terraform to create an Amazon EKS cluster and its dependencies, then deploy and manage your container workloads on that cluster with Kubernetes tools like kubectl or Helm. This separation of concerns lets each tool do what it does best: Terraform handles infrastructure lifecycle, Kubernetes handles application lifecycle, and AWS supplies the raw building blocks.

What Each Tool Does

To understand the combination, you need a clear picture of each piece.

  • AWS is the cloud provider. It offers virtual servers (EC2), managed Kubernetes (EKS), databases (RDS), load balancers, and hundreds of other services. AWS is the physical and logical foundation: you rent its data centers and networking.
  • Terraform is an Infrastructure as Code (IaC) tool. You write configuration files (in HCL) that declare the AWS resources you want—VPCs, subnets, security groups, IAM roles, and EKS clusters. Terraform then creates, updates, or deletes those resources in the correct order, keeping a state file that tracks what it manages.
  • Kubernetes is a container orchestration platform. It schedules and runs Docker containers across a cluster of machines, handles scaling, rolling updates, service discovery, and self-healing. You interact with it via the kubectl command and YAML manifests that describe your desired state (deployments, services, configmaps).

In a typical setup, Terraform creates the EKS cluster and the node group (the EC2 instances that run your containers). After that, you use Kubernetes to deploy your actual application—say, a web server and a database—onto that cluster. The two tools rarely overlap: Terraform does not deploy containers, and Kubernetes does not create cloud resources (though it can, via controllers, but that’s an advanced pattern).

How They Interact in a Real Workflow

Here’s a step-by-step view of how a team might use all three together:

  1. Write Terraform code that defines a VPC with public and private subnets, an internet gateway, and a route table. This is the network foundation.
  2. Add the EKS module (e.g., the official Terraform AWS EKS module) to create the control plane, node group, and the necessary IAM roles. Terraform also configures the aws-auth ConfigMap so your IAM users can access the cluster.
  3. Run terraform apply. Terraform calls the AWS API to create everything, waits for the cluster to become active, and outputs the kubeconfig needed to connect.
  4. Configure kubectl with that kubeconfig, then apply your Kubernetes manifests: kubectl apply -f deployment.yaml. This deploys your containers, services, and ingress rules.
  5. Manage changes: if you need more nodes, update the Terraform code and run apply again. If you need to scale your app, change the replica count in the Kubernetes manifest and run kubectl apply.

This separation means each tool has a clear scope. Terraform is the “infrastructure” layer; Kubernetes is the “application” layer. AWS is the substrate both sit on.

Why Use Both Instead of One?

You might wonder: can’t you just use AWS’s native tools (CloudFormation, or the AWS console) or just use Kubernetes alone? You can, but the combination offers distinct advantages:

  • Declarative everything: Both Terraform and Kubernetes let you describe the desired end state in code, which enables version control, code review, and rollback.
  • Portability: Kubernetes abstracts away the cloud provider. If you ever want to move from AWS to GCP or Azure, your Kubernetes manifests stay mostly the same; only the Terraform code changes.
  • Separation of concerns: Infrastructure engineers can focus on Terraform, while application developers use Kubernetes without needing to understand VPCs or IAM.
  • Lifecycle management: Terraform is great at creating and destroying infrastructure cleanly. Kubernetes is great at rolling out updates and healing failed containers. They complement each other.

One common misconception is that Kubernetes replaces Terraform. It doesn’t. Kubernetes runs on top of infrastructure; something has to create that infrastructure. Terraform is often that something.

Common Problems and Pitfalls

Even with a clear division, teams hit recurring issues when combining these tools.

  • State file conflicts: Terraform stores its state in a local file or remote backend (like S3). If two people run apply simultaneously, you get corruption. Always use a remote backend with locking (e.g., DynamoDB).
  • Version drift: The Kubernetes API evolves, and Terraform’s EKS module lags behind. Pin versions of both the module and the Kubernetes provider to what you’ve tested.
  • IAM and RBAC confusion: Terraform creates IAM roles for the cluster, but Kubernetes has its own Role-Based Access Control (RBAC). Forgetting to map IAM users to Kubernetes groups via aws-auth leads to “access denied” errors.
  • Node group scaling: Terraform manages the node group’s desired size. If you manually scale via the AWS console, Terraform will revert it on the next apply. Use Terraform for node scaling, or use Kubernetes Cluster Autoscaler (which adjusts the node group via AWS APIs).
  • Deleting resources: If you run terraform destroy, it will delete the EKS cluster, but any LoadBalancer services you created in Kubernetes might leave orphaned AWS resources. Always clean up Kubernetes objects first.

How to Choose What to Manage with Each

A practical rule of thumb: if it’s a cloud resource, Terraform; if it’s a container workload, Kubernetes. But there’s a gray area. For example, an RDS database is a cloud resource—use Terraform. A Redis cache? Terraform. An ingress controller (like NGINX) is a Kubernetes object—use Helm/kubectl. A Kubernetes Namespace? That’s a Kubernetes object, but you might want Terraform to create it if it’s part of the initial cluster setup.

Some teams adopt GitOps (with tools like ArgoCD) where Kubernetes manifests are stored in Git and automatically applied. In that model, Terraform still creates the cluster, but all application deployments go through GitOps. This reduces the chance of drift between what’s running and what’s in code.

Costs and Efficiency Considerations

Running an EKS cluster has a base cost (the control plane is billed per hour), plus the cost of the worker nodes (EC2 instances), storage, and data transfer. Terraform can help control costs by tagging resources, but the real savings come from Kubernetes’ ability to pack containers densely onto fewer nodes. You can also use spot instances for non-critical workloads, which Terraform can configure in the node group. However, be careful: spot instances can be reclaimed, so they’re not suitable for stateful applications unless you have proper replication.

To avoid surprise bills, use Terraform to enforce resource limits (e.g., set max_size on node groups) and use Kubernetes ResourceQuotas to cap what each namespace can consume. Also, remember to destroy unused environments with terraform destroy—leaving a test cluster running overnight is a common cost leak.

Alternatives and Comparisons

Terraform isn’t the only IaC tool. AWS CloudFormation is Amazon’s native alternative, and Pulumi is a popular competitor that lets you use general-purpose programming languages. Kubernetes itself has alternatives like Docker Swarm or HashiCorp Nomad, but Kubernetes is the de facto standard for container orchestration. On AWS, you could also use Elastic Beanstalk (which abstracts away both infrastructure and orchestration) or AWS Fargate (which runs containers without managing nodes). The choice depends on how much control you need and how much you want to manage yourself.

Many teams use Terraform with the EKS Blueprints framework, which codifies best practices for cluster setup. Others prefer to use eksctl, a CLI tool that simplifies EKS creation, but that’s less declarative than Terraform. The key is consistency: pick one IaC tool and stick with it for all infrastructure.

Frequently Asked Questions

Can Terraform deploy Kubernetes resources directly?

Yes, Terraform has a Kubernetes provider that can create deployments, services, and namespaces. However, it’s generally not recommended for application workloads because it mixes two different lifecycles. Use Terraform for the cluster and kubectl/Helm for the apps.

Do I need Terraform if I use EKS?

No, you can create EKS via the AWS console, CLI, or eksctl. But Terraform gives you reproducible, reviewable infrastructure code. If you have more than a few resources, Terraform saves time and reduces errors.

How do I give my Kubernetes pods access to AWS services?

Use IAM Roles for Service Accounts (IRSA). Terraform creates the IAM role and the Kubernetes service account, and then you annotate the service account in your deployment. This is the secure way to let pods call S3, DynamoDB, etc., without hardcoding credentials.

What is the difference between Terraform state and Kubernetes state?

Terraform state tracks the cloud resources it manages (like cluster IDs and VPC IDs). Kubernetes state is stored in etcd and tracks your deployments, services, and pods. They are separate and should not be confused.

Can I use Terraform to manage an existing EKS cluster?

Yes, you can import an existing cluster into Terraform using terraform import. But it’s easier to start with Terraform from the beginning, because importing requires you to write configuration that matches the existing resources exactly.

Is it better to use EKS or a self-managed Kubernetes cluster on EC2?

EKS is managed by AWS: it handles the control plane, updates, and high availability. A self-managed cluster gives you more control but requires you to manage etcd, the API server, and upgrades yourself. For most teams, EKS is the better choice because it reduces operational burden.

Related

Formula used
in t | Finish time | min | 50 | 0
in d | Distance | km | 10 | 0 | 100000
out pace | Average pace | min/km | t / d | 2
out speed | Average speed | km/h | d / (t / 60) | 2

Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →