DevOps & Kubernetes (50 Qs)•Part 5: Infrastructure as Code, SRE Observability & Production Troubleshooting (43 - 50)
Terraform Modules, Multi-Environment Architecture, and Automated Configuration Drift Detection.
Scaling Terraform in Enterprise Organizations
As infrastructure expands, monolithic Terraform codebases become slow and risky. Platform engineering teams structure infrastructure using reusable Modules and isolated environment folders.
php
Root Repository Directory Structure:
├── modules/
│ ├── vpc/ # Reusable module
│ └── eks_cluster/ # Reusable module
└── environments/
├── dev/ # Isolated backend & state
│ └── main.tf # Calls modules with dev variables
└── prod/ # Isolated backend & state (Requires separate IAM role)
└── main.tf # Calls modules with prod variables
Detecting Configuration Drift
Drift occurs when engineers manually tweak cloud settings in the AWS/GCP Console without updating Terraform code.
bash
# Run hourly in CI to catch out-of-band manual console edits:
# -detailed-exitcode returns exit code 2 if drift is detected
terraform plan -detailed-exitcode -no-color || exit_code=$?
if [ $exit_code -eq 2 ]; then
echo "CRITICAL: Infrastructure drift detected between code and cloud!"
# Trigger Slack notification or automatically create Jira ticket
fi
Importing Existing Cloud Resources
When inheriting unmanaged legacy infrastructure:
hcl
# Modern Terraform 1.5+ declarative import block
import {
to = aws_s3_bucket.legacy_data
id = "my-existing-production-bucket-name"
}