cd ..
TERRAFORM

Terraform state - the file that can take down your infra

The terraform.tfstate is the map between your code and the real world. Lose the state, lose control of your infrastructure.

What the state stores

Every resource created, its real IDs in the cloud, dependencies, and metadata. This is how Terraform knows what needs to be created, changed, or destroyed in a plan.

Why local state is dangerous

Remote Backend with S3

terraform {
  backend "s3" {
    bucket       = "meu-terraform-state"
    key          = "prod/network.tfstate"
    region       = "us-east-1"
    encrypt      = true
    use_lockfile = true
  }
}

After configuring:

terraform init -migrate-state
terraform plan

Golden rules

  1. State always remote, versioned, and encrypted
  2. Never edit the state manually — use terraform state mv and similar commands
  3. One state per environment (dev/staging/prod separated)

References

What did you think?