Terraform Style Guide

Per-tool style guide for Terraform / OpenTofu (HCL) β€” OSBR's Infrastructure as Code tooling (IaC is the category; Terraform is the tool). Shared rules: the Coding Style Guide. The policy that mandates IaC and sets tool choice, state, and resource-ownership rules is the Infrastructure Planning Policy β€” this page is the coding-level how-to. Requirement levels follow RFC 2119; tags 🌎 / 🏠 are defined in the Coding Style Guide.

1. Formatting 🌎

2. Naming 🌎

❌ The type is duplicated in the name:

resource "aws_instance" "web_api_aws_instance" {}
# referenced as aws_instance.web_api_aws_instance β€” redundant

βœ… The name is just the descriptive noun:

resource "aws_instance" "web_api" {}
# referenced as aws_instance.web_api

3. File Structure 🌎

4. Version Pinning 🌎

terraform {
  required_version = ">= 1.6"
  required_providers {
    cloudflare = {
      source  = "cloudflare/cloudflare"
      version = "~> 5.0"
    }
  }
}

5. Remote State 🏠

Rationale: diverges from Terraform's default of local terraform.tfstate. A laptop-local state file cannot be shared, locked, or recovered, and leaks secrets if committed β€” unacceptable for team infrastructure.

6. Secrets & Credentials 🏠

Rationale: stricter than the many published examples that inline tokens. A committed credential is a breach; scoping limits blast radius.

7. Modules 🌎

8. One Tool Owns Each Resource 🏠

Rationale: two tools on one resource produce state drift and conflicting applies. This is an OSBR hard rule, not a stylistic preference.

References