Skip to content
DashboardWebsite

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.

parse_diff in opentremor_pack_terraform_code_change/parser.py reads the diff one hunk at a time:

  1. Core’s iter_hunks(text, path_suffix=".tf") returns hunks from .tf files. Core’s change_kind determines each unit’s action.
  2. In each hunk, the parser looks for block-opening lines: resource, module, data, variable, output, locals, terraform, and provider. It tracks braces from each opening line to find where the block ends.
  3. 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 opens withtypename
resource "aws_db_instance" "orders"aws_db_instanceorders
module "rds_postgres"modulerds_postgres
data "aws_ami" "latest"aws_amilatest
variable "env"variableenv
output "db_endpoint"outputdb_endpoint
provider "aws"provideraws
locals / terraformsame as keywordsame as keyword
Markers present in the blockaction
+ onlyadd
- onlydelete
bothmodify

body keeps its original +, -, and space prefixes. A rule that checks what changed needs to distinguish added lines from unchanged context.

{
"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.

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.

Rules are Markdown files in this package’s opentremor_pack_terraform_code_change/rules/ directory:

FileServed at
code-change-rules.mdGET /v1/terraform-code-change/rules
{type}-code-change-rules.mdGET /v1/terraform-code-change/rules?rule_type={type}
Rule IDAreaWhat it catches
TFCC-001Module complianceA raw resource of a type the org wraps in a module; edit the rule’s default list
TFCC-002Module complianceA module without a pinned version or ref, or with a branch name as its Git ref
TFCC-003Module complianceA module from outside the org’s registry or repositories
TFCC-010Resource hygieneA missing required tag key; edit the rule’s default list
TFCC-011Resource hygieneA hardcoded account ID, region, or AMI ID where surrounding code uses variables
TFCC-020Security regressionPublic access or a 0.0.0.0/0 or ::/0 network rule added
TFCC-021Security regressionA plaintext secret added
TFCC-022Security regressionA wildcard IAM action or resource without a condition
TFCC-023Security regressionEncryption at rest removed or turned off
TFCC-024Security regressionAudit logging removed or disabled
TFCC-025Security regressionVersioning, point-in-time recovery, backup retention, or deletion protection removed
TFCC-026Security regressionA role trust policy widened to include a new principal
TFCC-027Security regressionTLS verification turned off
TFCC-PERF-001, -002PerformanceCapacity lowered; an external or HTTP data source added
TFCC-REL-001 to -005ReliabilityDestroy 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).

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.tf
new 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.

diff --git a/infra/rds.tf b/infra/rds.tf
new 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.

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.

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.