// Infrastructure as Code with Terraform and Terragrunt. Use for creating, validating, troubleshooting, and managing Terraform configurations, modules, and state. Covers Terraform workflows, best practices, module development, state management, Terragrunt patterns, and common issue resolution.
| name | iac-terraform |
| description | Infrastructure as Code with Terraform and Terragrunt. Use for creating, validating, troubleshooting, and managing Terraform configurations, modules, and state. Covers Terraform workflows, best practices, module development, state management, Terragrunt patterns, and common issue resolution. |
Comprehensive guidance for infrastructure as code using Terraform and Terragrunt, from development through production deployment.
Use this skill when:
Workflow Decision Tree:
Is this reusable across environments/projects?
├─ Yes → Create a Terraform module
│ └─ See "Creating Terraform Modules" below
└─ No → Create environment-specific configuration
└─ See "Environment Configuration" below
When building reusable infrastructure:
python3 scripts/init_module.py my-module-name
This automatically creates:
Use module template structure:
assets/templates/MODULE_TEMPLATE.md for complete structuremain.tf, variables.tf, outputs.tf, versions.tf, README.mdexamples/ directory with working examplesFollow module best practices:
validation blockssensitive = trueValidate module:
python3 scripts/validate_module.py /path/to/module
This checks for:
cd examples/complete
terraform init
terraform plan
terraform-docs markdown . > README.mdKey Module Patterns:
See references/best_practices.md "Module Design" section for:
For environment-specific infrastructure:
environments/
├── dev/
├── staging/
└── prod/
environment/
├── main.tf # Resource definitions
├── variables.tf # Variable declarations
├── terraform.tfvars # Default values (committed)
├── secrets.auto.tfvars # Sensitive values (.gitignore)
├── backend.tf # State configuration
├── outputs.tf # Output values
└── versions.tf # Version constraints
module "vpc" {
source = "git::https://github.com/company/terraform-modules.git//vpc?ref=v1.2.0"
name = "${var.environment}-vpc"
vpc_cidr = var.vpc_cidr
environment = var.environment
}
When to inspect state:
Inspect state and check health:
python3 scripts/inspect_state.py /path/to/terraform/directory
Check for drift:
python3 scripts/inspect_state.py /path/to/terraform/directory --check-drift
The script provides:
Manual state operations:
# List all resources
terraform state list
# Show specific resource
terraform state show aws_instance.web
# Remove from state (doesn't destroy)
terraform state rm aws_instance.web
# Move/rename resource
terraform state mv aws_instance.web aws_instance.web_server
# Import existing resource
terraform import aws_instance.web i-1234567890abcdef0
State best practices: See references/best_practices.md "State Management" section for:
# 1. Initialize (first time or after module changes)
terraform init
# 2. Format code
terraform fmt -recursive
# 3. Validate syntax
terraform validate
# 4. Plan changes (always review!)
terraform plan -out=tfplan
# 5. Apply changes
terraform apply tfplan
# 6. Verify outputs
terraform output
With Terragrunt:
# Run for single module
terragrunt plan
terragrunt apply
# Run for all modules in directory tree
terragrunt run-all plan
terragrunt run-all apply
When encountering errors:
Read the complete error message - Don't skip details
Check common issues: See references/troubleshooting.md for:
Enable debug logging if needed:
export TF_LOG=DEBUG
export TF_LOG_PATH=terraform-debug.log
terraform plan
# Test specific resource
terraform plan -target=aws_instance.web
terraform apply -target=aws_instance.web
State locked:
# Verify no one else running, then:
terraform force-unlock <lock-id>
Provider cache issues:
rm -rf .terraform
terraform init -upgrade
Module cache issues:
rm -rf .terraform/modules
terraform init
Before committing:
terraform fmt -recursive
terraform validate
tflint --module
checkov -d .
python3 scripts/validate_module.py modules/vpc
terraform-docs markdown modules/vpc > modules/vpc/README.md
Review checklist:
See references/best_practices.md for comprehensive guidelines.
terragrunt-project/
├── terragrunt.hcl # Root config
├── account.hcl # Account-level vars
├── region.hcl # Region-level vars
└── environments/
├── dev/
│ ├── env.hcl # Environment vars
│ └── us-east-1/
│ ├── vpc/
│ │ └── terragrunt.hcl
│ └── eks/
│ └── terragrunt.hcl
└── prod/
└── us-east-1/
├── vpc/
└── eks/
# In eks/terragrunt.hcl
dependency "vpc" {
config_path = "../vpc"
# Mock outputs for plan/validate
mock_outputs = {
vpc_id = "vpc-mock"
subnet_ids = ["subnet-mock"]
}
mock_outputs_allowed_terraform_commands = ["validate", "plan"]
}
inputs = {
vpc_id = dependency.vpc.outputs.vpc_id
subnet_ids = dependency.vpc.outputs.private_subnet_ids
}
See assets/templates/MODULE_TEMPLATE.md for complete Terragrunt configuration templates including:
Comprehensive best practices covering:
Read this when:
Detailed troubleshooting guide for:
Read this when:
Each issue includes:
Cloud cost optimization strategies for Terraform-managed infrastructure:
Read this when:
Ready-to-use CI/CD pipeline templates in assets/workflows/:
Complete GitHub Actions workflow including:
Terragrunt-specific workflow featuring:
GitLab CI/CD pipeline with:
Use these templates as starting points for your CI/CD pipelines. Customize based on your:
Scaffolds a new Terraform module with proper structure and template files.
Usage:
# Create module in current directory
python3 scripts/init_module.py my-vpc
# Create in specific path
python3 scripts/init_module.py my-vpc --path ./modules
# Get JSON output
python3 scripts/init_module.py my-vpc --json
Creates:
main.tf - Resource definitions with TODO placeholdersvariables.tf - Input variables with validation examplesoutputs.tf - Output values with descriptionsversions.tf - Terraform and provider version constraintsREADME.md - Module documentation templateexamples/complete/ - Complete usage exampleUse when:
Comprehensive state inspection and health check.
Usage:
# Basic inspection
python3 scripts/inspect_state.py /path/to/terraform
# Include drift detection
python3 scripts/inspect_state.py /path/to/terraform --check-drift
Provides:
Use when:
Validates Terraform modules against best practices.
Usage:
python3 scripts/validate_module.py /path/to/module
Checks:
Returns:
Use when:
Complete Terraform module template including:
Use this when:
# Initialize
terraform init
terraform init -upgrade # Update providers
# Validate
terraform validate
terraform fmt -recursive
# Plan
terraform plan
terraform plan -out=tfplan
# Apply
terraform apply
terraform apply tfplan
terraform apply -auto-approve # CI/CD only
# State
terraform state list
terraform state show <resource>
terraform state rm <resource>
terraform state mv <old> <new>
# Import
terraform import <resource_address> <resource_id>
# Destroy
terraform destroy
terraform destroy -target=<resource>
# Outputs
terraform output
terraform output <output_name>
# Single module
terragrunt init
terragrunt plan
terragrunt apply
# All modules
terragrunt run-all plan
terragrunt run-all apply
terragrunt run-all destroy
# With specific modules
terragrunt run-all apply --terragrunt-include-dir vpc --terragrunt-include-dir eks
Always:
Never:
Key Principles: