Skip to content

Instantly share code, notes, and snippets.

@bassmanitram
Created November 11, 2022 12:56
Show Gist options
  • Save bassmanitram/19d797e6e9f943bfb946e7381de935e5 to your computer and use it in GitHub Desktop.
Save bassmanitram/19d797e6e9f943bfb946e7381de935e5 to your computer and use it in GitHub Desktop.
How to make Terraform 0.13 properly update a Terraform 0.12 state
/*
* Put this in a place where node can find it (referenced by $SCRIPT_HOME in the driver script)
* or figure out how to do this with JQ :)
*
* Basically, for every key that starts with "registry.terraform.io/hashicorp/" duplicate its value to
* that same key with "hashicorp" translated to "-" (keep the original property too)
*/
const fs = require("fs")
const output ={};
Object.entries(JSON.parse(fs.readFileSync( "/dev/stdin" ))).forEach(([key,value]) => {
output[key] = value;
if (key.startsWith("registry.terraform.io/hashicorp/")) {
output[key.replace("hashicorp","-")] = value;
}
});
console.log(JSON.stringify(output))
#
# Perform whatever TF init you need to do, then run this script in the folder
# where the .terraform flder is situated
#
# This first one copies all the installed "hashicorp" plugins to the "-" namespace
#
cp -r .terraform/plugins/registry.terraform.io/hashicorp .terraform/plugins/registry.terraform.io/-
#
# The hack the "selections" file
#
cat .terraform/plugins/selections.json | node $SCRIPT_HOME/hack-selections.js > .terraform/plugins/selections.json.hacked
#
# Then activate the hacked version
#
mv .terraform/plugins/selections.json.hacked .terraform/plugins/selections.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment