Now that we have gained an understanding about what Terraform State is how do we manipulate it, and when should we? In most scenarios it is actually not recommended to manipulate our state in any way as that is the responsibility of our Terraform code. However, in some situations we don’t have a choice, or perhaps we want to test a particular scenario that’s when state manipulation comes into play.
Let’s dive into some scenarios and then how we would resolve them:
As an example we are going to import the above virtual network into our state file.
As you can see from the above the import process is pretty simple, we simply need to pass in the address in Terraform state and the ID that is used for the resource. This will be different between providers.
In this instance we want to force our state to go and check what the real state of our resources are and update state with any changes. This can be done with a very simple command.
In this post we went through the basics of working with Terraform state. How to taint and untaint a resource, how to move a resource to a new address within state, importing existing resources into state and refreshing state from the resource APIs. This really just touches the surface of working with state but in reality it is also about as much as you should be doing in state for 95% of cases. As a general rule of thumb if you’re modifying a state file by hand you’re doing something wrong.