So. The scenario is that we want to change the names of the resources in Terraform. Nothing major, just some clean up/consistency issues.
Using terraform state mv
we can rename the resources, run the plan and see no changes. We are happy.
This scenario works perfectly if you are the only developer making changes or every change is direct to the main branch in version control.
What is a more realistic scenario is that one of the developers has been tasked with making the changes, so they have a feature branch in the version control. They cannot (I hope obviously) make changes to the tfstate as that would be inconsistent with the main branches code which may be needed for a release at any time.
In other projects, the mechanism to handle this scenario is called ‘migrations’. This is where the developer can describe what they want to change (and how to reverse it) as part of their branch. The migration system looks after making sure that all changes are executed in a consistent order, etc. (traditionally used for amending a database, but, in theory, can be used for anything).
Terraform migrations would be a really nice feature to help migrate the state for a branch, so prior to executing the plan, all outstanding migrations are run. In the event of a failure, the migrations are “rolled back” (hence the need to either have a mechanism of describing how to reverse a migration or a structure for reversible migrations).
In the projects where we’ve had migrations, a branch to be merged may only contain migrations that need to be run AFTER a deployment. We found that simple blocking tasks in project management dealt with this far easier that attempting to describe ‘before’ and ‘after’ migrations (though, of course, if there is an easy pattern for this, then fine).
Another possibility for this COULD be to have a non applying change to the state purely for planning.
So, the goal is, in a branch, a develop can rename the resources, run the plan and see the plan result to no changes (so indicating the rename is correctly done and resources are not being deleted/created because of a new name), but the rename hasn’t been applied to the live state (so the main branch will still see a fully valid state as it applies to the current resources and the .tf code in the main branch).
Think of it as a try-before-you-buy thing. Modify state, plan, see the results, throw it all away if not happy.
Basically, any ideas would be really appreciated.