Thuta Learning
IntermediateDevOpsintermediate

Terraform State

Relax. We'll talk through this in plain words — no textbook voice.

What you'll walk away with

  • Understand Terraform State, no intimidation required
  • Get hands-on running terraform commands and HCL code yourself
  • Be ready to apply this concept in a real project right away

Let's think about this for a second

The state file (`terraform.tfstate`) records every current attribute (ID, IP address, tags) of each resource in JSON format — every time Terraform runs, it diffs the configuration (what you want) against the state (what already exists) to decide what to do. If the state file is lost, Terraform no longer 'knows' those resources exist at all — it might try to create them again (leading to duplicate resources). The `terraform state list` (view the resource list) and `terraform state show <resource>` (view attribute details) commands are useful for inspecting state.

Let's connect this to a real scenario

If two team members (on two separate laptops) each keep the state file only on local disk, one person's apply can leave the other person's state stale (not updated) — leading to conflicts or duplicate resources (which is why production setups use Remote State, covered in a later chapter).

Let's look at an example

bash
# List all resources Terraform is tracking
terraform state list

# Show full detail of one resource
terraform state show aws_instance.web

# See the raw state file (JSON) — read-only inspection
cat terraform.tfstate
You should see
$ terraform state list
aws_instance.web
local_file.greeting

5-minute try-it

If you already applied the local_file resource from the first-resource lesson, run `terraform state list` and `terraform state show local_file.greeting` — read through the attributes in the state yourself.

A quick word of caution

If you delete the state file without a backup (or it gets corrupted), it can become very difficult to manage your production infrastructure again — always use Remote State plus versioning in production.

Easy traps

  • Manually editing the state file directly in a text editor — if the state format breaks, Terraform itself can stop working (you should only use the dedicated `terraform state` commands)
  • Multiple team members manually copying a local state file around via a shared drive/USB — this tends to cause conflicts

Now try it yourself

If you already applied the local_file resource from the first-resource lesson, run `terraform state list` and `terraform state show local_file.greeting` — read through the attributes in the state yourself.

You'll know it worked when: $ terraform state list aws_instance.web local_file.greeting

Terraform State | Thuta Learning