From Terraform to a GitOps Control Plane: Designing the Bootstrap Handoff
The first problem you hit when building a Kubernetes platform is not GitOps itself. It is a circular dependency: there is no controller yet to deploy the GitOps controller. IaC creates the cluster, but installing the first Helm release into that cluster already requires cluster access credentials.
This is the first article of the Platform Delivery on Kubernetes series. It defines, at wiring level, how far Terraform should own and where desired state should be handed over to GitOps. It does not re-explain GitOps basics; it covers exactly one boundary.
The first controller creates a circular dependency
Section titled “The first controller creates a circular dependency”The steady state of GitOps is simple. Git holds desired state and an in-cluster controller reconciles it. The problem is the single moment before that controller exists.
- Without a cluster you cannot deploy a controller.
- Without a controller you cannot reconcile manifests.
- So exactly once, something outside the cluster must push into it.
If a human performs that “exactly once” by hand, it stops being reproducible. If instead IaC is extended to deploy every workload, the IaC pipeline becomes a permanently cluster-admin deployment channel. The goal is an explicit handoff point between those two extremes.
What Terraform owns and what it hands over
Section titled “What Terraform owns and what it hands over”Fix the boundary as a list, not a sentence. The table below splits ownership by lifecycle.
| Object | Owner | Reason |
|---|---|---|
| Network, subnet, private endpoint | Terraform | Rarely changes and outlives the cluster |
Cluster (aks-platform-dev-01), node pools | Terraform | Durable primitive creatable only through the cloud API |
Resource group (rg-platform-dev), vault (platform-secrets), registry (registry.example.com) | Terraform | Cloud resources outside the cluster that need identity bindings |
| Workload identity, federated credential, role assignment | Terraform | Authorization is a cloud IAM concern, not a Git manifest concern |
| The first GitOps controller (bootstrap CD) | Terraform | The single exception that breaks the circular dependency |
| Platform addons (ingress, certificates, observability) | GitOps | Short change cadence; rollback must be Git-based |
| Application Deployment/Service/Ingress | GitOps | Desired state owned by the application team |
The last two rows carry the argument. Adding one more helm_release to Terraform is always easy. But every release added that way keeps the IaC pipeline holding cluster-admin rights, and it puts application delivery behind the terraform apply queue. Keep the exception at one.
The generated kubeconfig is an ephemeral input
Section titled “The generated kubeconfig is an ephemeral input”The Helm provider needs cluster access. A common practice is for CI to extract a kubeconfig from a Terraform output into a file and pass it to the provider.
provider "helm" { kubernetes { # Ephemeral file produced by CI right before apply. Never committed. config_path = ".kubeconfig" }}That file is a cluster-admin-grade credential. Enforce the following.
- It exists only in the CI runner workspace and disappears with the job.
- It is listed in
.gitignore, and secret scanning watches the filename pattern. - It is never uploaded as an artifact or printed to logs.
- Prefer a short-lived token exec credential over a file where possible.
The first controller itself is an ordinary Helm release. It gets its own namespace, and its values are injected from a template.
resource "helm_release" "bootstrap_cd" { name = "bootstrap-cd" repository = "https://argoproj.github.io/argo-helm/" chart = "argo-cd" namespace = "bootstrap-cd" create_namespace = true
# Run only after the cluster is Ready, via an explicit dependency. depends_on = [azurerm_kubernetes_cluster.platform]
values = [templatefile("${path.module}/bootstrap-cd-values.yaml", { ingress_class_name = var.ingress_class_name server_host = "argocd.platform.example.com" })]}Pin the chart version. Version compatibility is not the subject here, so no number is given, but a bootstrap controller that drifts to a different version on every apply makes the handoff itself unstable.
The bootstrap handoff flow
Section titled “The bootstrap handoff flow”The full path starts at a pull request and ends at a workload. Dotted lines mean control and reconciliation; solid lines mean creation or credential flow.
From the moment bootstrap CD is running, Terraform no longer needs to know about workloads. How the platform and application layers are then split into two tiers is covered in Splitting Platform and Application Delivery with Two Argo CD Instances.
An apply that continues silently is unsafe
Section titled “An apply that continues silently is unsafe”Bootstrap scripts often contain this shape.
# Do not copy this shape as-isterraform output --raw aks_kube_config > .kubeconfig || trueterraform apply -auto-approve|| true is a pragmatic workaround for the very first apply, when the output does not exist yet. On a run where the cluster has never existed, there is no kubeconfig output, the step fails, and the whole pipeline stops. That is where the workaround comes from.
The problem is that this one line cannot distinguish a credential failure from a not-yet-created cluster. Expired permissions, a wrong backend, or a renamed output all pass just as silently. The Helm provider then holds an empty kubeconfig, attaches to the wrong context, or fails mid-apply with an unrelated error.
The safe form separates the two states explicitly.
set -euo pipefail
# Step 1: converge the cluster itself first (excluding controller deployment)terraform apply -auto-approve -target=module.cluster
# Step 2: credential retrieval must stop immediately on failureterraform output --raw cluster_kube_config > .kubeconfigtest -s .kubeconfig || { echo "kubeconfig is empty: aborting apply"; exit 1; }
# Step 3: now run the full apply including the first controllerterraform apply -auto-approveThe rule is single. If the required kubeconfig cannot be obtained, apply must fail loudly. Solve bootstrap ordering by splitting execution into stages, never by suppressing errors.
PR guardrails are options, not a mandated product
Section titled “PR guardrails are options, not a mandated product”Treating infrastructure change as a pull request lets you attach several controls. None of these mandate a specific tool; choose each one against the failure it prevents.
| Guardrail | Failure prevented | Adoption criterion |
|---|---|---|
| State lock / PR lock | State corruption from concurrent applies | Effectively mandatory beyond one engineer |
| Plan posted as a PR comment | Reviewers approving without seeing real changes | When diffs are frequently large |
| Cost diff | Unexpected spend increases | When the team owns the cost |
| Auto-merge disabled | Unreviewed changes reaching the cluster | Always for production clusters |
| Drift detection | Accumulated manual console changes | Where break-glass is genuinely used |
Plan posting and cost diff only make sense when the pipeline runs in a pull request context, and plan output can leak backend names, subscription identifiers, and endpoints verbatim. For public repositories or those with outside collaborators, verify plan-output masking first.
Operations and failures after the handoff
Section titled “Operations and failures after the handoff”| Situation | Handling |
|---|---|
| The first Helm release fails | Remove the failed release and rerun. A partially installed state keeps colliding with later applies |
| Re-running apply | Terraform must be idempotent. Unchanged values must not change the release |
| The bootstrap controller dies | Terraform must be able to recreate it. This is the only reason the controller stays in IaC |
| A request to add a platform addon | It goes into the GitOps repository, not Terraform |
| Emergency manual change (break-glass) | Allow it with a time limit, then always reconcile back through IaC or Git |
| Cluster deleted and recreated | In-cluster state is restored from Git; Terraform only restores primitives |
What Terraform must not manage after the handoff is equally clear. Resources inside application namespaces, ConfigMap values, ingress rules, and Deployment replica counts all belong to GitOps. Once Terraform touches them, two systems keep reverting each other on the same objects.
Summary
Section titled “Summary”Break the bootstrap circular dependency with one rule: IaC owns exactly one controller. Treat cluster credentials as an ephemeral input created just before apply and discarded with the job, never committed. Do not hide credential failure behind || true; split execution into stages and fail loudly. PR guardrails are options chosen against specific failures. And once the first controller is running, do not pull platform or application desired state back into Terraform.