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

Guardrails / iac

Terraform state is kept in a remote backend

iac/terraform-remote-state@v1

Every live Terraform root stores its state in a remote backend, and no state file is left in the checkout.

Idiac/terraform-remote-state
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-remote-state@v1",
              "severity": "error",
              "with": {
                  "checkStateFiles": "true",
                  "modulePaths": "modules/*,*/modules/*"
              },
              "exemptions": []
          }
      ]
  }
}

Inputs

InputDescriptionDefaultEnvironment variable
checkStateFilesWhether a .tfstate or .tfstate.backup file in the working tree is a violation. The collector reads the working tree rather than the index, so a developer who has run terraform apply locally has one that nobody committed. In CI the checkout is the repository and the two are the same thing.trueGUARDRAIL_INPUT_CHECKSTATEFILES
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

Store state in a backend the team shares, locked and encrypted:

hcl
terraform {
  backend "s3" {
    bucket       = "acme-terraform-state"
    key          = "live/network.tfstate"
    region       = "eu-west-1"
    encrypt      = true
    use_lockfile = true
  }
}

Then keep state out of the tree:

bash
printf '*.tfstate\n*.tfstate.backup\n' >> .gitignore
git rm --cached terraform.tfstate

Local state is one laptop away from being lost, and two applies at once overwrite each other with nothing to stop them. A state file in the repository is worse than lost: it records every attribute of every resource, the ones marked sensitive included, to everyone who can read the repository.

More in iac

All 11 iac guardrails

Buildnote Limited
Registered in England and Wales, Reg: 16140412