State & Backends
Understand Terraform state, remote backends, locking, and how to keep it safe.
Terraform’s state maps your configuration to real resources and stores metadata needed to plan changes. Without it, Terraform can’t tell what already exists or what to update.
By default state is a local terraform.tfstate file — fine solo, unsafe for teams.
A remote backend (S3, Azure Storage, GCS, Terraform Cloud) stores it centrally
with locking so concurrent applies don’t corrupt it. State can hold secrets,
so it must be encrypted and access-controlled — and never committed to git.
terraform {
backend "s3" {
bucket = "my-tfstate"
key = "prod/terraform.tfstate"
region = "eu-west-1"
dynamodb_table = "tf-locks" # locking
encrypt = true
}
}terraform state list # what Terraform manages
terraform state show aws_s3_bucket.data
terraform import aws_s3_bucket.data my-existing-bucket - Explain what Terraform state is and why it’s necessary.
- Describe two risks of keeping state local for a team.
- Configure a remote backend with locking.
- Use
terraform import(in words) to bring an existing resource under management.
Cheat Sheet▾
| Concept | Detail |
|---|---|
| State | Maps config → real resources |
| Local state | terraform.tfstate (solo) |
| Remote backend | S3 / Azure / GCS / TF Cloud |
| Locking | Prevents concurrent corruption |
| Secrets risk | State may hold secrets |
state list/show | Inspect state |
import | Adopt existing resources |
Common Interview Questions▾
What is Terraform state and why does it matter?
A record mapping your configuration to real resources plus metadata for planning. Without it Terraform can’t determine what exists or what to change.
Why use a remote backend with locking?
So a team shares one authoritative state, and locking prevents two simultaneous applies from racing and corrupting it. Remote storage also keeps secrets off laptops.
How do you bring an existing resource under Terraform management?
terraform import <address> <id> records it in state, then you write matching
configuration so future plans show no drift.