Interview Prep/Terraform/Workflow and Providers

The EKS cluster and the Kubernetes/Helm providers are in the same root module, and first apply fails with "connection refused" on localhost. How do you fix the design?

AdvancedScenarioWorkflow and Providers
Answer

Situation read → Provider configuration depends on resource attributes that don't exist at plan time, so the Kubernetes provider falls back to defaults.

What I'd do →

  1. Split into two stacks: cluster creates EKS, cluster-addons configures the Kubernetes/Helm providers from a data source.
  2. In the second stack, read the cluster with data "aws_eks_cluster" and use an exec-based token.
provider "kubernetes" {
  host                   = data.aws_eks_cluster.this.endpoint
  cluster_ca_certificate = base64decode(data.aws_eks_cluster.this.certificate_authority[0].data)
  exec {
    api_version = "client.authentication.k8s.io/v1beta1"
    command     = "aws"
    args        = ["eks", "get-token", "--cluster-name", var.cluster_name]
  }
}

Why → Provider blocks are evaluated before resources are applied; splitting by lifecycle removes the chicken-and-egg.

Interview tip → Mention that deleting the cluster and its in-cluster resources in the same state is the other reason to split.

All Terraform questions Roadmap: Terraform

More on Workflow and Providers