terraform-safe-workflow
Skill49 starsChanged 35 days agoMITView on GitHub
Use when changing Terraform or OpenTofu code, modules, providers, or state — check versions, backend, and lock, run fmt, init, and validate, save a plan and review its destroy and replace actions, apply only that saved plan, and confirm a fresh plan is empty; recover with moved and import blocks, not hand edits or a rewound state.
In this repository117 other files it gives its agents.
- AGENTS.mdAGENTS.md
- COPILOT-INSTRUCTIONS.mdCopilot instructions
- .github/copilot-instructions.mdCopilot instructions
- ai-visibilitySkill
- content-strategySkill
- seoSkill
- actions-debuggingSkill
- agentic-engineeringSkill
- autopilot-patternsSkill
- background-agentSkill
- context-primeSkill
- copilot-memorySkill
- cross-session-memorySkill
- ecosystem-intakeSkill
- fleet-parallelSkill
- github-code-searchSkill
- github-codespaces-efficiencySkill
- github-issue-triageSkill
- github-pr-workflowSkill
- ide-switchingSkill
- knowledge-curatorSkill
- mcp-builderSkill
- mcp-ecosystemSkill
- multi-model-strategySkill
- plan-mode-masterySkill
- scope-guardSkill
- session-managementSkill
- stack-detectorSkill
- sub-agent-sandboxingSkill
- task-intake-routerSkill
- team-plannerSkill
- token-cost-optimizerSkill
- api-and-interface-designSkill
- code-reviewSkill
- context-engineeringSkill
- cpp-debuggingSkill
- deprecation-and-migrationSkill
- design-to-codeSkill
- diagnoseSkill
- fix-build-errorsSkill
- fix-github-issueSkill
- go-runtime-debuggingSkill
- implementSkill
- improve-codebase-architectureSkill
- nestjs-prismaSkill
- nextjs-prismaSkill
- performance-optimizationSkill
- pr-multi-perspective-reviewSkill
- prototypeSkill
- react-vitestSkill
- receiving-code-reviewSkill
- refactor-cleanSkill
- reviewSkill
- skill-creatorSkill
- source-driven-developmentSkill
- spec-driven-developmentSkill
- systematic-debuggingSkill
- tdd-workflowSkill
- zoom-outSkill
- add-to-changelogSkill
- The first 60 shown.
The file
We split this skill into the rules it gives an agent
5 rules. About 0 are common, 2 are distinctive, and 3 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈2
Distinctive · estimate
This author's own choices. Worth a look.
3
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 36
Read access to the remote state for plan, plus read, write, and delete access to its lock (the DynamoDB table or the S3 .tflock object), …
- Very specific: line 65
Read the summary line and every resource marked to destroy (-) or replace (-/+, or +/- when create_before_destroy is set).
- Very specific: line 98
After apply, terraform plan -detailed-exitcode must exit 0 (no changes).
- Distinctive: line 136
If apply fails partway, do not run apply again blindly: keep the state it left, plan again, compare the new plan with the reviewed one, and …
- Distinctive: line 144
Keep one state per environment and per blast radius.
≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Terraform Safe Workflow
Infrastructure code fails differently from application code: a wrong change does not throw an
exception, it deletes a database. The plan is the only preview, and state is the only record of what
Terraform manages. This skill keeps every change reviewable through a saved plan and every recovery
inside Terraform's own tools. The commands are the same for OpenTofu (tofu instead of terraform).
When to Use
- Editing
.tffiles, module inputs, or provider and module versions - Renaming or moving resources between modules, or importing resources created outside Terraform
- A plan shows unexpected changes (drift), or state is locked, corrupted, or out of date
- Designing the CI job that plans and applies infrastructure changes
When NOT to Use
| Instead of terraform-safe-workflow | Use |
Reviewing .tf files only for security misconfiguration | security-audit |
| Watching the service after the infrastructure change is live | deployment-canary |
| Coordinating a live outage caused by an infrastructure change | incident-response |
| Checking a provider's current arguments and behavior | source-driven-development |
Prerequisites
- The Terraform or OpenTofu version the configuration's
required_versionallows - Read access to the remote state for plan, plus read, write, and delete access to its lock (the
DynamoDB table or the S3
.tflockobject), because on a backend with locking enabled plan takes the state lock by default; write access to the state itself only where apply runs (creating a new workspace's empty state is a write, so do that from the apply role) - Credentials scoped to the target environment, supplied by environment or workload identity, never
in
.tffiles or variables files under version control
Workflow
1. Check the Ground Before Changing Anything
terraform version
terraform init -input=false
terraform validate
terraform fmt -check -recursiveConfirm the backend points at the intended environment (terraform workspace show if workspaces are
used) and that .terraform.lock.hcl is committed. Change provider versions on purpose
(terraform init -upgrade) and in their own change, not as a side effect.
2. Save the Plan and Read It
terraform plan -input=false -out=tfplan
terraform show -no-color tfplan > plan.txtRead the summary line and every resource marked to destroy (-) or replace (-/+, or +/- when
create_before_destroy is set). For each one, say why it is expected. A replace is a delete and a
create of the real object — check what data that resource holds. Only values marked sensitive print
as (sensitive value); other secrets and backend details can appear in plan.txt, and the saved
plan file contains values in plain text. Keep both out of version control and out of public
comments: store them as access-controlled CI artifacts and post only a checked, non-sensitive
summary to the pull request.
3. Move Code Without Destroying Resources
- Renaming a resource or moving it into a module: add a
movedblock from the old address to the new one, and plan again until the plan shows the move with no destroy - Adopting an existing resource: add an
importblock, check that the plan shows the import and no unexpected update or destroy for it, apply, then confirm the next plan is empty - Protecting stateful resources: set
lifecycle { prevent_destroy = true }on databases, buckets, and keys that must never be replaced by accident
4. Apply Only the Reviewed Plan
terraform apply -input=false tfplanApplying a saved plan runs exactly what was reviewed. Never run a bare terraform apply in CI and
never pass -auto-approve to a fresh plan; in CI, plan in one job, keep the plan job's whole working
directory (the plan file, .terraform, and .terraform.lock.hcl) as a restricted artifact for the
reviewers, and apply that same plan file in a later job that requires their approval. HashiCorp's
automation guide has the apply job restore that directory at the same absolute path, on the same OS
and CPU architecture; the apply job needs exactly the same Terraform version, the identical provider
binaries available at apply time, and credentials that target the same account.
5. Confirm and Recover
After apply, terraform plan -detailed-exitcode must exit 0 (no changes). Exit 2 means the plan
succeeded and still has changes — drift, a partial apply, someone else's change, or a provider that
reports late; read the whole plan before deciding which.
State records which real object each address manages; it is not a backup of the infrastructure.
Inspect it with terraform state list and terraform state show, and fix addresses with moved
or import blocks. Do not roll state back to an older version because a resource is missing —
that can orphan or duplicate real objects. Keep the current state (and any errored.tfstate),
compare it with what actually exists, then import, recreate, or run terraform plan -refresh-only.
Restore an earlier state version only to undo damage to the state itself — a corrupt or lost file,
or a mistaken state rm/state mv — after confirming which version was correct, under the
backend's lock. Run terraform force-unlock LOCK_ID only for a lock your own failed run left behind, after
confirming its ID and that nothing else is writing.
Common Rationalizations
| Rationalization | Reality |
| "The plan was fine yesterday" | State and remote resources change; apply the plan you just reviewed |
| "It's only a rename" | Without a moved block, a rename is a destroy and a create |
| "I'll fix the state file by hand" | Hand edits bypass locking and versioning; use state subcommands or blocks |
| "Auto-approve is fine in CI" | Then nobody reviewed the destroy lines; gate apply on the saved plan |
Red Flags
- A plan that destroys or replaces a database, bucket, key, or network with no explanation
-auto-approve,-target, or-lock=falsein routine scripts- Provider versions unpinned or
.terraform.lock.hclmissing from version control - Secrets in
*.tfvarsfiles committed to the repository
Verification
terraform fmt -check -recursiveandterraform validatepass- The saved plan was reviewed, and every destroy or replace has a written reason
- Apply used that saved plan file, not a new plan
terraform plan -detailed-exitcodeexits0after apply- Renames and imports appear as moves or imports in the plan, not as destroy and create
If apply fails partway, do not run apply again blindly: keep the state it left, plan again, compare the new plan with the reviewed one, and recover missing or extra objects with import or a reviewed follow-up plan rather than by rewinding state.
Tips
tflintandcheckovortrivy configcatch provider-specific mistakes and misconfiguration before plan- Keep one state per environment and per blast radius; a huge state makes every plan slow and risky
- Pin modules from registries to exact versions and review their changelog like any dependency
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

