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 →
- Split into two stacks:
clustercreates EKS,cluster-addonsconfigures the Kubernetes/Helm providers from a data source. - 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.
More on Workflow and Providers
A new teammate is about to run Terraform for the first time against a shared dev account. Which commands do you walk them through, and in what order?BeginnerYour manager asks why we bother with `terraform plan` when `apply` shows the same diff before asking for confirmation. What do you say?BeginnerTwo engineers get different provider versions and see different plans for the same code. How do you stop this?BeginnerSomeone committed the `.terraform/` directory to the repo. What should be in version control and what should not?BeginnerThe same configuration must create a VPC in `us-east-1` and a DR copy of a bucket in `eu-west-1`. How do you do it with one root module?IntermediateYou must deploy the same stack into 12 AWS accounts. How do you handle credentials and provider configuration?Intermediate