Terraform Code-Change Pack
URL slug: terraform-code-change
This page describes how the Terraform code-change pack parses unified git diff input for .tf files into analysis units and applies compliance rules. It is for engineers and platform administrators who review Terraform changes.
How the pack parses a diff
Section titled “How the pack parses a diff”parse_diff in opentremor_pack_terraform_code_change/parser.py reads the diff one hunk at a time:
- Core’s
iter_hunks(text, path_suffix=".tf")returns hunks from.tffiles. Core’schange_kinddetermines each unit’s action. - In each hunk, the parser looks for block-opening lines:
resource,module,data,variable,output,locals,terraform, andprovider. It tracks braces from each opening line to find where the block ends. - The parser creates a unit for each block with added or removed lines. Changed nonblank lines outside recognized blocks, such as comments between blocks, go into a fallback unit with type and name
unknown.
The parser only recognizes blocks whose opening line appears in the hunk. A block that opened before the hunk can therefore have its changed lines included in the fallback unit.
Block-to-identity mapping
Section titled “Block-to-identity mapping”| Block opens with | type | name |
|---|---|---|
resource "aws_db_instance" "orders" | aws_db_instance | orders |
module "rds_postgres" | module | rds_postgres |
data "aws_ami" "latest" | aws_ami | latest |
variable "env" | variable | env |
output "db_endpoint" | output | db_endpoint |
provider "aws" | provider | aws |
locals / terraform | same as keyword | same as keyword |
Action from diff markers
Section titled “Action from diff markers”| Markers present in the block | action |
|---|---|
+ only | add |
- only | delete |
| both | modify |
Diff markers stay in the body
Section titled “Diff markers stay in the body”body keeps its original +, -, and space prefixes. A rule that checks what changed needs to distinguish added lines from unchanged context.
Unit shape
Section titled “Unit shape”{ "hash": "<sha256 of the normalised body>", "type": "aws_db_instance", "name": "orders", "action": "add", "body": "+resource \"aws_db_instance\" \"orders\" {\n+ identifier = \"orders-primary\"\n+}", "metadata": { "generated_at_utc": "2026-06-22T10:30:00Z", "source_path": "infra/rds.tf", "group_path": null }}source_path comes from the diff header’s b/... path. group_path is null; that field holds the Terramate stacks of the terraform-plan pack, which this pack does not read.
How hashing and deduplication work
Section titled “How hashing and deduplication work”The pack hashes the normalized body with the same function as the Terraform plan pack. Normalization trims lines, drops blank lines and Terraform plan-time placeholders, and joins the remaining lines with newlines. Identical normalized bodies produce the same hash.
Rulesets
Section titled “Rulesets”Rules are Markdown files in this package’s opentremor_pack_terraform_code_change/rules/ directory:
| File | Served at |
|---|---|
code-change-rules.md | GET /v1/terraform-code-change/rules |
{type}-code-change-rules.md | GET /v1/terraform-code-change/rules?rule_type={type} |
What the default ruleset covers
Section titled “What the default ruleset covers”| Rule ID | Area | What it catches |
|---|---|---|
| TFCC-001 | Module compliance | A raw resource of a type the org wraps in a module; edit the rule’s default list |
| TFCC-002 | Module compliance | A module without a pinned version or ref, or with a branch name as its Git ref |
| TFCC-003 | Module compliance | A module from outside the org’s registry or repositories |
| TFCC-010 | Resource hygiene | A missing required tag key; edit the rule’s default list |
| TFCC-011 | Resource hygiene | A hardcoded account ID, region, or AMI ID where surrounding code uses variables |
| TFCC-020 | Security regression | Public access or a 0.0.0.0/0 or ::/0 network rule added |
| TFCC-021 | Security regression | A plaintext secret added |
| TFCC-022 | Security regression | A wildcard IAM action or resource without a condition |
| TFCC-023 | Security regression | Encryption at rest removed or turned off |
| TFCC-024 | Security regression | Audit logging removed or disabled |
| TFCC-025 | Security regression | Versioning, point-in-time recovery, backup retention, or deletion protection removed |
| TFCC-026 | Security regression | A role trust policy widened to include a new principal |
| TFCC-027 | Security regression | TLS verification turned off |
| TFCC-PERF-001, -002 | Performance | Capacity lowered; an external or HTTP data source added |
| TFCC-REL-001 to -005 | Reliability | Destroy protection or lifecycle safety removed, state locking turned off, a provider version constraint removed or widened, or redundancy lowered |
Performance and reliability findings have LOW or MEDIUM severity. They do not fail the merge check on their own. Core brings built-in rule rows that an org has not edited in line with this rules file (OpenTremor-core#0034).
Worked examples
Section titled “Worked examples”Raw resource instead of the approved module
Section titled “Raw resource instead of the approved module”diff --git a/infra/rds.tf b/infra/rds.tfnew file mode 100644--- /dev/null+++ b/infra/rds.tf@@ -0,0 +1,4 @@+resource "aws_db_instance" "orders" {+ identifier = "orders-primary"+ storage_encrypted = true+}One unit comes out with action add:
{ "hash": "a1b2c3...", "type": "aws_db_instance", "name": "orders", "action": "add", "body": "+resource \"aws_db_instance\" ...", "metadata": { "generated_at_utc": "...", "source_path": "infra/rds.tf", "group_path": null }}The LLM can match TFCC-001: the diff adds a raw aws_db_instance resource, a type in the rule’s default list of resources that may need an approved module.
Use the approved module
Section titled “Use the approved module”diff --git a/infra/rds.tf b/infra/rds.tfnew file mode 100644--- /dev/null+++ b/infra/rds.tf@@ -0,0 +1,5 @@+module "orders_db" {+ source = "git::https://internal.example.com/modules/rds.git?ref=v2.1.0"+ identifier = "orders-primary"+ environment = var.env+}One unit comes out with type: "module" and action add. The example source includes a Git ref, so it does not match TFCC-002’s unpinned-source condition. This example alone does not establish whether the source is approved under TFCC-003.
Modify an existing resource
Section titled “Modify an existing resource”diff --git a/infra/main.tf b/infra/main.tf--- a/infra/main.tf+++ b/infra/main.tf@@ -1,5 +1,5 @@ resource "aws_db_instance" "orders" { identifier = "orders-primary"- storage_encrypted = false+ storage_encrypted = true tags = {} }One unit comes out with action modify. Context lines stay in the body with the + and - lines, so the LLM can see that this diff enables encryption rather than disabling it.
A diff touching several files
Section titled “A diff touching several files”The parser processes each .tf file’s hunks separately. It ignores files that do not end in .tf, such as a README or CI configuration.