← Building the platform

Chapter 4 · Bootstrap

Bootstrap: The One Time Terraform Ran as Me

Terraform Entra ID Azure DevOps Terraform Cloud

What it is

Every change in the platform goes through a pipeline, and every pipeline runs as its own service principal. Those service principals have to come from somewhere. Before the first pipeline could deploy anything to Azure, someone had to create the identities it would run as, and that someone was me, from my laptop.

The bootstrap is the short list of applies that ran under my own az login as Owner. After it, nothing runs as me again.

Why it can't be skipped

It's a chicken-and-egg problem. A pipeline needs an identity with access to Azure, but the identity and its access are created by Terraform, and Terraform has to run as something. An identity also can't grant itself a permission it doesn't hold yet. So the first pass has to run as an account that already has the access: mine.

What happened, in order

  1. The identities. The first terraform plan and apply of the landing zone created the resource groups, the three pipeline service principals (one for the landing zone itself, one for shared-infra and one for ado_management), the custom role definitions and the role assignments, including the landing zone's own grant at the tenant root.
  2. The service connections. ado_management created an Azure DevOps service connection for each service principal, using workload identity federation (OIDC), so there is no client secret to store or rotate. Its own service principal didn't exist yet either, so these first applies also ran from my laptop, as me.
  3. The trust. Back in the landing zone, each service principal got a federated credential pointing at its service connection.

Steps 2 and 3 can't be merged into one apply. The federated credential needs the connection's subject, and Azure DevOps only generates that subject when the connection is created. It can't be worked out in advance, so the landing zone has to wait for ado_management to run first.

Moving state off my laptop

The state started on my laptop. That was fine for one person, but a pipeline can't read a file on my machine. Once the sharedccst storage account existed, I changed each repo's backend to azurerm and let Terraform migrate the state into it: ado_management, the landing zone and shared-infra, each with its own state file.

The one stored secret

The module registry was on Terraform Cloud from day one. Publishing a module needs a Terraform Cloud API token, so it's kept as a secret pipeline variable in Azure DevOps. It's one of the few stored secrets in the whole setup.

Current state

The bootstrap ran once. Since then every change, including changes to the landing zone itself, goes through a pipeline with its own identity and no stored Azure credentials. The tested steps are written up in the landing zone repo's README, so they can be run again if the platform ever has to be rebuilt.