Skip to content

Instantly share code, notes, and snippets.

@yardensachs
Last active October 9, 2018 20:00
Show Gist options
  • Save yardensachs/27c06ae26a890bde042b1e78cb4bdecb to your computer and use it in GitHub Desktop.
Save yardensachs/27c06ae26a890bde042b1e78cb4bdecb to your computer and use it in GitHub Desktop.
Terraform state moving script

Terraform state moving script

This script will use a mapping of old and new Terraform resources path to move from an exisiting state to a new state. This is handy when restructuring a Terraform project.

Steps before running this script:

  • In exisiting project:
    • Make sure you have intiiated with the correct state backend (if you are using different state backends)
    • Run terraform state list and grab the paths. you would need to filter between resource paths and other paths like data blocks
  • In new structure:
    • Run terraform init
    • Run terraform plan
    • Copy all the new resources paths that would be created (starts with the "+")
  • Map between exisiting project paths and new project paths
  • Add the mapping to this script
"""
Requirements: python sh
# Terraform state moving script
This script will use a mapping of old and new Terraform resources path to move from
an exisiting state to a new state. This is handy when restructuring a Terraform project.
Steps before running this script:
- In exisiting project:
- Make sure you have intiiated with the correct state backend (if you are using different state backends)
- Run `terraform state list` and grab the paths. you would need to filter between resource paths and other paths like data blocks
- In new structure:
- Run `terraform init`
- Run `terraform plan`
- Copy all the new resources paths that would be created (starts with the "+")
- Map between exisiting project paths and new project paths
- Add the mapping to this script
"""
from tempfile import NamedTemporaryFile
from sh import terraform
# Map old path to new path
mapping = [
('<old target path>', '<new target path>'),
]
# Get the JSON data returned by the `terraform state pull` command
# Represting the current project state
full_state_data = terraform.state('pull')
# Create a temporary file with the current state
source_temp_file = NamedTemporaryFile(mode='w')
source_temp_file.file.write(str(full_state_data))
source_temp_file.file.close()
# Create an empty temporary file for the new state
target_temp_file = NamedTemporaryFile()
# Loop through the resource mapping and move one by one
for source, target in mapping:
print(source, target)
# Run `terraform state mv`. moving from current state temp file to new state temp file
resp = terraform.state(
'mv',
'-state-out',
target_temp_file.name,
'-state',
source_temp_file.name,
source,
target,
)
print(resp)
print('-' * 100)
# Print the copy command form the temp file to whereever you want the new state file to be in
print('cp', target_temp_file.name, 'terraform.tfstate')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment