Move items between terraform states

Terraform states can become rather complex if all our .tf files live in the same directory. We can avoid that if we reorganize our code in separate folders.

If we move code from one folder to another we have to clean up the corresponding state files as well. One way to do it is to simply run terraform apply from both directories. After that we will have 2 clean state files after that. However the downside is that parts of our infrastructure will get destroyed and rebuilt. This is not what we want to do on a live infrastructure.

There is a cleaner way to do this. After we have transferred our code we move the items from one state to another with the following command:

terraform state mv -state-out=${PATH_TO_TARGET_TFSTATE} ${SOURCE_ITEM} ${TARGET_ITEM}