terraform
When to use
Use this skill when writing or modifying Terraform configurations (.tf files), creating new infrastructure modules, or understanding AWS resource definitions.
Procedure: Write Terraform config
- Read the infrastructure repo structure (check
agents/overrides/skills/terraform.mdfor the repo location). - Check existing modules in
modules/for patterns and conventions. - Read
variables.tfof the target module to understand required inputs. - Check
versions.tffor provider version constraints.
Project structure (typical)
{infrastructure-repo}/
├── environments/
│ ├── pro/ # Production environment
│ │ ├── root.hcl # Terragrunt root config
│ │ ├── core/ # Core infrastructure (VPC, DNS zones)
│ │ └── {service}/ # Per-service resources
│ └── sta/ # Stage environment
│ └── ...
├── modules/
│ ├── core/ # VPC, DNS, shared resources
│ └── {service}/ # Per-service module (ECS, ALB, ECR, etc.)
└── Taskfile.yml # Task runner commands (or Makefile)
Read agents/overrides/skills/terraform.md for the actual repository layout and service names.
Conventions
Provider versions
- Always pin provider versions in
versions.tf. - Check
versions.tfin the existing modules for the project's version constraints.
Module sources
Prefer community or organization-specific Terraform modules from the Terraform Registry:
module "alb" {
source = "{org}/application-load-balancer/aws"
version = ">= 1.0.0, < 2.0.0"
}
Check existing modules in the project for which registry modules are used.
Naming
- Resource prefix:
var.global_prefix(e.g.,{project}-{env}) - All resources must include
tags = var.tags - Security groups:
${var.global_prefix}-<purpose>(e.g.,-ecs,-mysql,-redis) - Log groups:
/aws/ecs/${cluster}/${service}
State management
- Remote state in S3 with DynamoDB locking.
- State is encrypted.
- Key pattern:
${path_relative_to_include()}/terraform.tfstate
Variables
- Use typed
variableblocks withdescription. - Use
object()types for complex inputs (notanyunless unavoidable). - Use
optional()with defaults where appropriate. - Group variables by domain (naming, network, ECS, Redis, database, etc.).
Lifecycle rules
- Use
ignore_changes = [task_definition]on ECS services — task definitions are managed by CI/CD, not Terraform. - Use
deletion_protection = trueon databases.
Security
- OIDC authentication for GitHub Actions (no long-lived credentials).
- Secrets stored in AWS Secrets Manager.
- Security groups follow least-privilege: only allow traffic between known services.
- IAM policies use specific resource ARNs, not wildcards (except where unavoidable).
Permissive defaults and missing security blocks
The failure that reaches production is almost never a pinned-version mistake;
it is a resource that works either way and was left on its permissive default.
Four constructs carry it. Each backstop grep is over the .tf tree you are
changing, not over this repository.
| Backstop grep | What a hit means | Override condition |
|---|---|---|
| grep -rn '0\.0\.0\.0/0' --include='*.tf' . | An ingress or egress rule whose CIDR is 0.0.0.0/0, i.e. open to the whole internet. On a management or database port stop and treat it as CRITICAL - the corpus row enumerates which ports those are. | A deliberately public listener - an ALB or CloudFront origin on 80/443. Nothing else. Record the reason next to the rule. |
| grep -rn 'publicly_accessible\|block_public_acls\|ignore_public_acls' --include='*.tf' . | Either publicly_accessible = true on a database, or a bucket with no public-access block present at all. Absence is the more common hit and the grep returning nothing is the failing case. | A bucket that genuinely serves public web assets, with the policy written explicitly rather than inherited from a default. |
| grep -rnE '"(Action\|Resource)" *: *"\*"\|= *\["\*"\]' --include='*.tf' . | A wildcard IAM action or resource. One compromised workload credential then reaches everything in the account. | A named, reviewed exception - iam:PassRole scoped by condition, or a bootstrap role. Never the default. |
| grep -rL 'encrypt' --include='*.tf' . (lists files with NO encryption mention; -L prints names only, so no -n) | A storage, volume, snapshot, or database resource with no encryption block. | None for data at rest. A resource holding no data at all. |
Read the surface class before the fix, not after: the mitigation text, the
abuse case, and the negative test live in the infrastructure rows of