Skip to content
| Marketplace
Sign in
Visual Studio Code>Linters>StateLock: Terraform State Backend Locking and Collision CheckerNew to Visual Studio Code? Get it now.
StateLock: Terraform State Backend Locking and Collision Checker

StateLock: Terraform State Backend Locking and Collision Checker

jaytank_dev

| (0) | Free
Finds Terraform and OpenTofu state backends that silently risk clobbered, unlocked or exposed state: two root modules or environments writing the same S3/GCS/Azure state, S3 without use_lockfile, deprecated DynamoDB locking, hardcoded backend credentials (masked), CI applying with local state, commi
Installation
Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter.
Copied to clipboard
More Info

StateLock: Terraform State Backend Locking and Collision Checker

Terraform state is the one file that must never be overwritten, unlocked or public, and the backend configuration that protects it is the part of an IaC repository nobody reviews. A copied envs/ folder keeps the old key, so staging and prod write the same state. An S3 backend has no lock, so two pipelines apply at once. A CI job runs terraform apply in a folder with no backend and the state disappears with the runner. A -backend-config=prod.hcl points at a file that was renamed.

# envs/staging/main.tf, copied from envs/prod
terraform {
  backend "s3" {
    bucket = "acme-tfstate"
    key    = "app.tfstate"   # <- same key as envs/prod: whichever applies last wins
    region = "us-east-1"
  }
}

None of this fails terraform validate. StateLock reads every backend in the workspace, works out where each root module and environment really keeps its state, and shows the problems before the next apply.

What you see

  • Activity bar: a StateLock icon opens the State backends view. Findings are grouped by file, and every row names the exact backend: envs/prod and envs/staging both use s3://acme-tfstate/app.tfstate, platform: prod.s3.tfbackend and staging.s3.tfbackend both use s3://acme-platform-state/platform.tfstate, sandbox has no backend: terraform apply keeps state on the runner, secret_key = **** (40 chars) in backend "s3". A badge shows the number of findings.
  • Inline Quick Fix on a row: add use_lockfile = true, add encrypt = true, or add the standard Terraform entries to .gitignore.
  • Problems and Quick Fixes in the files themselves, with links to the other module that shares the same state.
  • Status bar: StateLock: N. Click it to open the view.

Credentials are never shown: only the length, and for AWS access key IDs the AKIA prefix.

Rules

ID Rule Default Example Fix
SL001 Two root modules write the same state (same S3 bucket + key, GCS bucket + prefix, Azure container + key, consul path, pg schema, http address or local path), including Terragrunt units that share a parent's literal key Error envs/prod and envs/staging both use s3://acme-tfstate/app.tfstate Give each root module its own key
SL002 State locking is off: S3 without use_lockfile = true or dynamodb_table, http without lock_address, consul with lock = false Warning s3://acme-tfstate/app.tfstate has no state locking Add use_lockfile = true
SL003 DynamoDB locking, deprecated in Terraform 1.11 (not reported for OpenTofu, which keeps it supported) Information dynamodb_table "tf-locks" (deprecated locking) Add use_lockfile = true next to it for the migration
SL004 S3 backend sets neither encrypt = true nor kms_key_id Information s3://acme-tfstate/app.tfstate does not request encryption Add encrypt = true
SL005 Credential hardcoded in a backend block, .tfbackend / backend .hcl file, Terragrunt remote_state or a CI -backend-config="secret_key=..." flag Error conn_str = postgres://****:****@db.internal:5432 in backend "pg" Move it to the environment and rotate it
SL006 A CI workflow runs terraform apply / tofu apply in a root module with no backend (or backend "local") Warning sandbox has no backend: terraform apply keeps state on the runner Configure a remote backend
SL007 *.tfstate or .terraform/ committed to git, or not covered by .gitignore Warning envs/prod/terraform.tfstate is committed to git Add *.tfstate, *.tfstate.*, **/.terraform/* to .gitignore
SL008 -backend-config=<file> in CI, a Makefile or a script points at a file that does not exist Error envs/prod/prod.hcl does not exist (-backend-config=prod.hcl)
SL009 Variables, terraform.workspace, path.module or function calls in a backend block or backend config file (init stops with "Variables not allowed") Error key uses terraform.workspace in backend "s3"
SL010 Environments' backend config files resolve to the same state, or one configuration's default state is another's <workspace_key_prefix>/<workspace>/<key> Error s3://a/env:/prod/app.tfstate is both b and workspace "prod" of a Give each environment its own key

About SL004: Amazon S3 has encrypted every new object with SSE-S3 by default since January 2023, so on AWS the state is not stored in plain text even without encrypt = true. The rule is Information because the setting still matters for S3-compatible stores and because kms_key_id adds key-level access control on top of bucket permissions. Raise it to a warning in stateLock.rules if your team requires it.

How StateLock finds where state lives

  • backend blocks in .tf, .tofu and .tf.json files; each folder with Terraform files is a module, and a module with a backend, a provider block or a CI run is a root module.
  • Partial configuration: .tfbackend files next to the module, and -backend-config files and key=value pairs from terraform init / tofu init commands in GitHub Actions, GitLab CI, Azure Pipelines, Bitbucket, CircleCI, Buildkite, Jenkins, Makefiles, justfiles and shell scripts. The working directory comes from -chdir, cd, pushd and working-directory (step or job default). A dynamic path such as backends/${{ inputs.env }}.s3.tfbackend is matched against the files that exist.
  • Terragrunt remote_state blocks are read for locking, encryption and credentials, and a literal key in a parent config that several units include is reported (best effort).
  • OpenTofu: a module with .tofu files, or that CI only runs with tofu, follows OpenTofu's rules (variables and locals are allowed in backend blocks since 1.8, DynamoDB locking is not deprecated, an encryption block counts as state encryption). Set stateLock.engine to force either tool.

Settings

Setting Default Description
stateLock.enabled true Turn the checks on or off.
stateLock.rules {} Severity per rule (error, warning, information, hint, off), e.g. { "SL004": "warning" }.
stateLock.engine auto auto, terraform or opentofu.
stateLock.checkGitTracked true Run git ls-files (trusted workspaces) to find committed state.
stateLock.maxFiles 5000 Maximum number of files of each kind read per scan (files over 1 MB are skipped).
stateLock.exclude node_modules, .git, .terraform, .terragrunt-cache, vendor Globs that are never scanned.

Commands

  • StateLock: Rescan Workspace
  • StateLock: Show State Backends
  • StateLock: Open Settings

Privacy and safety

No network, no telemetry. StateLock never reads state file contents and never talks to a backend: everything comes from the configuration files in the workspace. Files are read only inside the workspace (symlinks that lead outside are skipped) and only up to 1 MB. Git runs only in trusted workspaces, only for rev-parse and ls-files, through execFile with no shell, with repository hooks and fsmonitor disabled and your global git config ignored.

In an untrusted workspace StateLock runs no git commands, so committed state files are not reported, and the repository's own StateLock settings are ignored (only your user settings apply).

Limitations

  • Only what the workspace shows is judged. A partial backend whose bucket or key comes from TF_CLI_ARGS_init, a wrapper outside the repository or a fully dynamic path is not checked for locking or encryption, and a collision with a state outside the workspace (another repository, another workspace folder) is not seen.
  • Working directories are followed through -chdir, cd, pushd and working-directory; Jenkins dir() blocks, make -C and variables that hold a directory are not followed.
  • Terragrunt support covers remote_state blocks and includes of a parent config; generate blocks and dependency outputs are not evaluated.
  • The .gitignore check reads the repository's .gitignore files in a workspace folder that is the repository root; .git/info/exclude and global excludes are not counted, because they do not protect your teammates.
  • Secret-valued attributes inside state files are out of scope: StateLock checks how state is stored, not what it contains.

Learn more

  • Terraform S3 backend: locking, encryption, workspace_key_prefix
  • Terraform backend configuration and partial configuration
  • Amazon S3 default encryption FAQ

License

MIT - included with the extension.

  • Contact us
  • Jobs
  • Privacy
  • Manage cookies
  • Terms of use
  • Trademarks
  • Your Privacy Choices
  • Consumer Health Privacy
© 2026 Microsoft