Full lesson
Explore the full explanation, examples, and visuals at your own pace.
New laptop, fresh clone
A fresh clone plans to add all twelve resources, even though they're already running. You clone the repo, run init, then plan, and Terraform says twelve to add, nothing to change. Nothing is wrong with your servers. What's missing is the state file, so Terraform thinks none of it exists.
$ git clone git@github.com:acme/infra.git
$ cd infra && terraform init
$ terraform planPlan: 12 to add, 0 to change, 0 to destroy.
Git carried the code, not the state
Here's what happened. The old laptop pushed main.tf to the git repo, and the new laptop cloned it. But with the default local backend, terraform.tfstate was written only on the old laptop, and it's usually git-ignored. So the code made the trip. The state file never did.
State maps code to real resource IDs
State is a lookup table. For the old laptop, each address in your config, like aws_instance.web, maps to a real ID, such as i-0abc123, and the bucket maps to acme-logs. On the new laptop, the same addresses exist, but the ID column is empty. Terraform can't recognise its own servers.
Empty state, so create everything
On the new laptop, Terraform reads the local state and gets back zero resources. The config declares twelve, so plan proposes twelve creates. Apply then asks AWS to create acme-logs, and AWS answers that the bucket already exists. That's the error you saw. Resources without unique names, like EC2 instances, may not fail at all. They can quietly duplicate.
backend.tf
The fix is a backend block. The bucket, acme-tf-state, holds the state file, and the key sets its path inside that bucket. Region and encrypt cover location and encryption at rest. The dynamodb_table gives you locking. On Terraform 1.10 and newer, S3-native locking with use_lockfile can replace that table.
terraform {
backend "s3" {
bucket = "acme-tf-state"
key = "prod/terraform.tfstate"
region = "us-east-1"
dynamodb_table = "tf-locks"
encrypt = true
}
}Move state once, then share it
Order matters here. On the old laptop, the one holding the real state, run terraform init with migrate-state, which copies that local state into S3. Then on the new laptop, pull the code, run init, and plan. It now reads the shared state, so it reports no changes.
# old laptop (has the real state)
$ terraform init -migrate-state
# new laptop
$ git pull && terraform init
$ terraform planNo changes. Your infrastructure matches the configuration.
One state, read by every machine
Now the picture is complete. Git still carries main.tf from the old laptop to the new one through a push and a clone. The state no longer travels that way. The old laptop migrates it into S3, and the new laptop reads from there, so every machine, and CI runners too, sees the same resource IDs.
Locking stops two applies colliding
Once state is shared, two machines could apply at the same moment and overwrite each other's changes. The lock prevents that. The old laptop acquires a lock in the tf-locks table, and when the new laptop asks, it's told the state is already locked. The old laptop writes the new state to S3, then releases the lock. After that, the new laptop can retry.
Keeping state safe
Three habits keep state safe. Don't commit terraform.tfstate to git, because it can hold secrets in plain text. Turn on S3 versioning so you can roll back a corrupted state. And if state is truly lost, terraform import re-binds existing resources instead of recreating them.
- Never commit terraform.tfstate; it holds secrets
- Enable S3 versioning to roll back state
- Old state lost? Use terraform import
Terraform only knows servers listed in its state file
Terraform only knows the servers listed in its state file. Code travels through git, but state doesn't, so a new laptop sees nothing. Put state in a shared remote backend like S3, and every machine plans against the same record. Set that up before a second machine ever runs plan.