skills/terraform-best-practices/SKILL.md
Comprehensive best practices for Terraform infrastructure as code from Anton Babenko's community guide
npx skillsauth add enuno/claude-command-and-control terraform-best-practicesInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Comprehensive community best practices for Terraform infrastructure as code, based on Anton Babenko's widely-adopted guide at terraform-best-practices.com.
Activate this skill when:
Small Infrastructure (< 20 resources)
Medium Infrastructure (20-100 resources)
Large Infrastructure (100+ resources)
Resource Modules
terraform-aws-modules/vpc/awsInfrastructure Modules
Compositions
# Small Infrastructure
terraform/
main.tf
variables.tf
outputs.tf
terraform.tfvars
# Medium Infrastructure
terraform/
modules/
vpc/
compute/
environments/
dev/
prod/
# Large Infrastructure (Terragrunt)
infrastructure/
_global/
dev/
vpc/
terragrunt.hcl
compute/
terragrunt.hcl
prod/
vpc/
compute/
# Pattern: {project}-{environment}-{resource-type}-{name}
resource "aws_s3_bucket" "main" {
bucket = "myapp-prod-data-customer-uploads"
}
# Pattern: this for single resource of type
resource "aws_security_group" "this" {
name = "${var.project}-${var.environment}-app"
}
instance_type, vpc_cidr_blockenable_ or create_: enable_monitoring, create_vpcsubnet_ids, availability_zonesmain.tf - Primary resource definitionsvariables.tf - Input variablesoutputs.tf - Output valuesversions.tf - Provider and Terraform version constraintsdata.tf - Data sources (optional)locals.tf - Local values (optional)# Use terraform fmt
# Group related settings
resource "aws_instance" "web" {
ami = data.aws_ami.ubuntu.id
instance_type = var.instance_type
tags = {
Name = "${var.project}-web"
Environment = var.environment
ManagedBy = "Terraform"
}
}
# Align equals signs in blocks
variable "instance_config" {
type = object({
instance_type = string
volume_size = number
volume_type = string
})
}
# versions.tf - Pin versions
terraform {
required_version = ">= 1.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
# variables.tf - Document everything
variable "vpc_cidr" {
description = "CIDR block for VPC"
type = string
default = "10.0.0.0/16"
validation {
condition = can(cidrhost(var.vpc_cidr, 0))
error_message = "Must be valid IPv4 CIDR."
}
}
# Use remote state for team collaboration
terraform {
backend "s3" {
bucket = "myapp-terraform-state"
key = "prod/vpc/terraform.tfstate"
region = "us-east-1"
encrypt = true
dynamodb_table = "terraform-state-lock"
}
}
.tfstate files to version control (contains plaintext secrets)sensitive = true for sensitive outputsterraform_remote_state data source for cross-stack references✅ Small to medium infrastructure (< 50 resources) ✅ Single cloud provider ✅ Few environments (dev/prod) ✅ Team comfortable with DRY through modules
✅ Large infrastructure (100+ resources) ✅ Many environments (dev/staging/prod/dr) ✅ Deep directory hierarchies ✅ Need for inheritance and composition ✅ Complex dependency orchestration
# environments/dev/main.tf
module "infrastructure" {
source = "../../modules/infrastructure"
environment = "dev"
instance_type = "t3.micro"
instance_count = 1
}
# environments/prod/main.tf
module "infrastructure" {
source = "../../modules/infrastructure"
environment = "prod"
instance_type = "t3.large"
instance_count = 3
}
# modules/infrastructure/main.tf
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "~> 5.0"
name = "${var.project}-${var.environment}"
cidr = var.vpc_cidr
}
module "security_group" {
source = "terraform-aws-modules/security-group/aws"
version = "~> 5.0"
name = "${var.project}-${var.environment}-app"
vpc_id = module.vpc.vpc_id
}
resource "aws_instance" "bastion" {
count = var.create_bastion ? 1 : 0
ami = data.aws_ami.ubuntu.id
instance_type = "t3.micro"
}
# Access with: aws_instance.bastion[0]
Problem: Circular dependencies between modules, version conflicts Solution:
Problem: "Error acquiring state lock" Solution:
terraform force-unlock cautiously.terraform.lock.hclProblem: Manual changes outside Terraform Solution:
terraform plan regularly in CIterraform refresh to detect driftProblem: Changing count causes resource recreation Solution:
for_each with maps for stable resourcescount only for simple on/off toggles# Bad - index changes cause recreation
resource "aws_subnet" "example" {
count = length(var.azs)
cidr_block = cidrsubnet(var.vpc_cidr, 8, count.index)
}
# Good - stable keys prevent recreation with explicit mapping
locals {
az_cidrs = {
"us-east-1a" = cidrsubnet(var.vpc_cidr, 8, 0)
"us-east-1b" = cidrsubnet(var.vpc_cidr, 8, 1)
"us-east-1c" = cidrsubnet(var.vpc_cidr, 8, 2)
}
}
resource "aws_subnet" "example" {
for_each = local.az_cidrs
availability_zone = each.key
cidr_block = each.value
}
references/terraform.md for code structure examplesterraform fmt and tflint from day oneComplete documentation extracted from terraform-best-practices.com covering:
Practical examples demonstrating:
Multilingual index of all content (20+ languages available on source website)
# Initialize and validate
terraform init
terraform validate
terraform fmt -recursive
# Plan and apply
terraform plan -out=tfplan
terraform apply tfplan
# State management
terraform state list
terraform state show aws_instance.web
terraform state mv aws_instance.old aws_instance.new
# Workspace management
terraform workspace list
terraform workspace select dev
terraform workspace new staging
# Import existing resources
terraform import aws_instance.web i-1234567890abcdef0
# Debugging
TF_LOG=DEBUG terraform apply
terraform console # Interactive evaluation
To refresh with latest best practices:
skill-seekers scrape https://www.terraform-best-practices.com/ \
--name terraform-best-practices \
--max-pages 50
tools
MemPalace local-first AI memory system. Use when setting up persistent memory for Claude Code sessions, mining project files or conversation transcripts, querying past context, configuring MCP tools, managing the knowledge graph, or troubleshooting palace operations.
tools
LangSmith Python SDK — trace, evaluate, and monitor LLM applications. Covers @traceable decorator, trace context manager, Client API, evaluate() / aevaluate(), comparative evaluation, custom evaluators, dataset management, prompt caching, ASGI middleware, and pytest plugin.
development
LangGraph (Python) — build stateful, controllable agent graphs with checkpointing, streaming, persistence, interrupts, fault tolerance, and durable execution. Covers both Graph API (StateGraph) and Functional API (@entrypoint/@task).
development
LangGraph Graph API (Python) — build explicit DAG agent workflows with StateGraph, typed state, nodes, edges, Command routing, Send fan-out, checkpointers, interrupts, and streaming. Use when you need explicit control flow and graph topology.