I’m undertaking a refactor where I’m splitting up a large, monolithic root module into multiple smaller root modules. To achieve this without destroying and recreating all of our infrastructure, which would incur downtime we’d rather avoid, it seems like this is going to require a LOT of
terraform state rm-ing from the source module and
terraform import-ing into the destination module(s). I haven’t counted, but I suspect I have hundreds, possibly 1000+, resources to move.
It seems like I should be able to write a script to do this, except that
terraform import seems really hard to script, because the key that you need to import things differs from resource to resource, and can’t be determined programatically. For AWS, some things want an ARN, some things want an ID, others want some combination of attributes joined with underscores. I filed Feature request: All resources should export their import key as a computed attribute · Issue #29666 · hashicorp/terraform · GitHub thinking that it would help others trying to do something like this in the future.
I’m starting to consider working directly on the state file itself. If I can use
jq or something to grab the relevant pieces of the state directly from the source state file to build the dest state file, then upload it to my remote state backend with
terraform state push, that might be easier than doing all of these fiddly imports.
Is there a different way that I could tackle this that I’m not seeing?