Repository navigation
sec(ci): gate terraform force-unlock behind explicit input (closes #438) #818
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| # Runbook: Terraform Stuck State Lock | ||
|
|
||
| ## Symptoms | ||
|
|
||
| A Terraform workflow fails with an error similar to: | ||
|
|
||
| ```text | ||
| Error: Error acquiring the state lock | ||
| ... | ||
| Lock Info: | ||
| ID: <lock-id> | ||
| Operation: OperationTypeApply | ||
| ... | ||
| ``` | ||
|
|
||
| or the S3 backend reports the `.tflock` object already exists from a prior run. | ||
|
|
||
| ## When this happens | ||
|
|
||
| A lock file is left behind when a CI run is interrupted (runner killed, job | ||
| canceled mid-apply, network error during apply). The workflow does NOT | ||
| auto-delete the lock on failure: a blanket failure-path delete would itself | ||
| risk state corruption, because a run that fails *because* it could not acquire | ||
| the lock would delete the lock another run is still actively holding. Terraform | ||
| releases its own lock on a clean apply error, so a lock that survives a run | ||
| means that run died abnormally; clearing it requires operator confirmation that | ||
| the owning run is dead. | ||
|
|
||
| ## Safe recovery steps | ||
|
|
||
| Before clearing the lock, confirm the prior run that owns it is truly dead: | ||
|
|
||
| 1. Note the `Lock Info.ID` from the error output. | ||
| 2. In GitHub Actions, find the run that created the lock (check the timestamp in | ||
| `Lock Info.Created`). Verify its status is "Failed" or "Cancelled" -- not | ||
| still in progress. | ||
| 3. If the owning run is still running, do NOT clear the lock. Wait for it to | ||
| finish or cancel it explicitly first. | ||
|
|
||
| ## Clearing the lock via workflow dispatch | ||
|
|
||
| Once you have confirmed the owning run is dead: | ||
|
|
||
| 1. Go to **Actions > Deploy to AWS Fargate > Run workflow**. | ||
| 2. Select the affected environment. | ||
| 3. Check **"Force-clear a stale S3 state lock from a prior crashed run"**. | ||
| 4. Click **Run workflow**. | ||
|
|
||
| The `Clear stale state lock (operator-triggered only)` step will remove the | ||
| `.tflock` object from S3 and then proceed with a normal deploy. | ||
|
|
||
| ## Manual recovery (AWS CLI) | ||
|
|
||
| If you prefer to clear the lock outside CI: | ||
|
|
||
| ```bash | ||
| # Replace <bucket> and <env> with the actual backend bucket and environment name | ||
| BUCKET=<bucket> | ||
| ENV=<env> | ||
| LOCK_KEY="github-fargate-${ENV}/terraform.tfstate.tflock" | ||
|
|
||
| aws s3 ls "s3://${BUCKET}/${LOCK_KEY}" # confirm it exists | ||
| aws s3 rm "s3://${BUCKET}/${LOCK_KEY}" # remove it | ||
| ``` | ||
|
|
||
| Then re-run the deployment workflow normally (without `clear_stale_lock`). | ||
|
|
||
| ## Why locks are not cleared automatically | ||
|
|
||
| Any unconditional lock delete (before `terraform init`, or in a blanket | ||
| failure-path cleanup step) lets two concurrent deploys race: one run could | ||
| remove another run's active lock, enabling parallel state writes and potential | ||
| state corruption. A failure-path delete is especially dangerous because a run | ||
| that fails *because* it lost the race to acquire the lock would then delete the | ||
| winner's live lock. The lock must only be cleared when you know the holder is no | ||
| longer active, which is why clearing is gated behind the operator-triggered | ||
| `clear_stale_lock` input rather than an automatic step. | ||
|
|
||
| Terraform releases its own lock on a clean apply error within the same run, so | ||
| this gating only affects the rarer case where a run dies abnormally before that | ||
| release happens. |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.