403 β€” Managing Drift

Advanced

Detect and fix discrepancies between your Terraform code, state, and actual infrastructure.

Learning Objectives

1
Understand what drift is and why it happens
2
Detect drift using terraform plan
3
Remediate drift in different ways
4
Prevent drift with processes and tools
Step 1

Set up a baseline configuration

Create resources that we'll intentionally drift.

Commands to Run

mkdir -p ~/terraform-practice/lesson-403
cd ~/terraform-practice/lesson-403
cat > main.tf << 'EOF'
terraform {
  required_providers {
    local = {
      source  = "hashicorp/local"
      version = "~> 2.4"
    }
  }
}

resource "local_file" "app_config" {
  filename = "${path.module}/app.conf"
  content  = <<-EOT
    server_name = production
    port = 8080
    debug = false
    max_connections = 100
  EOT
}

resource "local_file" "db_config" {
  filename = "${path.module}/database.conf"
  content  = <<-EOT
    host = db.example.com
    port = 5432
    pool_size = 10
  EOT
}
EOF

What This Does

We're creating two configuration files. In real scenarios, these would be cloud resources like VMs, databases, or security groups.

Expected Outcome

main.tf with two local_file resources.

Pro Tips

  • 1
    Drift is common in cloud resources modified via console
Was this step helpful?

All Steps (0 / 8 completed)