Terraform IaC Builder
Purpose
Provision and manage cloud infrastructure with Terraform using modular, reusable, and safe IaC patterns.
Terraform Best Practices
Module Structure
infrastructure/
├── modules/
│ ├── vpc/
│ │ ├── main.tf
│ │ ├── variables.tf
│ │ └── outputs.tf
│ ├── rds/
│ └── ecs/
├── environments/
│ ├── prod/
│ │ ├── main.tf
│ │ ├── variables.tf
│ │ └── terraform.tfvars
│ └── staging/
└── shared/
└── backend.tf
AWS Example: ECS + RDS
# modules/ecs-service/main.tf
resource "aws_ecs_service" "this" {
name = var.name
cluster = var.cluster_id
task_definition = aws_ecs_task_definition.this.arn
desired_count = var.desired_count
launch_type = "FARGATE"
network_configuration {
subnets = var.private_subnet_ids
security_groups = [aws_security_group.service.id]
assign_public_ip = false
}
load_balancer {
target_group_arn = var.target_group_arn
container_name = var.name
container_port = var.container_port
}
deployment_circuit_breaker {
enable = true
rollback = true
}
lifecycle {
ignore_changes = [desired_count] # Managed by autoscaling
}
}
# Remote state backend
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "prod/infrastructure.tfstate"
region = "us-east-1"
encrypt = true
dynamodb_table = "terraform-state-lock"
}
}
Variables and Outputs
variable "environment" {
type = string
description = "Deployment environment (prod, staging, dev)"
validation {
condition = contains(["prod", "staging", "dev"], var.environment)
error_message = "Environment must be prod, staging, or dev."
}
}
output "database_endpoint" {
value = aws_rds_instance.main.endpoint
sensitive = true
description = "RDS instance endpoint"
}
Outputs
- Module structure for your infrastructure
- Environment-specific configurations
- Remote state configuration
- CI/CD integration for Terraform
- State management strategy
- Cost estimation commands