$npx -y skills add hashicorp/agent-skills --skill terraform-style-guideGenerate Terraform HCL code following HashiCorp's official style conventions and best practices. Use when writing, reviewing, or generating Terraform configurations.
| 1 | # Terraform Style Guide |
| 2 | |
| 3 | Generate and maintain Terraform code following HashiCorp's official style conventions and best practices. |
| 4 | |
| 5 | **Reference:** [HashiCorp Terraform Style Guide](https://developer.hashicorp.com/terraform/language/style) |
| 6 | |
| 7 | ## Code Generation Strategy |
| 8 | |
| 9 | When generating Terraform code: |
| 10 | |
| 11 | 1. Start with provider configuration and version constraints |
| 12 | 2. Create data sources before dependent resources |
| 13 | 3. Build resources in dependency order |
| 14 | 4. Add outputs for key resource attributes |
| 15 | 5. Use variables for all configurable values |
| 16 | |
| 17 | ## File Organization |
| 18 | |
| 19 | | File | Purpose | |
| 20 | |------|---------| |
| 21 | | `terraform.tf` | Terraform and provider version requirements | |
| 22 | | `providers.tf` | Provider configurations | |
| 23 | | `main.tf` | Primary resources and data sources | |
| 24 | | `variables.tf` | Input variable declarations (alphabetical) | |
| 25 | | `outputs.tf` | Output value declarations (alphabetical) | |
| 26 | | `locals.tf` | Local value declarations | |
| 27 | |
| 28 | ### Example Structure |
| 29 | |
| 30 | ```hcl |
| 31 | # terraform.tf |
| 32 | terraform { |
| 33 | required_version = ">= 1.14" |
| 34 | |
| 35 | required_providers { |
| 36 | aws = { |
| 37 | source = "hashicorp/aws" |
| 38 | version = "~> 6.0" |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | # variables.tf |
| 44 | variable "environment" { |
| 45 | description = "Target deployment environment" |
| 46 | type = string |
| 47 | |
| 48 | validation { |
| 49 | condition = contains(["dev", "staging", "prod"], var.environment) |
| 50 | error_message = "Environment must be dev, staging, or prod." |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | # locals.tf |
| 55 | locals { |
| 56 | common_tags = { |
| 57 | Environment = var.environment |
| 58 | ManagedBy = "Terraform" |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | # main.tf |
| 63 | resource "aws_vpc" "main" { |
| 64 | cidr_block = var.vpc_cidr |
| 65 | enable_dns_hostnames = true |
| 66 | |
| 67 | tags = merge(local.common_tags, { |
| 68 | Name = "${var.project_name}-${var.environment}-vpc" |
| 69 | }) |
| 70 | } |
| 71 | |
| 72 | # outputs.tf |
| 73 | output "vpc_id" { |
| 74 | description = "ID of the created VPC" |
| 75 | value = aws_vpc.main.id |
| 76 | } |
| 77 | ``` |
| 78 | |
| 79 | ## Code Formatting |
| 80 | |
| 81 | ### Indentation and Alignment |
| 82 | |
| 83 | - Use **two spaces** per nesting level (no tabs) |
| 84 | - Align equals signs for consecutive arguments |
| 85 | |
| 86 | ```hcl |
| 87 | resource "aws_instance" "web" { |
| 88 | ami = "ami-0c55b159cbfafe1f0" |
| 89 | instance_type = "t2.micro" |
| 90 | subnet_id = "subnet-12345678" |
| 91 | |
| 92 | tags = { |
| 93 | Name = "web-server" |
| 94 | Environment = "production" |
| 95 | } |
| 96 | } |
| 97 | ``` |
| 98 | |
| 99 | ### Block Organization |
| 100 | |
| 101 | Arguments precede blocks, with meta-arguments first: |
| 102 | |
| 103 | ```hcl |
| 104 | resource "aws_instance" "example" { |
| 105 | # Meta-arguments |
| 106 | count = 3 |
| 107 | |
| 108 | # Arguments |
| 109 | ami = "ami-0c55b159cbfafe1f0" |
| 110 | instance_type = "t2.micro" |
| 111 | |
| 112 | # Blocks |
| 113 | root_block_device { |
| 114 | volume_size = 20 |
| 115 | } |
| 116 | |
| 117 | # Lifecycle last |
| 118 | lifecycle { |
| 119 | create_before_destroy = true |
| 120 | } |
| 121 | } |
| 122 | ``` |
| 123 | |
| 124 | ## Naming Conventions |
| 125 | |
| 126 | - Use **lowercase with underscores** for all names |
| 127 | - Use **descriptive nouns** excluding the resource type |
| 128 | - Be specific and meaningful |
| 129 | - Resource names must be singular, not plural |
| 130 | - Default to `main` for resources where a specific descriptive name is redundant or unavailable, provided only one instance exists |
| 131 | |
| 132 | ```hcl |
| 133 | # Bad |
| 134 | resource "aws_instance" "webAPI-aws-instance" {} |
| 135 | resource "aws_instance" "web_apis" {} |
| 136 | variable "name" {} |
| 137 | |
| 138 | # Good |
| 139 | resource "aws_instance" "web_api" {} |
| 140 | resource "aws_vpc" "main" {} |
| 141 | variable "application_name" {} |
| 142 | ``` |
| 143 | |
| 144 | ## Variables |
| 145 | |
| 146 | Every variable must include `type` and `description`: |
| 147 | |
| 148 | ```hcl |
| 149 | variable "instance_type" { |
| 150 | description = "EC2 instance type for the web server" |
| 151 | type = string |
| 152 | default = "t2.micro" |
| 153 | |
| 154 | validation { |
| 155 | condition = contains(["t2.micro", "t2.small", "t2.medium"], var.instance_type) |
| 156 | error_message = "Instance type must be t2.micro, t2.small, or t2.medium." |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | variable "database_password" { |
| 161 | description = "Password for the database admin user" |
| 162 | type = string |
| 163 | sensitive = true |
| 164 | } |
| 165 | ``` |
| 166 | |
| 167 | ## Outputs |
| 168 | |
| 169 | Every output must include `description`: |
| 170 | |
| 171 | ```hcl |
| 172 | output "instance_id" { |
| 173 | description = "ID of the EC2 instance" |
| 174 | value = aws_instance.web.id |
| 175 | } |
| 176 | |
| 177 | output "database_password" { |
| 178 | description = "Database administrator password" |
| 179 | value = aws_db_instance.main.password |
| 180 | sensitive = true |
| 181 | } |
| 182 | ``` |
| 183 | |
| 184 | ## Dynamic Resource Creation |
| 185 | |
| 186 | ### Prefer for_each over count |
| 187 | |
| 188 | ```hcl |
| 189 | # Bad - count for multiple resources |
| 190 | resource "aws_instance" "web" { |
| 191 | count = var.instance_count |
| 192 | tags = { Name = "web-${count.index}" } |
| 193 | } |
| 194 | |
| 195 | # Good - for_each with named instances |
| 196 | variable "instance_names" { |
| 197 | type = set(string) |
| 198 | default = ["web-1", "web-2", "web-3"] |
| 199 | } |
| 200 | |
| 201 | resource "aws_instance" "web" { |
| 202 | for_each = var.instance_names |
| 203 | tags = { Name = each.key } |
| 204 | } |
| 205 | ``` |
| 206 | |
| 207 | ### count for Conditional Creation |
| 208 | |
| 209 | ```hcl |
| 210 | resource "aws_cloudwatch_metric_alarm" "cpu" { |
| 211 | count = var.enable_monitoring ? 1 : 0 |
| 212 | |
| 213 | alarm_name = "high-cpu-usage" |
| 214 | threshold = 80 |
| 215 | } |
| 216 | ``` |
| 217 | |
| 218 | ## Security Best Practices |
| 219 | |
| 220 | Refer to SECURITY.md. It includes guidance on encrypting resources, |
| 221 | preventing sensitive data in state, and secure configura |