| name | terraform-iac |
| description | Infrastructure as Code with Terraform — providers, modules, state management, workspaces, multi-cloud deployments. Use when working with terraform iac. |
| domain | devops |
| author | oyi77 |
| license | Apache-2.0 |
| subdomain | devops |
| tags | ["ci-cd","devops","iac","infrastructure","terraform"] |
| version | 1.0.0 |
Overview
Terraform enables declarative infrastructure management across AWS, GCP, Azure, and 1000+ providers. Use when provisioning cloud resources, managing multi-cloud infrastructure, implementing GitOps workflows, or enforcing infrastructure standards.
Capabilities
- Define infrastructure in HCL (HashiCorp Configuration Language)
- Manage state with remote backends (S3, GCS, Terraform Cloud)
- Create reusable modules for standardized infrastructure
- Use workspaces for multi-environment deployments
- Import existing resources and detect drift
- Integrate with CI/CD pipelines
When to Use
Trigger phrases:
-
"terraform iac"
-
"Provisioning new cloud infrastructure"
-
"Migrating from manual to IaC management"
-
"Enforcing infrastructure standards across teams"
-
Provisioning new cloud infrastructure
-
Migrating from manual to IaC management
-
Enforcing infrastructure standards across teams
-
Multi-cloud deployments
-
Detecting configuration drift
When NOT to Use
- Task is outside your authorization scope
- You need to implement controls (use implementing-* skills)
- Task is about analysis, not action (use analyzing-* skills)
- You don't have access to target systems
- Task requires compliance expertise (consult professionals)
- Task is about defense, not offense (use defensive skills)
Pseudo Code
The terraform-iac workflow follows a standard pipeline pattern.
Core flow:
# terraform-iac primary flow
input = prepare(raw_data)
result = process(input, config={cloud, code, deployments, iac, infrastructure})
validate(result)
deliver(result)
Error handling:
on error:
log(error_details)
retry_with_backoff(max=3)
if still_failing: alert_and_escalate()
Basic resource provisioning
# main.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
backend "s3" {
bucket = "my-terraform-state"
key = "prod/terraform.tfstate"
region = "us-east-1"
}
}
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t3.micro"
tags = { Name = "web-server" }
}
Module pattern
# modules/vpc/main.tf
variable "cidr_block" { type = string }
variable "environment" { type = string }
resource "aws_vpc" "main" {
cidr_block = var.cidr_block
tags = { Environment = var.environment }
}
output "vpc_id" { value = aws_vpc.main.id }
# Usage
module "vpc" {
source = "./modules/vpc"
cidr_block = "10.0.0.0/16"
environment = "production"
}
CLI operations
terraform init
terraform plan -out=tfplan
terraform apply tfplan
terraform import aws_instance.web i-1234567890abcdef0
terraform plan -refresh-only
terraform destroy -target=aws_instance.web
Workspace management
terraform workspace new staging
terraform workspace select production
terraform workspace list
resource "aws_instance" "web" {
instance_type = terraform.workspace == "production" ? "t3.large" : "t3.micro"
}
CI/CD integration
- name: Terraform Plan
run: |
terraform init
terraform plan -out=tfplan
- name: Terraform Apply
if: github.ref == 'refs/heads/main'
run: terraform apply -auto-approve tfplan
Common Patterns
Proven patterns for terraform-iac usage.
- Batch processing: Process multiple items in parallel for throughput
- Retry with backoff: Handle transient failures gracefully
- Rate limiting: Respect API limits with configurable delays
- Logging: Structured logging for debugging and audit trails
State management
Remote backend (S3/GCS) + state locking (DynamoDB/Consul)
Never commit .tfstate to git
Use separate state files per environment
Module structure
modules/
vpc/
ecs/
rds/
monitoring/
envs/
dev/main.tf → calls modules with dev variables
staging/
prod/
Security best practices
- Use sensitive = true for secrets
- Store secrets in Vault/Secrets Manager, not in tfvars
- Enable state encryption at rest
- Use least-privilege IAM for Terraform execution
- Review plan output before apply
How to Use
- Define infrastructure as code (Terraform, CloudFormation, Pulumi)
- Review changes through PR process before applying
- Configure monitoring and alerting for critical paths
- Set up secrets management (Vault, AWS Secrets Manager, etc.)
- Document runbooks for deployment, rollback, and incident response
- Test disaster recovery procedures regularly
Red Flags
- Infrastructure changes without review: Unreviewed changes cause outages — use PRs for infra code
- No rollback strategy: Every deployment needs a tested rollback plan before it runs
- Secrets in configuration files: Secrets in YAML/JSON get committed to version control
- Missing monitoring and alerting: Without monitoring, outages go undetected until users report them
- No documentation for runbooks: Without runbooks, on-call engineers waste time re-discovering procedures
Verification
Process
- Analyze the task requirements
- Apply domain expertise
- Verify output quality
Anti-Rationalization Table
| Rationalization | Reality |
|---|
| "Manual deployments are fine" | Manual deployments are error-prone and不可 repeatable. Automate. |
| "We do not need monitoring" | Without monitoring, you are flying blind. Add observability from day one. |
| "Infrastructure as code is overkill" | IaC enables reproducibility, version control, and disaster recovery. |