agentleFS
mchittinenimchittineni/cloud-platform-skills/.cursor/rules/skills/terraform-iac-modules.mdc

cloud-platform-skills / skills

Cursor rule1 starsChanged 3 months agoMITView on GitHub

Terraform and OpenTofu module architecture, remote state with locking and encryption, provider and module version pinning, drift detection, and safe plan/apply workflow. Use when structuring or restructuring a Terraform repository across dev, staging and production environments, writing reusable modules, configuring a state backend, or investigating unexplained infrastructure drift.

The file

We split this terraform-iac-modules.mdc into the rules it gives an agent

2 rules. About 0 are common, 1 is distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Don't: Never hardcode credentials in .tf files (access_key / secret_key).

    line 72
  2. Distinctive:

    Don't: Avoid monolithic single-state repositories that manage both foundation networking and application workloads in one state lock.

    line 73

≈ “Common” is an estimate: at least 1 in 5 of the 3,030 Cursor rule files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
terraform-iac-modules.mdc
---
description: Terraform and OpenTofu module architecture, remote state with locking and encryption, provider and module version pinning, drift detection, and safe plan/apply workflow. Use when structuring or restructuring a Terraform repository across dev, staging and production environments, writing reusable modules, configuring a state backend, or investigating unexplained infrastructure drift.
globs:
alwaysApply: false
---

# Production Terraform Infrastructure as Code Patterns

## When to Use This Skill

**Triggers — load this skill when:**

- An IaC repository needs module boundaries, versioning, or directory structure decided
- Remote state must be configured with locking, encryption, and least-privilege access
- Drift, a dirty plan, or an unsafe apply needs diagnosis

**Route elsewhere when:**

- Multi-account DRY orchestration and policy gates -> `enterprise-iac-governance-terragrunt`
- Cloud-specific resource design -> the `aws-*`, `azure-*`, or `gcp-*` skills

## 1. Modular Directory Layout

```text
terraform/
├── environments/
│   ├── dev/
│   │   ├── main.tf
│   │   ├── variables.tf
│   │   └── terraform.tfvars
│   └── prod/
│       ├── main.tf
│       └── terraform.tfvars
└── modules/
    └── secure-vpc/
        ├── main.tf
        ├── variables.tf
        └── outputs.tf
```

---

## 2. Remote State Locking & Encryption Configuration

```hcl
terraform {
  required_version = ">= 1.6.0"

  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }

  backend "s3" {
    bucket         = "company-tfstate-production"
    key            = "networking/vpc/terraform.tfstate"
    region         = "us-east-1"
    encrypt        = true
    dynamodb_table = "terraform-state-lock"
  }
}
```

---

## 3. Best Practices & Anti-Patterns

- **Do**: Maintain distinct state files per environment and per blast-radius tier (Network, Database, Compute, Apps).
- **Do**: Schedule automated `terraform plan -detailed-exitcode` checks in CI for drift detection.
- **Don't**: Never hardcode credentials in `.tf` files (`access_key` / `secret_key`); rely on OIDC or environment variables.
- **Don't**: Avoid monolithic single-state repositories that manage both foundation networking and application workloads in one state lock.

---

## 4. Terraform or OpenTofu

OpenTofu is the MPL-licensed fork of Terraform 1.5.x and remains configuration-compatible for
the patterns in this skill; the practical differences are licensing, registry defaults, and a
few post-fork features (OpenTofu state encryption, early variable evaluation).

```hcl
terraform {
  required_version = "~> 1.9"            # honoured by both binaries
  required_providers {
    aws = { source = "hashicorp/aws", version = "~> 5.60" }
  }
}
```

Migration is mechanical (`tofu init` against existing state), but pick one binary per repository
and pin it in CI — running `terraform` locally and `tofu` in the pipeline against shared state
invites provider-schema drift. Never mix the two against the same state file concurrently.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.