Skip to content
BETAGuardrails are in beta. The library, the configuration format and the CLI command can still change.

Guardrails / iac

Terraform modules are pinned

iac/terraform-modules-pinned@v1

Every module a Terraform root takes from a registry or a git URL names the version it takes, so the same commit resolves to the same module twice.

Idiac/terraform-modules-pinned
Versionv1
Categoryiac
Default severityerror
Interpreterpython3
Timeout30 seconds
Violations tolerated0
Collectsterraform

Collectors

This guardrail gathers nothing itself. It depends on the collectors below, which the CLI runs once per build before any check, and reads what they found out of GUARDRAIL_FACTS. A collector that collects nothing skips this guardrail rather than failing it.

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. Read out of the files themselves, so it works on a runner with no Terraform installed, and enriched with the versions an initialised root has already resolved when the terraform CLI is there.none

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

Configuration

json
{
  "guardrails": {
      "failOn": "error",
      "comment": true,
      "checks": [
          {
              "use": "iac/terraform-modules-pinned@v1",
              "severity": "error",
              "with": {
                  "allowMovableRefs": "false"
              },
              "exemptions": []
          }
      ]
  }
}

Inputs

InputDescriptionDefaultEnvironment variable
allowMovableRefsWhether a git source pinned to a tag or a branch rather than to a commit sha counts as pinned. false requires a 40 character sha, which is the only ref nobody can move.falseGUARDRAIL_INPUT_ALLOWMOVABLEREFS

How to fix

Pin the modules named in the violations. A registry source pins with version:

hcl
module "vpc" {
  source  = "terraform-aws-modules/vpc/aws"
  version = "5.8.1"
}

A git source pins with a ref in the URL, and a tag is not enough because a tag moves:

hcl
module "vpc" {
  source = "git::https://github.com/acme/terraform-vpc.git?ref=6f3a1c2d9b8e4f7a0c5d2e1b3a4f5c6d7e8f9a0b"
}

An unpinned module means the plan you reviewed and the plan that applied can be built from different code. A module in this repository, taken with a relative source, is already pinned by the commit and is not judged.

More in iac

All 11 iac guardrails

Buildnote Limited
Registered in England and Wales, Reg: 16140412