Terraform is a safe and efficient open-source tool for building, changing, and versioning infrastructure safely and efficiently.
10 common gotchas in terraform
State File Is Critical
- Losing or corrupting your
terraform.tfstatefile can break your infrastructure management.
- Losing or corrupting your
always back up your state file
terraform { backend “s3” { bucket = “my-tf-state” key = “state.tfstate” region = “us-east-1” } }
| |
Provider Version Pinning
- Not pinning provider versions can lead to breaking changes when providers update.
1 2 3 4 5 6 7 8terraform { required_providers { aws = { source = "hashicorp/aws" version = "~> 5.0" } } }Sensitive Data in State
- Secrets and sensitive data may be stored in plain text in state files.
1resource "aws_db_instance" "example" {
…
password = var.db_password # This will be stored in state
}
| |
Implicit Dependencies
- Terraform may not know the correct order for resources unless you use
depends_on.
1 2resource "aws_instance" "app" { depends_on = [aws_db_instance.example]- Terraform may not know the correct order for resources unless you use
…
}
| |
Variable Defaults and Overrides
- Be careful with variable precedence; defaults can be overridden in unexpected ways.
1 2 3variable "region" { default = "us-east-1" }
can be overridden by cli: terraform apply -var=“region=us-west-2”
| |
Resource Addressing
- Changing resource addresses (e.g., renaming modules) can orphan resources or cause errors.
renaming module or resource can break state association
use ’terraform state mv’ to move resources safely
```
These should be kept in mind to avoid common pitfalls when working with Terraform!