Skip to content
Sarj Code Standards

canonical-terraform-declarations

Keep variable and output declarations in their owning module canonical interface files.

Why

A module's inputs and exports form its interface. Canonical files expose that interface without introducing folders Terraform does not load.

Fix

Consolidate declarations into variables.tf or outputs.tf in the same module directory; use variables.tf.json or outputs.tf.json for JSON. Keep implementation and deployment entrypoints in place. Review comments, override files and duplicate declarations before moving blocks.

Examples

Before — flagged Keep the interface visible in its owning module
service.tf
variable "region" { type = string }
resource "terraform_data" "this" { input = var.region }
variables.tf
variable "project" { type = string }
After — preferred A module owns its input file
variables.tf
variable "region" { type = string }
Autofix output
service.tf
resource "terraform_data" "this" { input = var.region }
variables.tf
variable "project" { type = string }
variable "region" { type = string }
Before — flagged JSON declarations belong to the same owning module
service.tf.json
{ "output": { "endpoint": { "value": "${var.endpoint}" } } }
After — preferred A module owns its JSON output file
outputs.tf.json
{ "output": { "endpoint": { "value": "${var.endpoint}" } } }