agentleFS
hashi-demo-labhashi-demo-lab/claude-skill-hcp-terraform/terraform-style-guide/CLAUDE.md

claude-skill-hcp-terraform / terraform-style-guide

CLAUDE.md4 starsChanged 13 months agoMITView on GitHub

This skill provides comprehensive guidance on Terraform code style, formatting, and best practices based on HashiCorp's official standards. Use this skill when working with any Terraform configuration to ensure code quality, consistency, and maintainability. Proactively reference this skill when: - Writing new Terraform configurations - Reviewing or refactoring existing Terraform code - Formatting code before commits - Organizing files and directory structures - Establishing or enforcing team coding standards - Resolving style inconsistencies - Setting up version control configurations -…

The file

Show its rules and text

We split this CLAUDE.md into the rules it gives an agent

30 rules. About 0 are common, 27 are distinctive, and 3 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈27

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 3

    Very specific

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

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Consistency: - All code follows the same formatting and organizational patterns

    line 24
  2. Distinctive:

    Always recommend running these before committing:

    line 31
  3. Distinctive:

    Consider Git pre-commit hooks to automate these

    line 34
  4. Distinctive:

    Use descriptive nouns with underscores (not hyphens)

    line 45
  5. Distinctive:

    When reviewing Terraform code, check against the style guide:

    line 75
  6. Distinctive:

    When writing Terraform configurations:

    line 94
  7. Distinctive:

    Start with file structure: - Create standard files (terraform.tf, providers.tf, main.tf, etc.)

    line 96
  8. Distinctive:

    Apply formatting rules: - Use proper indentation, alignment, spacing

    line 97
  9. Distinctive:

    When refactoring existing code:

    line 104
  10. Distinctive:

    Minimize code changes: - follow bestpractice but focus on achieving outcome.

    line 106
  11. Distinctive:

    Update variable declarations: - Add missing types and descriptions

    line 108
  12. Distinctive:

    When initializing a new Terraform project:

    line 112
  13. Distinctive:

    When providing code examples:

    line 123
  14. Distinctive:

    Call out these common mistakes:

    line 152
  15. Distinctive:

    Use tabs for indentation

    line 155
  16. Distinctive:

    Use hyphens in names

    line 157
  17. Very specific:

    Use // or /* */ comments

    line 159
  18. Distinctive:

    Commit state files or .terraform directory

    line 160
  19. Distinctive:

    Use overly generic names

    line 161
  20. Distinctive:

    Use 2 spaces for indentation

    line 164
  21. Distinctive:

    Use descriptive nouns for names

    line 165
  22. Distinctive:

    Use underscores as separators

    line 166
  23. Very specific:

    Use # for comments

    line 168
  24. Distinctive:

    Use specific, meaningful names

    line 170
  25. Very specific:

    Run terraform fmt before committing

    line 184
  26. Distinctive:

    All variables need type and description

    line 185
  27. Distinctive:

    Use descriptive nouns with underscores

    line 186
  28. Distinctive:

    Follow standard file structure

    line 187
  29. Distinctive:

    When referencing specific sections, use markdown links:

    line 204
  30. Distinctive:

    Explain why: - Don't just fix, explain the style principle

    line 228

≈ “Common” is an estimate: at least 1 in 5 of the 12,729 CLAUDE.md 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
CLAUDE.md

Terraform Style Guide - Claude Code Guidance

Overview

This skill provides comprehensive guidance on Terraform code style, formatting, and best practices based on HashiCorp's official standards. Use this skill when working with any Terraform configuration to ensure code quality, consistency, and maintainability.

When to Apply This Skill

Proactively reference this skill when:

  • Writing new Terraform configurations
  • Reviewing or refactoring existing Terraform code
  • Formatting code before commits
  • Organizing files and directory structures
  • Establishing or enforcing team coding standards
  • Resolving style inconsistencies
  • Setting up version control configurations
  • Designing module structures
  • Implementing testing and validation

Core Concepts

Code Quality Pillars

  1. Consistency - All code follows the same formatting and organizational patterns
  2. Readability - Code is easy to understand with clear naming and structure
  3. Maintainability - Changes are easy to make without breaking functionality
  4. Scalability - Structure supports growth from simple to complex projects

The Three Fundamental Commands

Always recommend running these before committing:

  • terraform fmt - Auto-format code to standards
  • terraform validate - Catch syntax and configuration errors
  • Consider Git pre-commit hooks to automate these

Key Style Principles to Enforce

Formatting

  • 2 spaces for indentation (never tabs)
  • Align equals signs for consecutive arguments
  • Meta-arguments first (count, for_each), then standard args, then blocks
  • Blank lines separate logical groups

Naming

  • Use descriptive nouns with underscores (not hyphens)
  • Lowercase only
  • Exclude resource type from resource names (redundant)
  • Examples: web_server, database_primary, vpc_main

File Organization

Standard files in order of importance:

  1. terraform.tf - Version requirements
  2. providers.tf - Provider configs
  3. main.tf - Primary resources
  4. variables.tf - Input variables (alphabetical)
  5. outputs.tf - Output values (alphabetical)
  6. locals.tf - Local values
  7. backend.tf - State backend config

Resource Organization

  • Data sources before resources that reference them
  • Dependent resources after their dependencies
  • Standard parameter order within resources (meta-arguments → args → blocks → lifecycle → depends_on)

Variables and Outputs

Always require:

  • type on all variables
  • description on all variables and outputs
  • Mark sensitive = true for secrets

Common Scenarios

Scenario 1: Code Review

When reviewing Terraform code, check against the style guide:

Style Guide Checklist:
- [ ] Code formatted with `terraform fmt`
- [ ] All variables have type and description
- [ ] Resource names use descriptive nouns with underscores
- [ ] Files organized according to standard structure
- [ ] Version constraints pinned
- [ ] Sensitive values marked appropriately
- [ ] .gitignore configured correctly

Reference specific sections:

  • "See the Naming Conventions section for resource naming"
  • "Review Variables and Outputs for required attributes"

Scenario 2: Writing New Code

When writing Terraform configurations:

  1. Start with file structure - Create standard files (terraform.tf, providers.tf, main.tf, etc.)
  2. Apply formatting rules - Use proper indentation, alignment, spacing
  3. Follow naming conventions - Descriptive nouns with underscores
  4. Add complete metadata - Type and description for all variables
  5. Organize logically - Data sources first, group related resources

Scenario 3: Refactoring

When refactoring existing code:

  1. Minimize code changes - follow bestpractice but focus on achieving outcome.
  2. Run terraform fmt - Auto-fix formatting issues
  3. Update variable declarations - Add missing types and descriptions

Scenario 4: Setting Up New Project

When initializing a new Terraform project:

  1. Create standard file structure
  2. Configure .gitignore properly
  3. Pin versions in terraform.tf
  4. Set up provider configurations
  5. Consider Git hooks for fmt and validate
  6. Add README with project documentation

Code Examples

When providing code examples:

  1. Always use proper formatting (2-space indentation, aligned equals)
  2. Include required attributes (type, description)
  3. Show before/after for improvements
  4. Add comments explaining style choices
  5. Reference specific style guide sections

Example template:

# Good - follows style guide
resource "aws_instance" "web_server" {
  # Meta-arguments first
  count = var.instance_count

  # Standard arguments (aligned)
  ami           = data.aws_ami.ubuntu.id
  instance_type = var.instance_type

  # Blocks last
  tags = {
    Name = "web-${count.index}"
  }
}

Anti-Patterns to Avoid

Call out these common mistakes:

❌ Don't:

  • Use tabs for indentation
  • Include resource type in resource name
  • Use hyphens in names
  • Skip variable types or descriptions
  • Use // or /* */ comments
  • Commit state files or .terraform directory
  • Use overly generic names

✅ Do:

  • Use 2 spaces for indentation
  • Use descriptive nouns for names
  • Use underscores as separators
  • Include complete variable metadata
  • Use # for comments
  • Configure .gitignore properly
  • Use specific, meaningful names

Integration with Other Skills

This skill complements:

  • terraform-test - Style guide informs test structure and organization
  • terraform-stacks - Apply style conventions to terraform stack configurations
  • General Terraform development - Foundation for all Terraform work

Quick Reference

Most Important Rules

  1. Run terraform fmt before committing
  2. All variables need type and description
  3. Use descriptive nouns with underscores
  4. Follow standard file structure
  5. Pin version constraints

File Organization Quick Ref

/
├── terraform.tf     # Versions
├── providers.tf     # Provider configs
├── main.tf          # Resources
├── variables.tf     # Inputs (alphabetical)
├── outputs.tf       # Outputs (alphabetical)
├── locals.tf        # Local values

Referencing the Style Guide

When referencing specific sections, use markdown links:

  • Code Formatting Standards
  • Naming Conventions
  • File Organization
  • Variables and Outputs
  • Version Control

Success Criteria

Code following this style guide should:

  • ✅ Pass terraform fmt without changes
  • ✅ Pass terraform validate without errors
  • ✅ Be immediately readable by any team member
  • ✅ Follow consistent patterns throughout
  • ✅ Include complete metadata (types, descriptions)
  • ✅ Be organized in standard file structure
  • ✅ Use meaningful, descriptive names
  • ✅ Have proper version constraints
  • ✅ Exclude sensitive files from version control

Tips for Claude Code

  • Be proactive - Suggest style improvements even when not explicitly asked
  • Explain why - Don't just fix, explain the style principle
  • Show examples - Provide before/after comparisons
  • Reference sections - Point to specific style guide sections
  • Use checklists - Help users verify compliance systematically
  • Automate - Recommend terraform fmt and Git hooks
  • Be consistent - Apply the same standards across all Terraform code

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.