terraform-conventions
Skill7 starsChanged 4 days agoMITView on GitHub
Terraform / OpenTofu: layout, naming, variables, providers, state, tests. TRIGGER when: editing a `.tf`, `.tfvars`, `.tftest.hcl` or `.terraform.lock.hcl` file; adding a resource, variable, output, module or provider; pre-commit or CI for Terraform; user asks about Terraform/OpenTofu state, providers, tflint, terraform-docs or trivy findings on HCL. SKIP when: no HCL is involved.
1 other project uses a version of this.
Adopted by
Most recently taken up by
- leroyguillaume/skillsword for word, first seen Nov 22, 2026
Other versions1 project uses a version of this.
Word for word · 1
- leroyguillaume/skills★ 7100% same
Edited · 0
None found.
Wrote their own version · 0
None found.
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository54 other files it gives its agents.
- adr-conventionsSkill
- api-conventionsSkill
- architecture-conventionsSkill
- argocd-conventionsSkill
- ci-conventionsSkill
- config-conventionsSkill
- contributing-conventionsSkill
- councilSkill
- cryptography-conventionsSkill
- dependency-update-conventionsSkill
- diagram-conventionsSkill
- docker-conventionsSkill
- documentation-conventionsSkill
- frontend-conventionsSkill
- github-actions-conventionsSkill
- github-issue-conventionsSkill
- github-pr-conventionsSkill
- github-repo-settingsSkill
- gitlab-ci-conventionsSkill
- helm-conventionsSkill
- herdr-core-delegationSkill
- herdr-delegationSkill
- herdr-tui-delegationSkill
- kopf-conventionsSkill
- kubernetes-operator-conventionsSkill
- logging-conventionsSkill
- logo-conventionsSkill
- mise-conventionsSkill
- ollama-conventionsSkill
- openssf-conventionsSkill
- pr-conventionsSkill
- pre-commit-conventionsSkill
- project-metadata-conventionsSkill
- python-async-conventionsSkill
- python-conventionsSkill
- readme-conventionsSkill
- release-script-conventionsSkill
- release-supply-chain-conventionsSkill
- renovate-ci-conventionsSkill
- rest-conventionsSkill
- rust-build-conventionsSkill
- rust-conventionsSkill
- rust-http-conventionsSkill
- security-policy-conventionsSkill
- signal-handling-conventionsSkill
- sql-conventionsSkill
- starlight-conventionsSkill
- systemd-conventionsSkill
- system-user-conventionsSkill
- task-conventionsSkill
- topic-splittingSkill
- ui-copy-conventionsSkill
- web-layout-conventionsSkill
- yaml-conventionsSkill
The file
Show its rules and text
We split this skill into the rules it gives an agent
28 rules. About 0 are common, 2 are distinctive, and 26 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.
26
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 37
Every
datablock lives indata.tf. No exceptions: Not next to the resource that consumes it, not "just this one because it's only used … - Very specific: line 59
When data.tf grows, keep each cluster of lookups together, a blank line between clusters.
- Very specific: line 133
When the same component shape repeats a third time, extract a module in modules/<name>/.
- Very specific: line 190
Every variable declares a
typeand adescription: No exceptions, no bare variable "foo" {}. - Very specific: line 195
Use precise types: map(object({...})) with optional(x, default) beats map(any).
- Very specific: line 244
Never hardcode a value an operator could want to change — variable, with its value in terraform.tfvars (see Variables).
- Very specific: line 246
Use locals for anything constructed more than once (a resource-name format string, a principal, a label set).
- Very specific: line 262
Prefer write-only arguments (*_wo with *_wo_version) and ephemeral blocks for generated credentials — the value exists only inside the …
- Very specific: line 268
Never output a secret's value, never write one into terraform.tfvars, never paste one into a plan output pasted into a PR.
- Distinctive: line 336
Read the plan before proposing it: a replacement you did not expect is the bug, and the plan is where it is cheapest to find.
- Very specific: line 341
Never put the backend / cloud block anywhere but backend.tf, or a provider block anywhere but terraform.tf.
- Very specific: line 343
Never name a file after a technology (postgres.tf, cloudsql.tf) when its resources exist for a component.
- Very specific: line 345
Never put a data block anywhere but data.tf, and never split data.tf into per-component data files.
- Very specific: line 347
Never spread one component's resources across files by resource type (iam.tf, secrets.tf, databases.tf) — they all go in <component>.tf.
- Very specific: line 349
Never label a component's resource anything but <component> (one self-explanatory resource of its type) or <component>_<role> (an ambiguous …
- Very specific: line 352
Never label a module block anything but <module> (one unambiguous call) or <module>_<role> (a generic module, or several calls of one).
- Very specific: line 354
Never use main.tf in a stack that holds anything besides a single module call.
- Very specific: line 356
Never put a variable, output or locals block in a component file.
- Very specific: line 357
Never leave a root stack's variable value in a default rather than terraform.tfvars, and never write a literal where a variable fits.
- Very specific: line 359
Never ship a variable or output without a description, or a variable without a type.
- Very specific: line 361
Never use any where a concrete type can be written.
- Distinctive: line 362
Never leave a provider version unpinned or use the short registry form.
- Very specific: line 363
Never declare a provider block inside a module.
- Very specific: line 364
Never use count to iterate over a collection — for_each.
- Very specific: line 365
Never use a provisioner.
- Very specific: line 366
Never let a secret reach the state file, an output, or terraform.tfvars.
- Very specific: line 367
Never hand-edit state or run state mv where a moved block would do.
- Very specific: line 368
Never silence a tflint or trivy finding on your own initiative.
≈ “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 / OpenTofu conventions
Applies to root stacks and to modules. "Terraform" below means whichever of
terraform / tofu the repo actually runs — pick one and pin every tool to
it (see the pre-commit section), because a hook that shells out to whatever it
finds behaves one way on a laptop and another way in CI.
File layout
One concern per file, named after the concern. A root stack is a flat
directory of .tf files; the filename is the only navigation aid a reader
gets, so it has to mean something. Reserved names, always at the root:
| File | Holds |
terraform.tf | the terraform {} block — required_version, required_providers — and every provider block |
backend.tf | the backend / cloud block, always here and never in terraform.tf — in its own terraform {} block |
variables.tf | every variable block |
outputs.tf | every output block |
locals.tf | every locals block |
data.tf | every data block |
<component>.tf | every resource that exists for one component, whatever its type: toto.tf, billing.tf; for infrastructure no app owns, the technical building block: network.tf, gke.tf |
terraform.tfvars | the value of every variable the stack declares |
main.tf | only in a stack whose sole content is one module call — never alongside component files |
- Every
datablock lives indata.tf. No exceptions. Not next to the resource that consumes it, not "just this one because it's only used here". Data sources are the stack's inputs from the outside world — the things it reads but does not own — and that list is the first thing anyone auditing a stack wants: what does this depend on that it did not create? Scattered across eight component files, that question takes agrep; indata.tfit takes acat. It also makes the blast radius of an external change (a renamed zone, a subnet moved to another project) legible in one place, and it stops the same lookup being declared twice under two names in two files because neither author saw the other's.
When# data.tf data "google_compute_network" "cluster" { name = var.network_name project = var.host_project } data "google_dns_managed_zone" "platform" { name = var.dns_zone project = var.host_project }data.tfgrows, keep each cluster of lookups together, a blank line between clusters; do not split it intodata-network.tf/data-dns.tf. One file, one answer. One
localsblock, inlocals.tf, and never a second one appended below the first. HCL merges them, so two blocks parse and plan exactly like one — which is the problem: nothing fails, and the file now has two places a local could live. A reader looking forcluster_namehas to scan past the closing brace of a block that looked complete, and the next person to add a value picks one of the two by coin flip. The split also carries no meaning, because HCL gives it none: the blocks are not scopes, not ordering, not visibility. Merge them instead, with a blank line between groups.This is almost always an editing artefact rather than a decision — appending to the file with
cat >>or an editor's "add to end" is one keystroke and opening the existing block is several. Add the value inside the block that is already there. The same applies to a secondterraform {}block, a secondvariables-style grouping, or any other container the layout above says there is one of — the one deliberate exception being theterraform {}block inbackend.tfthat carries only the backend.- The same "all of a kind in one file" rule is what
variables.tf,outputs.tfandlocals.tfalready encode —data.tfcompletes the set. Resources are the exception: they group by component. - Every resource created for a component lives in
<component>.tf, named after the component — never in a file named after a resource type. The apptotoneeds a secret, a database instance, a database, a service account and the IAM bindings that tie them together: all of it goes intoto.tf. Notsecrets.tf+cloudsql.tf+iam.tf, where adding, reviewing or deleting one app means touching three files and removing it cleanly means hoping you found every piece.# toto.tf resource "google_service_account" "toto" { account_id = "toto" display_name = "toto" } resource "google_secret_manager_secret" "toto_api_key" { secret_id = "toto-api-key" replication { auto {} } } resource "google_secret_manager_secret_iam_member" "toto_api_key" { secret_id = google_secret_manager_secret.toto_api_key.id role = "roles/secretmanager.secretAccessor" member = google_service_account.toto.member } resource "google_sql_database_instance" "toto" { name = "toto" database_version = var.toto_database_version region = var.region settings { tier = var.toto_database_tier } } resource "google_sql_database" "toto" { name = "toto" instance = google_sql_database_instance.toto.name }- The file is named after the component a resource is created for,
never after what the resource is. The database instance created for
totolives intoto.tf, not inpostgres.tforcloudsql.tf— a technology is not a component. When another component uses somethingtotocreated (a database on its instance, an IAM binding on its bucket), that addition lives in the consumer's file. - Only infrastructure created for no app in particular is named after
its technical building block: the VPC, its subnets and its routers in
network.tf, the cluster ingke.tf. The moment a resource exists for a component, the component wins. - When the same component shape repeats a third time, extract a module in
modules/<name>/; each call still gets its own<component>.tfholding themoduleblock and whatever is specific to that component.
- The file is named after the component a resource is created for,
never after what the resource is. The database instance created for
- A file that outgrows a screen or two is usually two components wearing a trenchcoat. Split it by component, never by resource type.
- Modules live in
modules/<name>/and repeat the exact same layout internally,data.tfincluded. - A stack that only calls a module puts that call in
main.tf. There is no component to name the file after — the module is the whole stack — somain.tfis the honest name. The reserved files (terraform.tf,variables.tf,outputs.tf,terraform.tfvars, …) still apply. The moment the stack declares a resource of its own, or a second module call,main.tfis gone: every block moves to its<component>.tf.
Naming
snake_casefor everything: files, variables, outputs, locals, resource and data labels.- Never repeat the type in the label.
resource "google_sql_database" "toto", not"toto_database"— the reference already readsgoogle_sql_database.toto. - A resource is labelled after the component it belongs to. Whether the
label stops there depends on whether the type already says what the
resource is:
- A self-explanatory type, one of it in the component: the label is
exactly the component name. A database, a Mongo cluster, a service
account, a bucket —
google_sql_database.totoandgoogle_service_account.totosay everything. - An ambiguous type: always
<component>_<role>, even when there is only one.google_secret_manager_secret.totosays nothing about what it holds;google_secret_manager_secret.toto_api_keydoes. Same for config maps, random values, generic IAM bindings — anything whose type names a container rather than its content. A binding on a named resource takes that resource's label (google_secret_manager_secret_iam_member.toto_api_key). - Several self-explanatory resources of one type: all of them
<component>_<role>, none left bare —toto_primaryandtoto_replica, nevertotoplustoto_replica.
- A self-explanatory type, one of it in the component: the label is
exactly the component name. A database, a Mongo cluster, a service
account, a bucket —
- A
moduleblock follows the same rule, with the module's name in place of the component's. One unambiguous call ofmodules/postgresismodule "postgres". When the name alone does not say which call it is — several calls of the same module, or a generic module such assecret— every call is<module>_<role>:module "postgres_toto"andmodule "postgres_billing", neverpostgrespluspostgres_billing. thisonly inside a module, where the module is the component. Never label by index or by environment.- Name the thing, not its shape:
data.google_project.this, notdata.google_project.project_data. - Reusing an existing resource is the moment to challenge its name. When
a change references a resource created earlier, check its label against
these rules and against what the resource is now used for; an existing name
is not a reason to keep a wrong one. When it falls short, say so and
propose the better name: relabelling is a
movedblock, cheap and reviewable. Renaming the cloud-sidenameattribute usually forces replacement — flag that cost instead of changing it silently.
Variables
- Every variable declares a
typeand adescription. No exceptions, no barevariable "foo" {}. The description is a sentence that says what the value does and what happens when it changes — including "this forces replacement", which is exactly the fact a reviewer needs and the type cannot carry. - Use precise types:
map(object({...}))withoptional(x, default)beatsmap(any).anyis a promise to debug it at apply time. validationblocks on anything with a real constraint (a name length, an enum, a reserved key, a CIDR). Fail at plan, not at the API's 400.sensitive = trueon anything credential-shaped — and prefer not taking it as a variable at all (see Secrets).- Prefer a variable to a literal, as far as it goes. A project ID, a
region, a machine type, a version, a CIDR, a replica count, a domain — any
value an operator could plausibly want to read or change without reading
the resource code is a variable. Literals are for what is the code's own
identity (a component's name, a role string the API fixes) and nothing
else. Constructed values stay
locals, built from variables. - A root stack sets every variable's value in
terraform.tfvars, one file at the stack root, commented where the value is not self-evident. It is the stack's configuration at a glance; a value hidden in adefaultmakes the reader openvariables.tfto learn what is actually deployed. Defaults belong in modules, where they describe the common case for every caller. Secrets never go interraform.tfvars(see Secrets).
Outputs
- Every output has a
description. It is API surface for whatever consumes the state — another stack, a GitOps repo, a human. - Mark credential-shaped outputs
sensitive = true, and prefer emitting the reference (a Secret Manager secret name, a resource ID) over the value. - Output what another stack must agree with, not everything you happen to have.
Providers and versions
- Pin provider versions exactly (
version = "7.16.0"), and use the fully qualified source, with the hostname of the binary the repo runs:registry.opentofu.org/hashicorp/googleunder OpenTofu,registry.terraform.io/hashicorp/googleunder Terraform. The short form resolves to different registries under Terraform and OpenTofu and the two fight over.terraform/providersand the lock file every time you switch. required_versiongets a floor (>= 1.11), justified in a comment when the floor is not the obvious one — the feature that moved it.- Commit
.terraform.lock.hcl, and regenerate it with-platform=for every platform CI and laptops actually run on. - Provider configuration stays in the root module. Never declare a
providerblock inside a module — pass providers in explicitly when a module needs an aliased one.
Resources
for_eachovercount.countkeys state by index, so removing the middle element of a list re-creates everything after it.for_eachkeys by a stable string. Usecountonly for a genuine on/off toggle (count = var.enabled ? 1 : 0).- Never hardcode a value an operator could want to change — variable, with its
value in
terraform.tfvars(see Variables). - Use
localsfor anything constructed more than once (a resource-name format string, a principal, a label set). - Use
movedblocks to rename or restructure, neverstate mvby hand and never a destroy/recreate you did not intend.importblocks overterraform import, so adoption is reviewable in the diff. lifecycle { prevent_destroy = true }on anything holding data — databases, buckets, KMS keys.- No
provisioner, nolocal-exec, noremote-exec. They run on someone's laptop, are invisible to the plan, and are not idempotent. If a step cannot be expressed as a resource, it belongs in a script the pipeline calls, documented inREADME.md.
Secrets and state
- Treat the state file as a published document. Anything a resource attribute holds is in it, in cleartext.
- Prefer write-only arguments (
*_wowith*_wo_version) andephemeralblocks for generated credentials — the value exists only inside the apply and never lands in state. Where the provider offers no write-only form, store a reference (a Secret Manager secret name) and let the consumer resolve it at runtime. - Remote state only, versioned and encrypted, with locking.
- Never
outputa secret's value, never write one intoterraform.tfvars, never paste one into a plan output pasted into a PR.
Tests
- Tests live in
tests/*.tftest.hcland run in CI withterraform test. - At minimum: a
plan-mode test per stack asserting the invariants that actually matter (a name format, a count, a flag that must stay off). terraform testis not a file-scoped lint — run it in CI, not in pre-commit.
Docs
README.mdcarries the operational content plus theterraform-docsinjected block between<!-- BEGIN_TF_DOCS -->/<!-- END_TF_DOCS -->, generated from a committedterraform-docs.yml.- Bootstrap and recovery procedures go in
README.md, or a runbook it links. - The why — why this provider, why the VPC is adopted rather than created —
goes in
ARCHITECTURE.md. Only a version floor's reason stays in its comment, beside the line it explains.
pre-commit
Baseline hooks plus, pinned to one binary so laptop and CI agree:
- repo: https://github.com/antonbabenko/pre-commit-terraform
rev: <latest tag SHA> # frozen: <latest tag>
hooks:
- id: terraform_fmt
args:
- --hook-config=--tf-path=tofu
- id: terraform_validate
args:
- --hook-config=--tf-path=tofu
- id: terraform_tflint
args:
- --hook-config=--tf-path=tofu
- id: terraform_docs
args:
- --hook-config=--tf-path=tofu
- --args=--config=terraform-docs.yml
- repo: local
hooks:
- id: trivy-terraform
name: trivy config (Terraform)
entry: trivy config --exit-code 1 --misconfig-scanners terraform --tf-exclude-downloaded-modules .
language: system
pass_filenames: false
files: \.tf$The sample runs OpenTofu; in a Terraform repo, --tf-path=terraform.
terraform_docs must fail when the regenerated README.md differs from the
committed one, so an undocumented variable blocks the commit.
Scanning
The trivy-terraform hook above fails on every misconfiguration finding;
fix each at the source. Findings carry provider-prefixed IDs (AWS-0086,
GCP-0001, AZU-…). No self-authorised ignores (ci-conventions);
--tf-exclude-downloaded-modules is not one, as it skips third-party code the
repo cannot fix, never its own. Being a pre-commit hook, it also gates CI through quality.
Applying
planis the default answer to "does this work?";apply,destroyand-auto-approvewait for an explicit ask, every time.- Read the plan before proposing it: a replacement you did not expect is the bug, and the plan is where it is cheapest to find.
Never:
- Never put the
backend/cloudblock anywhere butbackend.tf, or aproviderblock anywhere butterraform.tf. - Never name a file after a technology (
postgres.tf,cloudsql.tf) when its resources exist for a component. - Never put a
datablock anywhere butdata.tf, and never splitdata.tfinto per-component data files. - Never spread one component's resources across files by resource type
(
iam.tf,secrets.tf,databases.tf) — they all go in<component>.tf. - Never label a component's resource anything but
<component>(one self-explanatory resource of its type) or<component>_<role>(an ambiguous type such as a secret, or several of a type). - Never label a
moduleblock anything but<module>(one unambiguous call) or<module>_<role>(a generic module, or several calls of one). - Never use
main.tfin a stack that holds anything besides a single module call. - Never put a
variable,outputorlocalsblock in a component file. - Never leave a root stack's variable value in a
defaultrather thanterraform.tfvars, and never write a literal where a variable fits. - Never ship a variable or output without a
description, or a variable without atype. - Never use
anywhere a concrete type can be written. - Never leave a provider version unpinned or use the short registry form.
- Never declare a
providerblock inside a module. - Never use
countto iterate over a collection —for_each. - Never use a
provisioner. - Never let a secret reach the state file, an output, or
terraform.tfvars. - Never hand-edit state or run
state mvwhere amovedblock would do. - Never silence a
tflintortrivyfinding on your own initiative.
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.

