| name | terraform-workflow |
| description | Terraform infrastructure as code workflows and best practices |
| homepage | https://developer.hashicorp.com/terraform/docs |
| metadata | {"emoji":"🏗️","version":"1.0.0","author":"Gourav Shah","license":"MIT","requires":{"bins":["terraform"]},"tags":["terraform","iac","infrastructure","devops"]} |
Terraform Workflow
Infrastructure as Code practices with Terraform.
When to Use This Skill
Use this skill when:
- Managing infrastructure with Terraform
- Reviewing terraform plans
- Debugging state issues
- Following IaC best practices
Basic Workflow
Initialize
terraform init
terraform init -upgrade
terraform init -reconfigure
Plan
terraform plan
terraform plan -out=tfplan
terraform plan -target=aws_instance.example
terraform plan -destroy
Apply
terraform apply
terraform apply tfplan
terraform apply -auto-approve
terraform apply -target=aws_instance.example
Destroy
terraform plan -destroy
terraform destroy
terraform destroy -target=aws_instance.example
State Management
View State
terraform state list
terraform state show aws_instance.example
terraform show
State Operations
terraform state mv aws_instance.old aws_instance.new
terraform state rm aws_instance.example
terraform import aws_instance.example i-1234567890abcdef0
terraform state pull > terraform.tfstate.backup
State Locking
terraform force-unlock LOCK_ID
Workspaces
terraform workspace list
terraform workspace new staging
terraform workspace select production
terraform workspace show
Validation & Formatting
terraform validate
terraform fmt
terraform fmt -check
terraform fmt -recursive
Output & Variables
View Outputs
terraform output
terraform output instance_ip
terraform output -json
Variable Files
terraform plan -var-file=production.tfvars
terraform plan -var="instance_type=t3.large"
Debugging
Verbose Logging
export TF_LOG=DEBUG
terraform plan
export TF_LOG_PATH=terraform.log
terraform plan
unset TF_LOG TF_LOG_PATH
Common Issues
| Issue | Cause | Fix |
|---|
| State lock | Concurrent access | terraform force-unlock |
| Provider error | Version mismatch | terraform init -upgrade |
| Resource drift | Manual changes | terraform refresh then plan |
| Cycle error | Circular dependency | Break dependency with depends_on |
Refresh State
terraform refresh
terraform plan -refresh-only
Safe Practices
Plan Review Checklist
- Check the summary: How many add/change/destroy?
- Review destroys: Any unexpected deletions?
- Check sensitive changes: IAM, security groups, encryption
- Validate resource names: Especially for stateful resources
- Look for force replacements:
# forces replacement
Safe Apply Workflow
terraform plan -out=tfplan
terraform show tfplan
terraform apply tfplan
terraform show
Prevent Accidental Destroys
# In your terraform config
resource "aws_instance" "critical" {
# ...
lifecycle {
prevent_destroy = true
}
}
Module Management
terraform get
terraform get -update
terraform providers
CI/CD Integration
GitHub Actions Example
- name: Terraform Plan
run: |
terraform init
terraform plan -out=tfplan -no-color
- name: Terraform Apply
if: github.ref == 'refs/heads/main'
run: terraform apply -auto-approve tfplan
Plan Output for PR
terraform plan -no-color > plan.txt 2>&1
Cost Estimation
infracost breakdown --path .
infracost diff --path .
Security Scanning
tfsec .
checkov -d .
trivy config .
Quick Reference
terraform init && terraform plan -out=tfplan && terraform apply tfplan
terraform plan -destroy | grep "will be destroyed"
terraform state list
terraform import aws_instance.name i-1234567890
terraform taint aws_instance.example
terraform untaint aws_instance.example
Related Skills
- aws-ops: For AWS resource verification
- git-workflow: For IaC version control
- cost-optimization: For infrastructure costs