← Lessons/State — Break It & Recover
← State — Break It & Recover

Recover with import

Learn

Recovering with import

The resources still exist, so the fix is to put Terraform's memory back, not to build anything. Import takes an address in your code and a real object, and records the mapping in state without changing the object.

In a real project it looks like this, with the real id of the existing resource:

terraform import demo_server.web demo_server-ea9af2dc

(or, in Terraform 1.5+, an import block in the code, which is planned and reviewed like any other change).

Import every resource back, then plan:

No changes. Your infrastructure matches the configuration.

The mapping is restored, the code already matched the real objects, and nothing gets created. No changes after an import is the proof you got every address and id right.

Don't apply — that would create duplicates. Instead import the real resources back into state. Import demo_network.main, then change the address field to demo_server.web and import it too. Now run plan: with the mapping restored, there are no changes. That's the correct recovery.

Loading...

State Inspector

State

Terraform's memory

empty — nothing here yet

Real Infrastructure

the actual world

empty — nothing here yet

Break it & recover

These act outside Terraform — exactly how state gets into trouble in real life.