Drift Detection, Brownfield Import & State Migration
Master Terraform drift detection, brownfield cloud adoption, modern declarative import blocks, and zero-downtime state refactoring.
Drift Detection, Brownfield Import & State Migration
Section titled “Drift Detection, Brownfield Import & State Migration”In a perfect world, all cloud infrastructure is created by Terraform from day one. In reality, engineers face brownfield environments: manually created legacy resources, emergency hotfixes applied in the AWS console, and the dreaded Configuration Drift.
1. What is Configuration Drift & How to Stop It
Section titled “1. What is Configuration Drift & How to Stop It”Drift occurs when the physical cloud state diverges from your recorded code. Common culprits include:
- A junior admin manually opening port
22in an AWS Security Group. - AWS Auto Scaling adding or removing instances out-of-band.
- Console-based emergency patches applied during a midnight production outage.
Declared HCL Code (Port 443 only) <--- MISMATCH ---> Live AWS Security Group (Port 443 + Port 22)Automated Drift Detection Pipeline
Section titled “Automated Drift Detection Pipeline”To stop drift before it causes security vulnerabilities, schedule a daily, non-blocking CI/CD job:
# Returns exit code 2 if drift is detected without modifying live assetsterraform plan -detailed-exitcode -no-colorIf the command exits with 2, alert your on-call Slack channel immediately with the exact diff!
2. Brownfield Adoption: Modern import Blocks (Terraform 1.5+)
Section titled “2. Brownfield Adoption: Modern import Blocks (Terraform 1.5+)”Historically, importing resources required manual CLI commands (terraform import aws_s3_bucket.data my-bucket). Starting in Terraform 1.5+, import is declarative, reviewable, and generates code automatically!
Step 1: Write the Declarative Import Block
Section titled “Step 1: Write the Declarative Import Block”import { to = aws_s3_bucket.legacy_archive id = "corporate-legacy-data-archive-2022"}Step 2: Auto-Generate HCL Code
Section titled “Step 2: Auto-Generate HCL Code”Run the plan command with code generation enabled:
terraform plan -generate-config-out=generated_resources.tfTerraform automatically inspects the live AWS bucket and outputs clean, idiomatic HCL directly into generated_resources.tf. Review the code, submit a Pull Request, and your brownfield asset is officially under version control!
3. Zero-Downtime State Refactoring (state mv)
Section titled “3. Zero-Downtime State Refactoring (state mv)”What happens when you refactor your code to move standalone resources into reusable modules?
If you simply change the code, Terraform thinks you want to destroy the old resource and create a brand-new one. To prevent production destruction, use terraform state mv (or declarative moved blocks):
# Modern Declarative Refactoring in HCL:moved { from = aws_security_group.legacy_sg to = module.networking.aws_security_group.web_sg}When you run terraform plan, Terraform recognizes that only the internal state address changed—zero cloud resources are touched, zero downtime occurs!