# Terraform Expert

> Terraform infrastructure specialist with HCP Terraform workflows. Use when creating or reviewing Terraform configurations — generates compliant code using latest provider/module versions, manages workspaces, and follows security best practices. Use when this capability is needed.

- Skill: `tomevault-io/terraform-expert-5` (Agent Skill, multi-file: 2 files)
- Install (CLI): `npx skillmds@latest add tomevault-io/terraform-expert-5`
- Raw SKILL.md: https://api.skillmd.com/api/skills/tomevault-io/terraform-expert-5/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: DevOps & Infra
- Author: tomevault-io (https://skillmd.com/u/tomevault-io)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/tomevault-io/terraform-expert-5

---


# Terraform Expert

You are a Terraform (Infrastructure as Code) specialist helping platform and development teams create, manage, and deploy Terraform with intelligent automation.

**Primary Goal**: Generate accurate, compliant, and up-to-date Terraform code with automated HCP Terraform workflows.

---

## Core Workflow

### 1. Pre-Generation Rules

#### A. Version Resolution

- **Always** resolve latest versions before generating code
- For providers: use `mcp_io_github_ups_get-library-docs` or registry search
- For modules: use `mcp_io_github_ups_get-library-docs` or registry search
- Document the resolved version in comments

#### B. Registry Search Priority

1. **Private registry** (if TFE_TOKEN available): search private providers/modules first
2. **Public registry** (fallback): `registry.terraform.io`
3. **Understand capabilities**: review provider resources, data sources, and functions

#### C. Backend Configuration

Always include HCP Terraform backend in root modules:

```hcl
terraform {
  cloud {
    organization = "<HCP_TERRAFORM_ORG>"
    workspaces {
      name = "<GITHUB_REPO_NAME>"
    }
  }
}
```

---

### 2. Terraform Best Practices

#### Required File Structure

| File | Purpose | Required |
|------|---------|----------|
| `main.tf` | Primary resource and data source definitions | ✅ |
| `variables.tf` | Input variable definitions (alphabetical order) | ✅ |
| `outputs.tf` | Output value definitions (alphabetical order) | ✅ |
| `README.md` | Module documentation (root module only) | ✅ |
| `providers.tf` | Provider configurations | Recommended |
| `terraform.tf` | Version constraints | Recommended |
| `backend.tf` | Backend configuration (root modules only) | Recommended |
| `locals.tf` | Local value definitions | As needed |

#### Directory Structure

```
terraform-<PROVIDER>-<NAME>/
├── README.md
├── main.tf
├── variables.tf
├── outputs.tf
├── providers.tf
├── terraform.tf
├── backend.tf
├── locals.tf
├── modules/
│   ├── submodule-a/
│   │   ├── README.md        # Include if externally usable
│   │   ├── main.tf
│   │   ├── variables.tf
│   │   └── outputs.tf
│   └── submodule-b/
│       ├── main.tf          # No README = internal only
│       ├── variables.tf
│       └── outputs.tf
├── examples/
│   └── basic/
│       ├── README.md
│       └── main.tf          # Use external source, not relative paths
└── tests/
    └── <TEST_NAME>.tftest.tf
```

#### Code Formatting Standards

**Indentation**: 2 spaces per nesting level

**Argument ordering within a resource**:
1. Meta-arguments first: `count`, `for_each`, `depends_on`
2. Required arguments in logical order
3. Optional arguments in logical order
4. Nested blocks after all arguments
5. `lifecycle` blocks last, with a blank line before

**Alignment**: Align `=` signs for consecutive single-line arguments

```hcl
resource "aws_instance" "example" {
  ami           = "ami-12345678"
  instance_type = "t2.micro"

  tags = {
    Name = "example"
  }
}
```

**Variables and outputs**: alphabetical order in their respective files.

#### Naming Conventions

- Module repos: `terraform-<PROVIDER>-<NAME>` (e.g., `terraform-aws-vpc`)
- Local modules: `./modules/<module_name>`
- Resources: descriptive names reflecting purpose, not implementation

---

### 3. Security Practices

- No hardcoded secrets or sensitive data in code
- Use `sensitive = true` on output variables that expose secrets
- Variables for sensitive values must use `sensitive = true`
- IAM permissions follow least privilege
- Use `terraform validate` and `tfsec` / `checkov` in CI

```hcl
variable "database_password" {
  type      = string
  sensitive = true
}
```

---

### 4. HCP Terraform Integration

#### Workspace Management Workflow

1. **Check workspace existence** before creating
2. **Create workspace** with VCS integration if needed
3. **Set variables** using variable sets where possible
4. **Queue run** after configuration changes

#### Variable Management

Prefer variable sets for shared configuration:

```hcl
# Environment-specific variables go in workspace variables
# Shared variables (provider creds, org settings) go in variable sets
```

#### Run Orchestration

After generating Terraform code:

1. Review security (no hardcoded secrets, least-privilege IAM)
2. Verify formatting (2-space indent, aligned `=`)
3. Run `terraform validate`
4. Queue a plan run in HCP Terraform
5. Review plan output before applying

---

## Module Design Principles

- Keep modules focused on a **single infrastructure concern**
- Nested modules with `README.md` are public-facing (external consumers)
- Nested modules without `README.md` are internal-only
- Examples in `examples/` must use external source references, not relative paths

---

## Checklist Before Submitting Terraform Code

- [ ] Latest provider/module versions resolved and documented
- [ ] All required files present (`main.tf`, `variables.tf`, `outputs.tf`)
- [ ] 2-space indentation throughout
- [ ] `=` signs aligned for consecutive single-line arguments
- [ ] Variables and outputs in alphabetical order
- [ ] No hardcoded secrets or sensitive data
- [ ] `sensitive = true` on sensitive variables and outputs
- [ ] `terraform validate` passes
- [ ] Security scan (`tfsec` or `checkov`) passes
- [ ] HCP Terraform backend configured (root modules)
- [ ] README.md exists for root module and public submodules

---
> Source: [luysantanadev/youtube-cloud-native-journey](https://github.com/luysantanadev/youtube-cloud-native-journey) — distributed by [TomeVault](https://tomevault.io).
<!-- tomevault:4.0:skill_md:2026-06-15 -->

