Terraform as a change-management control
Infrastructure as code is usually justified by automation, but its greater value is control: every change is reviewable, reversible and recorded. Six guardrails provide this control, and Little's law determines how often to check for drift.
Infrastructure as code is usually justified by speed: describe the infrastructure once, create it many times. That is true, but in my experience its greater benefit is control. A Terraform change can be reviewed like code, tested before it is applied, traced afterwards and reverted. These are the properties an auditor looks for in change management.
These properties require deliberate design. Without guardrails, Terraform is only a faster way to make unreviewed changes. This article lists the six guardrails I consider essential, maps them to ISO/IEC 27001, and then addresses a question that appears operational but is mathematical: how often to check for drift.
The relevant guidance comes from several standards and papers.
ISO/IEC 27001:2022 includes, in Annex A, controls for configuration management (8.9), logging (8.15) and change management (8.32). NIST SP 800-128 (2011) gives guidance on security-focused configuration management, including monitoring for unauthorised changes.
Morris (2nd edition, 2020) describes infrastructure-as-code practices, including pipelines, testing and the problem of configuration drift.
Little (1961) proved the queueing law : the average number of items in a system equals their arrival rate times the average time each spends there. It holds under very general conditions, which makes it well suited to engineering estimates.
The Terraform documentation defines the behaviour of plan, saved plan files and -detailed-exitcode; Conftest, from the Open Policy Agent project, tests structured data such as a JSON plan against policies.
1. State that is shared, locked and protected
Terraform's state file records what exists. It must live in a remote backend with locking, so that two people cannot apply at the same time. GitLab, for example, can host Terraform state behind its HTTP backend.
The state should be treated as sensitive. It often contains secrets in plain text, such as generated passwords or connection strings. It should be encrypted at rest, with restricted read access.
2. Plan in review, apply only what was reviewed
The central rule is that the change applied must be the change reviewed.
- On every merge request, run
terraform planand post a readable summary as a comment, so reviewers see the intended change. - After merge, the main-branch pipeline runs
terraform plan -out=tfplan, a manual approval job shows that plan, and the apply job applies exactly that saved file.
Applying whatever a fresh plan produces at apply time breaks the link between review and execution. Saved plan files can contain sensitive values in plain text, so they require the same protection as the state.
3. Policies checked by machine
Some rules should never depend on a reviewer noticing. Export the plan as JSON and test it against policies:
terraform show -json tfplan > plan.json
conftest test plan.json --policy policy/
Typical policies: no publicly readable storage, mandatory tags for ownership and cost, only approved regions and instance types. A static configuration scanner such as Trivy or Checkov catches common misconfigurations before the plan stage.
4. A controlled supply chain
Terraform downloads providers and modules from the internet, which makes it part of the software supply chain.
- Commit
.terraform.lock.hclso that provider versions and checksums are fixed. - Pin module versions, and use only internal or verified module sources.
5. Short-lived credentials
The pipeline needs cloud permissions to apply changes. Long-lived access keys stored as CI variables should be avoided. Most clouds can trust the CI system's OpenID Connect tokens directly, so each job receives temporary credentials scoped to what it needs.
6. Drift detection
Manual changes will eventually occur. Schedule a pipeline that runs:
terraform plan -detailed-exitcode
The exit code is 0 when nothing would change, 2 when there are differences, and 1 on error. Exit code 2 indicates drift: an alert is raised, and the manual change is either imported into code or reverted.
Audit perspective
Mapped to ISO/IEC 27001:2022, these guardrails support several Annex A controls directly: configuration management (8.9), change management (8.32) and logging (8.15). The merge request is the change record, the plan is the impact assessment, the approval is the authorisation and the pipeline log is the evidence. None of this evidence has to be produced separately.
Frequency of drift detection
The schedule for drift detection is a small queueing problem. If manual changes arrive at random at a rate and the pipeline checks every hours, a change goes unnoticed for hours on average. By Little's law, the average number of undetected changes at any moment is
With two manual changes a week, a weekly check leaves about one undetected change in the estate at any given time; a daily check, about 0.14.
The same arithmetic applies to the smoke alarms in a home. If a battery fails at a random moment, the failure stays unnoticed for half the interval between tests on average: about two weeks with monthly tests and six months with yearly ones. Whether that matters depends on what a drifted resource can do, which is a question for the risk analysis described in risk matrices and loss exceedance curves, not for the pipeline.
The simulation below illustrates the queueing result: if manual changes arrive at random at rate λ and a pipeline checks every T hours, the average number of undetected changes at any moment is λT/2, and each change stays hidden for T/2 on average.
Suggested settings:
- Weekly checks. With two manual changes a week and a check every 7 days, the simulated year averages close to one undetected change, matching . The peaks show what an auditor might find on an unfavourable day.
- Daily checks. Set the interval to 24 hours. The average falls to about 0.14 and each change hides for about half a day.
- High change rate. Raise the rate to 10 changes a week with weekly checks. About five changes are hidden at any time. Halving the interval halves the number; the formula is linear in both parameters.
- Repeated runs. The simulated average varies around the line from run to run. Little's law depends only on the averages, not on the randomness.
Derivation of λT/2. If changes arrive as a Poisson process, independent of the check schedule, a change's arrival time is uniformly distributed within its check interval of length . The time until the next check is then uniform on , with mean . Little's law, , gives the average number hidden: . With per week and day week, .
Terraform becomes a change-management control when the change applied is the change reviewed, the rules that matter are checked by machine, the credentials are short-lived and drift is detected on a schedule. The audit evidence is then produced by the pipeline: the merge request is the change record, the plan the impact assessment, the approval the authorisation, the log the evidence.
- Apply saved plans, never fresh ones.
- Encode non-negotiable rules as policies instead of relying on reviewers to spot them.
- Choose the drift-check interval from and the impact of a drifted resource, rather than from convention.
References
- ISO/IEC 27001:2022, Information security, cybersecurity and privacy protection - Information security management systems - Requirements, Annex A.
- NIST SP 800-128, Guide for Security-Focused Configuration Management of Information Systems, 2011.
- K. Morris, Infrastructure as Code: Dynamic Systems for the Cloud Age, 2nd ed., O'Reilly, 2020.
- J. D. C. Little, "A proof for the queuing formula: L = λW", Operations Research, 9(3), 383-387, 1961.
- HashiCorp, Terraform documentation,
terraform plancommand: developer.hashicorp.com. - Open Policy Agent, Conftest: conftest.dev.