Skip to content

project rulesDevOps and cloudterraform

Terraform Rules

Project rules for Terraform: module layout, naming, remote state with locking, typed variables, tagging and a reviewed plan before every apply.

npx workspacetemplates add terraform-rules --tool claude-code

Writes 1 file to your project root

CLAUDE.mdadd to your file19 lines · 1.4 KBFile
# Project rules: Terraform

## Layout
- Keep a root configuration per environment (`envs/dev`, `envs/prod`) that calls reusable modules in `modules/`. Each module has `main.tf`, `variables.tf`, `outputs.tf` and `versions.tf`, with a short README.
- Pin the Terraform version and every provider with `required_version` and `required_providers`, and commit `.terraform.lock.hcl`.

## State
- Use a remote backend with locking and encryption, and separate state per environment. Never commit state files or secrets. Do not edit state by hand; use `moved` blocks, `import` blocks or `terraform state` commands with review.

## Style
- Use snake_case resource names, with `this` for a module's single main resource. Give every variable a `type` and `description`, add `validation` blocks where limits exist, and mark sensitive values.
- Prefer `for_each` over `count` so removing an item does not shift others. Avoid over-nested modules and huge `locals`.
- Apply consistent tags through provider `default_tags`, including owner and environment.

## Safety
- Set `prevent_destroy` on stateful resources and use `lifecycle` rules sparingly. Pass secrets from a secret manager, not variables files.

## Before finishing
- Run `terraform fmt -check -recursive`, `terraform validate`, `tflint` and a scanner such as Checkov or Trivy. Run `terraform plan -out=tfplan` and summarize the adds, changes and destroys. Never run `apply` without approval.

What it does

Infrastructure code changes real systems, so sloppy conventions cost more than they do in an application. These rules give an agent a structure for modules, state and variables that keeps changes small and reviewable.

They also make plans the unit of review: nothing is applied until someone has read exactly what will change.

Example

you › Add an S3 bucket for logs with a 90 day lifecycle rule

agent › The agent adds a module call with typed variables, encryption and public access blocks enabled, default tags applied, then runs fmt, validate, tflint and a plan, and reports one resource to add.

Questions

Why should the agent not apply on its own?
An apply changes live infrastructure. A person should read the plan first, so the agent stops at the plan and reports it.
Does it work with OpenTofu?
Yes. The structure and review rules are the same; use the tofu command in place of terraform.
  • AWS CDK Rules

    Project rulesaws

    Free
  • Docker Rules

    Project rulesdocker

    Free
  • GitHub Actions Rules

    Project rulesgithub-actions

    Free
  • Kubernetes Rules

    Project ruleskubernetes

    Free