Interview Prep/Terraform/Dependencies, Cycles and Providers

Creating an EKS cluster and Kubernetes resources in one stack: `Error: Get "http://localhost/api/v1/namespaces/app": dial tcp [::1]:80: connect: connection refused`.

AdvancedTroubleshootingDependencies, Cycles and Providers
Answer

Likely causes → The kubernetes provider was configured from values unknown at plan time (cluster not created yet, or being replaced), so it fell back to defaults (localhost). Also happens when the cluster is replaced in the same plan.

Diagnose → Is the cluster being created/replaced in this plan? Does the provider use module.eks.cluster_endpoint?

Fix → Apply in two stages (cluster stack first, workloads stack second), and configure the provider with exec auth:

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", "prod"]
  }
}

Prevent → Keep cluster infrastructure and in-cluster resources in separate states.

All Terraform questions Roadmap: Terraform

More on Dependencies, Cycles and Providers