Skip to content

From Terraform to a GitOps Control Plane: Designing the Bootstrap Handoff

Interactive Diagram

새 탭 ↗

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.

ObjectOwnerReason
Network, subnet, private endpointTerraformRarely changes and outlives the cluster
Cluster (aks-platform-dev-01), node poolsTerraformDurable primitive creatable only through the cloud API
Resource group (rg-platform-dev), vault (platform-secrets), registry (registry.example.com)TerraformCloud resources outside the cluster that need identity bindings
Workload identity, federated credential, role assignmentTerraformAuthorization is a cloud IAM concern, not a Git manifest concern
The first GitOps controller (bootstrap CD)TerraformThe single exception that breaks the circular dependency
Platform addons (ingress, certificates, observability)GitOpsShort change cadence; rollback must be Git-based
Application Deployment/Service/IngressGitOpsDesired 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.

stacks/dev/providers.tf
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.

modules/kubernetes/helm-init.tf
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 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.

PlantUML Diagram

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.

Terminal window
# Do not copy this shape as-is
terraform output --raw aks_kube_config > .kubeconfig || true
terraform 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.

Terminal window
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 failure
terraform output --raw cluster_kube_config > .kubeconfig
test -s .kubeconfig || { echo "kubeconfig is empty: aborting apply"; exit 1; }
# Step 3: now run the full apply including the first controller
terraform apply -auto-approve

The 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.

GuardrailFailure preventedAdoption criterion
State lock / PR lockState corruption from concurrent appliesEffectively mandatory beyond one engineer
Plan posted as a PR commentReviewers approving without seeing real changesWhen diffs are frequently large
Cost diffUnexpected spend increasesWhen the team owns the cost
Auto-merge disabledUnreviewed changes reaching the clusterAlways for production clusters
Drift detectionAccumulated manual console changesWhere 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.

SituationHandling
The first Helm release failsRemove the failed release and rerun. A partially installed state keeps colliding with later applies
Re-running applyTerraform must be idempotent. Unchanged values must not change the release
The bootstrap controller diesTerraform must be able to recreate it. This is the only reason the controller stays in IaC
A request to add a platform addonIt 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 recreatedIn-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.

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.