Terraform
HashiCorp's official agent guidance for authoring, testing, importing, and
refactoring Terraform infrastructure-as-code. Follows the official
Terraform Style Guide.
When to use
- Writing or reviewing any
.tf / HCL configuration.
- Generating resources, variables, outputs, modules.
- Importing existing (unmanaged) cloud resources into Terraform.
- Writing
.tftest.hcl tests or refactoring configs into reusable modules.
File organization
One concern per file (do not cram everything into main.tf):
| File |
Purpose |
terraform.tf |
required_version + required_providers constraints |
providers.tf |
Provider configurations |
main.tf |
Primary resources and data sources |
variables.tf |
Input variables (alphabetical) |
outputs.tf |
Outputs (alphabetical) |
locals.tf |
Local values |
Generation strategy (in order)
- Provider config + version constraints.
- Data sources before the resources that depend on them.
- Resources in dependency order.
- Outputs for key attributes.
- Variables for every configurable value.
Style rules
- Two spaces per nesting level, no tabs; align
= for consecutive args.
- Block order inside a resource: meta-arguments (
count/for_each) first,
then arguments, then nested blocks, then lifecycle last.
- Names: lowercase_with_underscores, descriptive nouns, singular, exclude the
resource type. Use
main when only one instance exists and no better name fits.
Bad: aws_instance.web_apis; Good: aws_instance.web_api, aws_vpc.main.
- Every variable has
type + description (and validation where useful);
mark secrets sensitive = true.
- Every output has
description; mark secret outputs sensitive = true.
Dynamic creation
- Prefer
for_each (named, stable keys) over count for multiple resources.
- Use
count = condition ? 1 : 0 only for conditional creation.
Version pinning
terraform {
required_version = ">= 1.14"
required_providers {
aws = { source = "hashicorp/aws", version = "~> 6.0" }
}
}
Operators: = exact, >= floor, ~> 1.0 pessimistic (rightmost increments),
>= 1.0, < 2.0 range. Use latest provider major + latest Terraform minor unless
constrained by the lock file or dependent modules.
Validate before commit
terraform fmt -recursive
terraform validate
Optional: tflint (lint), checkov / tfsec (security scan).
Version control hygiene
- Never commit:
terraform.tfstate*, .terraform/, *.tfplan, sensitive *.tfvars.
- Always commit: all
.tf files and .terraform.lock.hcl.
Testing (.tftest.hcl)
Terraform's built-in framework runs against temporary resources, protecting real
state. Concepts: a test file holds run blocks; each run is one scenario
with optional variables/providers and assert blocks; mode is apply
(default, real resources) or plan (logic only). Mock providers simulate a
provider without real infra (Terraform 1.7.0+ only). Run with terraform test.
Importing existing resources (Search + bulk import)
For bringing unmanaged infra under Terraform / migrating to IaC:
- Identify the target resource type (e.g.
aws_s3_bucket).
- Verify provider list-resource support FIRST — requires Terraform >= 1.14 and a
provider with list-resource support (latest provider version).
- Use Terraform Search (
list / tfquery block) to discover resources, then
generate config + import blocks for bulk import.
Code review checklist
Notes / gotchas
- The upstream repo splits Terraform into many sub-skills under
terraform/ (code-generation: style-guide, test, search-import,
azure-verified-modules; module-generation: refactor-module, terraform-stacks;
provider-development: provider-resources/actions/docs/tests, new-terraform-provider).
Helper scripts and deep references (e.g. terraform-search-import/scripts/list_resources.sh,
terraform-test/references/{MOCK_PROVIDERS,CI_CD,EXAMPLES}.md,
terraform-stacks/references/*) live at those source paths — fetch on demand
rather than duplicating here.
- Terraform Cloud/Enterprise work can use
terraform-mcp-server with
TFE_TOKEN + TFE_ADDRESS env vars.
- For provider development (Go) or Terraform Stacks orchestration, pull the
dedicated sub-skill from the source repo.
1---2name: terraform3description: "Use when writing, reviewing, or generating Terraform/HCL infrastructure-as-code — .tf files, providers, modules, variables, outputs, for_each/count, terraform fmt/validate/plan/apply, .tftest.hcl tests, importing existing cloud resources, or refactoring monoliths into reusable modules."4version: 1.0.05license: MPL-2.06tags: [terraform, hcl, iac, infrastructure-as-code, hashicorp, modules, terraform-test, devops, aws, cloud]7source: https://github.com/hashicorp/agent-skills8derived_from: awesomeclaude9prerequisites:10 commands: [terraform]11metadata:12 source: https://skillmd.com/skills/aashutosh396/terraform13---1415# Terraform1617HashiCorp's official agent guidance for authoring, testing, importing, and18refactoring Terraform infrastructure-as-code. Follows the official19[Terraform Style Guide](https://developer.hashicorp.com/terraform/language/style).2021## When to use2223- Writing or reviewing any `.tf` / HCL configuration.24- Generating resources, variables, outputs, modules.25- Importing existing (unmanaged) cloud resources into Terraform.26- Writing `.tftest.hcl` tests or refactoring configs into reusable modules.2728## File organization2930One concern per file (do not cram everything into `main.tf`):3132| File | Purpose |33|------|---------|34| `terraform.tf` | `required_version` + `required_providers` constraints |35| `providers.tf` | Provider configurations |36| `main.tf` | Primary resources and data sources |37| `variables.tf` | Input variables (alphabetical) |38| `outputs.tf` | Outputs (alphabetical) |39| `locals.tf` | Local values |4041## Generation strategy (in order)42431. Provider config + version constraints.442. Data sources before the resources that depend on them.453. Resources in dependency order.464. Outputs for key attributes.475. Variables for every configurable value.4849## Style rules5051- Two spaces per nesting level, no tabs; align `=` for consecutive args.52- Block order inside a resource: meta-arguments (`count`/`for_each`) first,53 then arguments, then nested blocks, then `lifecycle` last.54- Names: lowercase_with_underscores, descriptive nouns, singular, exclude the55 resource type. Use `main` when only one instance exists and no better name fits.56 Bad: `aws_instance.web_apis`; Good: `aws_instance.web_api`, `aws_vpc.main`.57- Every **variable** has `type` + `description` (and `validation` where useful);58 mark secrets `sensitive = true`.59- Every **output** has `description`; mark secret outputs `sensitive = true`.6061## Dynamic creation6263- Prefer `for_each` (named, stable keys) over `count` for multiple resources.64- Use `count = condition ? 1 : 0` only for conditional creation.6566## Version pinning6768```hcl69terraform {70 required_version = ">= 1.14"71 required_providers {72 aws = { source = "hashicorp/aws", version = "~> 6.0" }73 }74}75```7677Operators: `=` exact, `>=` floor, `~> 1.0` pessimistic (rightmost increments),78`>= 1.0, < 2.0` range. Use latest provider major + latest Terraform minor unless79constrained by the lock file or dependent modules.8081## Validate before commit8283```bash84terraform fmt -recursive85terraform validate86```8788Optional: `tflint` (lint), `checkov` / `tfsec` (security scan).8990## Version control hygiene9192- Never commit: `terraform.tfstate*`, `.terraform/`, `*.tfplan`, sensitive `*.tfvars`.93- Always commit: all `.tf` files and `.terraform.lock.hcl`.9495## Testing (`.tftest.hcl`)9697Terraform's built-in framework runs against temporary resources, protecting real98state. Concepts: a **test file** holds `run` blocks; each `run` is one scenario99with optional `variables`/`providers` and `assert` blocks; mode is `apply`100(default, real resources) or `plan` (logic only). **Mock providers** simulate a101provider without real infra (Terraform 1.7.0+ only). Run with `terraform test`.102103## Importing existing resources (Search + bulk import)104105For bringing unmanaged infra under Terraform / migrating to IaC:1061071. Identify the target resource type (e.g. `aws_s3_bucket`).1082. Verify provider list-resource support FIRST — requires Terraform >= 1.14 and a109 provider with list-resource support (latest provider version).1103. Use Terraform Search (`list` / `tfquery` block) to discover resources, then111 generate config + `import` blocks for bulk import.112113## Code review checklist114115- [ ] `terraform fmt` clean, `terraform validate` passes.116- [ ] Standard file layout; vars have type+description; outputs have description.117- [ ] Descriptive snake_case names; version constraints pinned.118- [ ] Secrets marked `sensitive`; no hardcoded credentials.119120## Notes / gotchas121122- The upstream repo splits Terraform into many sub-skills under123 `terraform/` (code-generation: style-guide, test, search-import,124 azure-verified-modules; module-generation: refactor-module, terraform-stacks;125 provider-development: provider-resources/actions/docs/tests, new-terraform-provider).126 Helper scripts and deep references (e.g. `terraform-search-import/scripts/list_resources.sh`,127 `terraform-test/references/{MOCK_PROVIDERS,CI_CD,EXAMPLES}.md`,128 `terraform-stacks/references/*`) live at those source paths — fetch on demand129 rather than duplicating here.130- Terraform Cloud/Enterprise work can use `terraform-mcp-server` with131 `TFE_TOKEN` + `TFE_ADDRESS` env vars.132- For provider *development* (Go) or Terraform Stacks orchestration, pull the133 dedicated sub-skill from the source repo.