Hound
Hound

Reputation: 947

Difference between terraform import and terraform state mv

I have recently started working on Terraform, have a question on terraform state mv and terraform import. As per the documentation, terraform state mv can be used when a resource name changes, and the updated name has to be added to the state file. And terraform import can be used to import the resources created outside of Terraform to a state file. My question is even when a resource name changes or code structure changes(using modules), we can still use terraform import to update the state file correct? Could anyone tell me, what is the real benefit of using terraform state mv command?

Upvotes: 4

Views: 5268

Answers (4)

Daniel Seichter
Daniel Seichter

Reputation: 929

Use terraform import for all resources, created outside terraform

Use terraform state mv in the case, you want to restruct a already exisiting terraform resource.

I am using terraform state mv as soon as my projects needs to be restructed, e.g. become more complex, want to move to modules, etc.

Sometimes (even for older terraform projects), it could also be a good practice to import the resource again (with another name) and to a terraform state rm.

Upvotes: 0

Jay
Jay

Reputation: 335

Terraform Import - Terraform is able to import existing infrastructure. This allows you take resources you've created by some other means and bring it under Terraform management.

Terraform State MV - It is less common situation where you wish to retain an existing remote object but track it as a different resource instance address in Terraform, such as if you have renamed a resource block or you have moved it into a different module in your configuration.

Upvotes: 0

Danny G
Danny G

Reputation: 3799

One benefit of terraform state mv is useful if you need to refactor your code in or out of modules. I've used it quite a bit. I recommend backing up your state before making any changes. If you are using a remote state, you can always take a copy of it, disable your use of the remote state temporarily and then utilize the copy locally.

You can see the names of your state objects by using terraform state list.

The usage of terraform import is to add an existing thing to your state file, so it's tracked.

Upvotes: 5

pijemcolu
pijemcolu

Reputation: 2605

So the question really is this particular case:

I have renamed the TF resource / changed the structure of the resource in IaC. Can I just re-import it into the new structure, instead of moving it?

Yes you can, but what will happen to the state? You'll be importing a resource you're already managing according to the TF state. The old resource that you've modified should still be managed, therefore you might run into issues where the TF operator will attempt to recreate it or even delete it. It will all depend on what state matches the reality in your cloud provider.

If you'd like to still import the updated, I'd go for terraform state rm & terraform import afterwards. This is sometimes required / an easy hack after big changes to a particular module / resource. It's also a good debugging experience, when you're not exactly sure about how does the cloud resource matches the TF code, as you're see state differences only for this newly imported resource.

Upvotes: 2

Related Questions