chore(terraform): refine state lock semantics (skip on plan, wait on apply)#997
Open
olepg wants to merge 2 commits into
Open
chore(terraform): refine state lock semantics (skip on plan, wait on apply)#997olepg wants to merge 2 commits into
olepg wants to merge 2 commits into
Conversation
sefornes
pushed a commit
that referenced
this pull request
Apr 29, 2026
Two small, independent hardening changes to the reusable `terraform.yml` workflow. ## 1. Stop leaking `ENCRYPTION_PASSWORD` to every step Removed `ENCRYPTION_PASSWORD` from the workflow-level `env:` block and scoped it to only the two steps that actually need it (`Create tarball` in the plan job, `Extract tarball` in the apply job). Secrets passed via `secrets:` to a reusable workflow are auto-masked by GitHub. Promoting them to a workflow-level `env:` widens exposure to every step (including any third-party actions added later) with no benefit. The secret's description was updated to note this. ## 2. Make plan-tarball retention configurable (default 5 days, was 35) Added a new `artifact_retention_days` input (number, default `5`) and use it for `retention-days` on the `Upload artifact` step. The artifact contains the full Terraform configuration and plan, encrypted with `ENCRYPTION_PASSWORD`. It is only consumed by the Apply job in the same workflow run. 35 days of retention is unnecessary exposure window if the password ever leaks. Making it an input lets consumers tune the value to their deployment cadence (e.g. workflows that span multiple environments over several days can opt for a higher value). This is an additive input change — existing consumers get the new 5-day default automatically without any code changes. --- The lock-semantics change originally proposed here has been split out into #997.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refine how the reusable
terraform.ymlworkflow interacts with the state lock.terraform plan:-lock=falseterraform planis read-only against state and does not need to acquire a lock. Taking one blocks concurrent plans from other PRs (or other working directories sharing the same backend) for no benefit. Skipping the lock lets plan runs proceed in parallel.terraform apply: newlock_timeout_minutesinput (default0)By default
terraform applyfails immediately if the state lock is held. With concurrent applies queued via the workflow'sconcurrencygroup this is rarely an issue, but transient contention from external tooling (manualterraform applyruns, drift-detection jobs, etc.) can sometimes cause spurious failures.Added a new
lock_timeout_minutesinput (number, default0) that is passed toterraform applyas-lock-timeout=<N>m. When set to0the flag is omitted entirely, preserving Terraform's native fail-fast behavior — so the default is a no-op for existing consumers. Consumers that hit lock contention can opt into waiting (e.g.lock_timeout_minutes: 20).This input change is additive; existing consumers do not need to update anything.
Split out from #993.
Caveat on
-lock=falseTo be upfront: HashiCorp explicitly warns against
-lock=false("dangerous if others might concurrently run commands against the same workspace"), so this is a real trade-off, not a free win. Worth being explicit about the actual race scenarios:terraform apply -lock=falsefrom a laptop,terraform state rm/mv/push -lock=false, custom scripts uploading state directly to the backend, or misconfigured wrappers that pass-lock=falseby default. Possible but rare; most tooling locks by default.-lock=true(default), our plan would block waiting for their lock, then read consistent post-apply state. With-lock=false, our plan reads the pre-apply snapshot while their apply is still in flight; they then finish and bump the state serial. Our plan now references a state version that no longer exists.In both cases
terraform apply <planfile>validates the saved plan's state lineage/serial against current state and normally refuses to apply a stale one, so the usual failure mode is a failed apply (re-run required), not a silent wrong apply. The check is lineage/serial only, not a full semantic diff, so narrow edge cases involvingdatasource reads can still slip through. Plans also go stale on their own within hours regardless of locking.