Back when our project was using Terraform 11.x, our module design was very linear.
For instance, every time we assigned a new IAM role, we would declare a new module to do so, and we had a resource defined that would provision 1 IAM role.
Our end result was that we now have N amount of modules for N amount of role assignments, e.g
module "iam_user1" {
// stuff to do first role assignment
}
module "iam_user2" {
// stuff to do first role assignment
}
module "iam_user3" {
// stuff to do first role assignment
}
// and so and so forth...
Now that we have switched over to Terraform 12, we want to delete the old module declarations, and create a module that will call a resource which will utilize the for_each
meta-argument, and create however many IAM roles needed with one module.
module "iam_all_users" {
// pass in a map(map(string)) and have for_each do its thing
}
I’ve basically implemented the second variant (using for_each), but a new problem has arisen:
The problem:
When running terraform plans, it’ll show the new module that employs for_each
is going to create however many resources, and that’s great. I’ve simply just moved the IAM declaration from a bunch of modules to one.
The problem is that once I delete the old modules from the terraform source code, the plan will now show that it also wants to delete all of the old modules in the plan, which if I’m not mistaken, is going to essentially end up being a destroy followed by a re-create.
Basically, my concern was the nature of the destroy / recreation when re-writing modules in this way. I’d like to do this for many types of resources (not just IAM but also things like instances, disks, and so on and so forth), and can see some complications if everything ends up getting destroyed and then re-created.
Is there some way to rewrite modules in a way similar to how I’ve described above but not have to re-create everything, or is the nature of this kind of re-write intended to produce this kind of consequence?