agentleFS
drvossdrvoss/everything-copilot-cli/skills/development/terraform-safe-workflow/SKILL.md

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.

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
  1. Very specific:

    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), …

    line 36
  2. Very specific:

    Read the summary line and every resource marked to destroy (-) or replace (-/+, or +/- when create_before_destroy is set).

    line 65
  3. Very specific:

    After apply, terraform plan -detailed-exitcode must exit 0 (no changes).

    line 98
  4. Distinctive:

    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 …

    line 136
  5. Distinctive:

    Keep one state per environment and per blast radius.

    line 144

≈ “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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

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 .tf files, 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-workflowUse
Reviewing .tf files only for security misconfigurationsecurity-audit
Watching the service after the infrastructure change is livedeployment-canary
Coordinating a live outage caused by an infrastructure changeincident-response
Checking a provider's current arguments and behaviorsource-driven-development

Prerequisites

  • The Terraform or OpenTofu version the configuration's required_version allows
  • 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), 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 .tf files 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 -recursive

Confirm 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.txt

Read 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 moved block 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 import block, 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 tfplan

Applying 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

RationalizationReality
"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=false in routine scripts
  • Provider versions unpinned or .terraform.lock.hcl missing from version control
  • Secrets in *.tfvars files committed to the repository

Verification

  • terraform fmt -check -recursive and terraform validate pass
  • 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-exitcode exits 0 after 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

  • tflint and checkov or trivy config catch 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

Download now (ZIP)

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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.