Skip to content

Instantly share code, notes, and snippets.

@audunsolemdal
Last active October 10, 2023 10:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save audunsolemdal/07bda02616dbd8ceacc23ba55a113da7 to your computer and use it in GitHub Desktop.
Save audunsolemdal/07bda02616dbd8ceacc23ba55a113da7 to your computer and use it in GitHub Desktop.
Terraform - move module contents to root module or to another nested module
#!/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