Last active
February 10, 2025 16:44
-
-
Save audunsolemdal/07bda02616dbd8ceacc23ba55a113da7 to your computer and use it in GitHub Desktop.
Terraform - move module contents to root module or to another nested module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Generates terraform "moved" blocks for files based on terraform state | |
export OLD_MODULE="old_module_name" | |
export NEW_MODULE="" #LEAVE EMPTY to move to root module or enter "module.new_module." if you want to move to another module | |
terraform state list | | |
grep module.${OLD_MODULE} | cut -d. -f 3- | sed 's/\"/\\\"/g' | | |
xargs -I {} sh -c 'echo "moved {" && echo " from = module.${OLD_MODULE}.{}" && echo " to = ${NEW_MODULE}{}" && echo "}"' | sed 's/\]/\"]/g' | sed 's/\[/\["/g' | tr -d '\r' > moved-blocks.tf |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment