Case study · Azure · Terraform · GitHub Actions

Azure Terraform pipeline with no stored passwords

Azure infrastructure built through a GitHub Actions pipeline that never stores a password or key. Changes are previewed on every pull request, production waits for a person, and manual changes can be detected and reversed.

Why I built it

This is a hands-on lab for the delivery patterns used in regulated environments such as banks. All Azure resources were deleted after the lab, and the repository documents what I would add for production.

How a change moves

  1. A change to the Terraform files is made on a branch and opened as a pull request.
  2. The pipeline signs in to Azure with OIDC, then checks formatting, scans for security issues with Checkov, and runs terraform plan. The plan is posted on the pull request as a comment.
  3. After review, the pull request is merged into main.
  4. The apply job pauses until someone approves it in the protected prod environment.
  5. Once approved, Terraform applies the change and releases the state lock.

The controls

PracticeHow it is done
No stored secretsGitHub signs in to Azure with OIDC workload identity federation. Each run gets a short-lived token and nothing long-lived is saved in GitHub.
Least privilegeThe pipeline identity can only build inside one resource group, and can only read and write the Terraform state file, not change the storage account itself.
Review before changeFormat check, Checkov scan and terraform plan on every pull request, with the plan posted so reviewers see exactly what will change.
Human approval for productionMerging to main starts the apply job, which waits in a protected environment until an approver says yes.
Safe shared stateState in Azure Storage with no account keys, Entra ID sign-in only, versioning turned on and a lock so only one run changes it at a time.
One run at a timeA GitHub Actions concurrency group stops two runs changing the same environment together.
Drift detectionA manual plan run shows when someone changed Azure by hand, and the Activity Log shows who and when.

What gets built

Free Azure resources only: a virtual network (10.20.0.0/16) with two subnets, a network security group that blocks all inbound internet traffic and is attached to the app subnet, a user-assigned managed identity for a future application, and tags on every resource showing the project, the owner and that Terraform manages it.

Things I broke on purpose

Sign-in subject mismatch

The apply job runs in the prod environment, which changes the identity GitHub presents to Azure. Without a matching federated credential, Azure rejected the run with AADSTS700213: No matching federated identity record found. The fix was a federated credential for the environment's subject.

The subject format includes ID numbers

The error showed that the subject included the account and repository ID numbers as well as their names. Credentials written from the documentation, using names only, did not match. The lesson is to copy the subject from the error message rather than write it from memory.

Manual changes cause drift

I added an inbound SSH rule by hand. A plan run showed Terraform wanted to remove it, the Activity Log showed my account made the change, and re-running the apply put the security group back as defined in code. Every Terraform change in the log appeared under the pipeline's identity, so a change by any other identity stood out immediately.

What I would add for production

  • Private networking for the state storage and other services, with the pipeline on self-hosted runners inside the network.
  • Azure Policy to deny public endpoints and inbound internet rules, so mistakes are blocked rather than only detected.
  • Scheduled nightly drift checks that alert when the plan finds differences.
  • Applying the exact plan reviewed on the pull request, rather than planning again at apply time.
  • Reusable, versioned Terraform modules for approved patterns.
  • Activity Log streamed to Log Analytics, with an alert on any change by an identity other than the pipeline.