Kubernetes provider sequencing
Separate cluster infrastructure from workload objects
Terraform can create a Kubernetes cluster using a cloud provider and then use the Kubernetes provider to manage objects inside it. These are different APIs and different lifecycles. The first needs a cloud identity; the second needs a reachable Kubernetes API, trusted TLS and Kubernetes permissions. A cluster ID is not a Kubernetes login. For a fictional catalog app, separate root configurations and state ownership by lifecycle so the workload plan starts only after the cluster is reachable. Pass approved nonsecret connection metadata through a controlled interface; obtain short-lived credentials at run time, not by sharing an entire state file.
cluster root -> reachable API + identity -> bootstrap rootbootstrap root -> established APIs + namespaces -> workloads rootA reference between resources can establish a graph edge, but it does not guarantee that a provider can initialize or discover a schema before the first apply. Separate runs also need an orchestrated handoff: a directory name is not an execution order.
Configure the provider and distinguish permission from connectivity
A provider source and version determine the plugin, while provider settings select a cluster and authentication method. The following example illustrates an explicit path and context, with the variable declaration omitted. It must not be used with a real kubeconfig. A kubeconfig can invoke executable authentication plugins; review it before use. Verify server identity, trust chain, route, credentials and authorization independently. Do not turn off TLS verification to fix an error.
# Static example only: no kubeconfig is supplied.terraform { required_version = "= 1.14.8" required_providers { kubernetes = { source = "hashicorp/kubernetes" version = "= 3.5.0" } }}provider "kubernetes" { config_path = var.approved_lab_kubeconfig config_context = "bytemind-lab"}The Kubernetes and Helm providers need their own configuration. In approved automation, prefer scoped short-lived identity or supported federation; verify its audience and expiry. A namespace limits object scope but is not itself a security boundary. A namespaced Role and RoleBinding can constrain access, while cluster-scoped resources need separate authorization. Avoid using cluster-admin for routine workload changes.
Choose typed resources or a manifest
Typed resources expose specific Terraform arguments for a Kubernetes kind. A versioned resource name helps identify its API version. Here a namespace and a ConfigMap illustrate a reference that establishes creation order. The ConfigMap contains only nonsecret configuration.
resource "kubernetes_namespace_v1" "catalog" { metadata { name = "catalog-lab" }}resource "kubernetes_config_map_v1" "settings" { metadata { name = "catalog-settings" namespace = kubernetes_namespace_v1.catalog.metadata[0].name } data = { MODE = "demo" }}A manifest resource takes an object-shaped declaration and uses the Kubernetes API to discover and validate schema information. It can cover kinds for which there is no dedicated typed resource. The following is an alternative ownership model for the same object, not another resource to apply.
# Static alternative to the typed ConfigMap above; do not use both.resource "kubernetes_manifest" "settings" { manifest = { apiVersion = "v1" kind = "ConfigMap" metadata = { name = "catalog-settings" namespace = "catalog-lab" } data = { MODE = "demo" } }}Do not let both addresses own the same ConfigMap. API defaults, mutations by admission and controller-managed fields can affect the planned and observed object. Review the exact provider schema before choosing field ownership or computed-field handling; never blindly ignore all differences.
Install CRDs before planning their custom resources
A CustomResourceDefinition adds an API kind and its schema. A custom resource is an instance of that kind. An operator may reconcile custom resources but creating a CRD alone does not install its controller. For kubernetes_manifest, planning a custom resource requires the API schema to be discoverable already. This is why a depends_on edge to a CRD created in the same initial plan is not a general solution.
# Fictional API; requires a separately installed, established CRD.resource "kubernetes_manifest" "catalog" { manifest = { apiVersion = "learning.example.com/v1" kind = "Catalog" metadata = { name = "demo" namespace = "catalog-lab" } spec = { replicas = 2 } }}The example API and spec are fictional. Stage the CRD installation first, verify its Established condition and discovery, then create a fresh plan for custom resources. Check controller readiness and admission webhooks separately. Remove dependent resources while their API and controller remain available; review finalizers, shared dependencies and retention before deleting the CRD or cluster. Deleting a CRD can delete all instances of that kind, not just this application.
Diagnose by phase
If a plan fails before it can read a schema, inspect the approved connection metadata and whether the kind exists, rather than retrying apply. Distinguish an unreachable endpoint, authentication failure, forbidden request, missing API version, schema mismatch and controller not ready. A successful plan is not proof of rollout readiness. In the worksheet, arrange the stages and decide what evidence permits the next one. No Kubernetes commands or credentials are required.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Kubernetes provider sequencing — knowledge check
Review API discovery, chart releases and ownership.
Why separate cluster and workload roots?
What does a CRD add?
Why can depends_on fail to solve first-plan custom-resource discovery?
A reachable API returns Forbidden. What should be reviewed?
Two Terraform addresses describe the same ConfigMap. What is the safe design?