Skip to content

Guardrails / terraform

Terraform providers are pinned ​

terraform/providers-pinned@v1

Every provider a live Terraform root requires is pinned to one exact version, and the lock file that resolves them is committed.

Idterraform/providers-pinned
Versionv1
Categoryterraform
Default severityerror
Interpreterpython3
Timeout30 seconds
Violations tolerated0
Collectsterraform

Collectors ​

This guardrail doesn't gather anything itself. It relies on the collectors below, which the CLI runs once per build before any check, and reads what they found from GUARDRAIL_FACTS. If a collector collects nothing, this guardrail is skipped, not failed.

CollectorGathersInputs it is given
terraformThe Terraform configuration in the checkout: every root directory holding .tf files, the backend it stores state in, the providers and modules it takes on and how tightly they are pinned, the resources it declares, the variables it takes, the lock file that resolves it, and any state file left in the tree. Everything is read from the files themselves, so it works on a runner with no Terraform installed. When the terraform CLI is there, the versions an initialised root has already resolved are added.none

The inputs above are this guardrail's own inputs, passed straight through to the collector. Setting one in buildnote.json changes what is collected, and two guardrails configured the same way share a single collection.

Configuration ​

json
{
  "guardrails": {
      "failOn": "error",
      "checks": [
          {
              "use": "terraform/providers-pinned@v1",
              "severity": "error",
              "with": {
                  "requireLockfile": "true",
                  "modulePaths": "modules/*,*/modules/*"
              },
              "exemptions": []
          }
      ]
  }
}

Inputs ​

InputDescriptionDefaultEnvironment variable
requireLockfileWhether a root that requires providers must commit a .terraform.lock.hcl. A root that requires no providers is never asked for one.trueGUARDRAIL_INPUT_REQUIRELOCKFILE
modulePathsComma separated globs naming directories that hold reusable modules rather than live roots. A module declares no backend and pins nothing, so it is not judged here. A directory another root takes on with a local source is recognised as a module whether or not it matches.modules/*,*/modules/*GUARDRAIL_INPUT_MODULEPATHS

How to fix ​

Pin each provider to an exact version, and commit the lock file terraform init writes:

hcl
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "6.4.0"
    }
  }
}
bash
terraform init
git add .terraform.lock.hcl

With ~> 6.0, a runner can resolve a version nobody reviewed, so the apply can run against a provider the approved plan never saw. The lock file records checksums as well as versions, so a swapped provider shows up as a mismatch instead of looking like an ordinary upgrade.

Reusable modules are the exception and declare a range instead, because the root that calls them does the pinning. List your module directories in modulePaths and they are skipped.

More in terraform ​

  • terraform/modules-pinned. Every module a Terraform root pulls from a registry or a git URL names a version, so the same commit always resolves to the same module code.
  • terraform/no-plaintext-secrets. Every Terraform variable named like a credential declares sensitive = true.
  • terraform/remote-state. Every live Terraform root stores its state in a remote backend, and no state file is left in the checkout.
  • terraform/state-encrypted. Every live Terraform root declares an encrypted backend, so its state isn't stored in the clear in its bucket.
  • terraform/state-locking. Every live Terraform root declares state locking, so two applies cannot write the same state at once.

All 6 terraform guardrails

Buildnote Limited
Registered in England and Wales, Reg: 16140412