Ia terraform

From iliaal/whetstone · View interactive version

Ia terraform is an AI agent skill from the GitHub repository iliaal/whetstone. Terraform and OpenTofu configuration, modules, testing, state management, and HCL review. Use when working with Terraform, OpenTofu, HCL, tfvars, tftest, state migration, or IaC patterns.

How to use this skill

Skill file

Terraform & OpenTofu

Working rules

File Organization & Naming

| File | Purpose |

|------|---------|

| terraform.tf | Terraform + provider version requirements |

| providers.tf | Provider configurations |

| main.tf | Primary resources and data sources |

| variables.tf | Input variables (alphabetical) |

| outputs.tf | Output values (alphabetical) |

| locals.tf | Local values |

Block Ordering

Resources: count/for_each (blank line after) → arguments → nested blocks → tags → depends_on → lifecycle (last)

Variables: description → type → default → validation → nullable

Every variable needs type + description. Every output needs description. Mark secrets sensitive = true.

Module Structure

| Type | Scope | Example |

|------|-------|---------|

| Resource Module | Single logical group | VPC + subnets, SG + rules |

| Infrastructure Module | Collection of resource modules | Networking + compute for one region |

| Composition | Complete infrastructure | Spans regions/accounts |

module-name/
├── main.tf, variables.tf, outputs.tf, versions.tf
├── examples/
│   ├── minimal/
│   └── complete/
└── tests/
    └── defaults.tftest.hcl

Keep modules small (single responsibility). examples/ double as documentation and integration test fixtures. Semantic versioning for all published modules.

count vs for_each

| Scenario | Use |

|----------|-----|

| Boolean toggle (create or skip) | count = condition ? 1 : 0 |

| Named/keyed items that may reorder | for_each = toset(list) or map |

| Fixed identical replicas | count = N |

Default to for_each: removing a middle item from a count list recreates all subsequent resources. Use count only for boolean conditionals or truly identical replicas.

Version Pinning

| Component | Strategy | Example |

|-----------|----------|---------|

| Terraform | Pin minor | required_version = "~> 1.9.0" |

| Providers | Pin major | version = "~> 5.0" |

| Modules (prod) | Pin exact | version = "5.1.2" |

| Modules (dev) | Allow patch | version = "~> 5.1.0" |

Key modern features: moved blocks (1.1+), optional() with defaults (1.3+), native testing (1.6+), mock providers (1.7+), removed blocks (1.7+), cross-variable validation (1.9+), write-only arguments (1.11+).

Stacks (HCP; check current release status): orchestrates multiple configs as a single deployment unit. Evaluate for multi-environment patterns.

State & Security

Troubleshooting

Dependency Management

Use locals with try() to control deletion ordering without explicit depends_on:

locals {
  vpc_id = try(aws_vpc_ipv4_cidr_block_association.this[0].vpc_id, aws_vpc.this.id, "")
}

This forces Terraform to destroy subnets before CIDR associations, which prevents deletion errors.

Verify

Run before declaring done:

terraform fmt -check && terraform validate && tflint && trivy config .

All commands must pass with zero errors. Inspect providers, data sources, provisioners, and external commands before selecting test files. Include safe plan tests and fully mocked apply tests in completion checks. Keep real-service or infrastructure tests behind the task's authorization and cost boundary. Pass the root-relative test filename, such as terraform test -filter=tests/vpc_unit_test.tftest.hcl, and verify that the intended file completed at least one run; an exit code alone can accept an unknown filter with no tests selected. Use the checked selection recipe in native-test-patterns.md.

Task-specific references

Read the relevant reference before implementing or reviewing the matching behavior:

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.