-
Notifications
You must be signed in to change notification settings - Fork 9.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Track resource renaming through configuration #9048
Comments
Hi @BogdanSorlea! Thanks for this feature request. Do you think you could achieve some of what you're asking here using the The workflow in this case would be:
The main difference between this and what you are describing, I think, is that using |
Hey, thanks, stupid of me, I wasn't aware of the state move command, I will try it out and let you know. |
|
The problem with this flow is that I would have to run the For a flow that involves review by a second engineer, I think it would be very useful to have something like
|
I think this is a really important feature. Refactoring the terraform resource names is a pretty common things, and it really needs to work with CI and source control. Other approaches might be:
Terraform should then upgrade any oldnames to the newname. Another option could be for terraform to attempt to detect renames, by having the developer mark an output variable as a 'resource identifier'. e.g. suppose you have a resource that creates an s3 bucket with a specific name and path. During plan time, terraform could look for any deleted resources with that arn, and if they match that of one being created, it can treat that as a renamed resource |
Just found this ticket after a 2nd search through open issues, it looks like this one even predates #18347 where it was said that Can we get consensus between the contributors or in some other way make a battle plan so that the community can contribute to a renaming feature? |
Thought I had posted it here before, but I started tfmv a couple years ago to try and address this problem. Haven't gotten back to it, but the problem just bit me again. Would love some help on it if there's interest. [email protected] |
I really, really wish Terraform had the second feature @mcintyre321 suggests, where specific attributes of a resource could be flagged as a resource identifier, similar in some ways to a Puppet "namevar", but potentially specified on a per-resource basis by the user. This would not only help with refactoring, it would also potentially allow for automatic importing of existing resources without resorting to |
While I'd really like the feature, and allowing to define an identifier would certainly make this more manageable for moving. |
@jlsjonas For sure, I do agree there should be a confirmation before auto-importing; I only mean "auto" in the sense that you don't have to type potentially hundreds of |
Hi all, Making it so that all side-effects are made using the standard With that said, I can acknowledge that this is something we intend to do eventually but I also want to be clear with you all that it is not going to happen in the near future. I can't predict far into the future with any certainty, but I strongly suspect that it will be at least a year before we'd be able to begin design and implementation work for this huge shift in Terraform's current model of plans, because work to stabilize Terraform's current featureset is taking priority. We don't intend to accept community contributions for the feature because the changes required would be too disruptive to Terraform's internals to happen concurrently with the stabilization work. I understand that this is annoying, and I want to have this new workflow at least as much as you all do, but I also want to be honest about the current situation in case it inspires any of you to seek third-party external solutions in the meantime. |
I've come across https://github.com/minamijoyo/tfmigrate Haven't used it personally but maybe helpful for some people in this thread. |
IIUC, this means that instead of manually renaming and moving resource definitions, we would write a file that represents desired state. The state being, in this case, of the root module. Let's look at a simple use case: I have a root module that is getting large and I want to refactor some of its code into a separate sub-module. To do this manually I use To do this declaratively in a file I would create a file with a "desired state" that lists the resources in a new file in a new subfolder, and the new outputs. A command like Example module-config state file that represents current state:
where the ID is whatever is required to refer to the actual object in terraform state, such as ARN for resources or some unique ID that terraform generates for variables, outputs, data, modules etc. This is to support renaming. The above config state says that the current module consists of 3 files with 2 resources, a data, 2 variables, and 2 outputs, no submodules. There are likely other entities that should appear there, like tfvars file, providers/versions file, backend, etc. Then if you modified that file by moving variable/output/data/resources between files, and into submodules, terraform module-config plan/apply would know what to do. Eg if the above file gets modified to this:
Terraform knows exactly how to refactor everything: what folder to create, what files to put in there, what objects to move, and can rename all references to variables and resources. Interestingly, this could also be used to import resources, or to capture the configuration of resources:
which would tell terraform to ensure that main.tf has the said resources and that the state file manages them. In any case such module-config declaration of state should only be necessary when modifying existing state, so the "current" state should be exportable, eg It would also be straightforward to have chains of migrations: eg if you have migration1.tf, migration2.tf, migration3.tf. It is sufficient for 3 to mark 2 as predecessor, and 2 to mark 1 as predecessor, and 1 to have nil predecessor (mean the initial module config is its predecessor), and for the state file to track what is the latest migration that successfully completed. Then it can tell whether a Not sure yet how to handle partial failure of a module-config apply. This will have moved some resource definitions and renamed some resources in state file, but others will be pending. And the problem might require mods to the migration file. Therefore perhaps module-config apply command can create a temp file similar to the lock file, and keep track of every mod that has succeeded, and deletes it if all succeeded. If partway failure occurs, you can do module-config apply on the previous migration and the apply command can see that it is a revert on an incomplete migration. |
Would it not be great if we could use a workflow similar to resource "example" "test" {
name = "Test149"
feature_x = "enabled"
lifecycle {
state_mv {
previous_state = example.previous_test
}
}
} A similar approach would be amazing to have for resource "example" "test" {
name = "Test149"
feature_x = "enabled"
lifecycle {
import {
id = "Cloudx/Test149"
}
}
} This would save us a lot of scripts and/or manual work if naming is refactored.. |
Thanks for pointing out this issue too, @mwarkentin! 😀 I didn't notice until now that we had this earlier request that #19354 later duplicated. I just replied over there and closed that one and what I said over there applies here too, in particular that if you have feedback on the new feature please create new issues for each bug report or feedback item rather than adding more comments here, because we'll be using issues to track work during the v1.1 beta period. Thanks! |
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. |
Hey,
Although this idea might get refuted, I think it could ease the pain of refactoring quite a lot. Example...
Let's say I have a terraform-based setup with two instances (does not really matter how many) and some SGs created, all located in the same folder. Thus, let's say that my SG, whose "name" (terraform resource name) is
integration
, SG which is attached to both instances. Therefore, when specifying the SG ID to associate to the instances, we reference it by${aws_security_group.integration.id}
.Now let's say we move the SG declaration to a separate module,
security-groups
, which is located outside the working directory and is referenced accordingly. Inside this module, we define the outputBecause of this change, the SG ID associated to the instances needs to change to
${module.security-groups.integration_sg_id}
. Now, assuming that before the refactoring we have actually spawned these resources, just applying the new manifest will result in errors, requiring to destroy and re-create the stack.For "large-scale" deployments, destroying and recreating is not always an option, so I am suggesting to somehow allow the defining of some resource equivalence maps, i.e. a file where you could say something like
Of course, there would be something related to scope that would have to be figured out - and, of course, it is very much a bit of a hairy problem, but I can assure you that at least for our setup it is very crucial.
If applied, this mechanism should probably be envisioned as a database incremental migration routine: the state file should keep track of all the equivalence definitions that have already been applied, add a new equivalence resource to the state file after it has been successfully applied and update the structure/values of/in the state file to reflect the post-apply state.
Please let me know if there's anything that is not very clear.
The text was updated successfully, but these errors were encountered: