Skip to content

Instantly share code, notes, and snippets.

@StephenKing
Created May 8, 2020 08:30
Show Gist options
  • Save StephenKing/7b57742e611758008f14243b30acb3d4 to your computer and use it in GitHub Desktop.
Save StephenKing/7b57742e611758008f14243b30acb3d4 to your computer and use it in GitHub Desktop.
Terraform 0.12 syntax migration
#!/bin/bash
#
# Replaces certain (simpler) occurences of Interpolation-only expressions,
# which are deprecated in Terraform 0.12
#
# works on MacOS sed, no clue if I have GNU sed or BSD sed.. it doesn't want to tell me...
SED_CMD="sed -i.bak -E "
for file in $(find . -name *.tf -not -path "*.terraform/*"); do
echo $file
# var.name
$SED_CMD 's/"\$\{(var\.[0-9a-zA-Z_-]+)\}"/\1/g' $file
# var.name["index"]
$SED_CMD 's/"\$\{(var\.[0-9a-zA-Z_-]+\["[0-9a-zA-Z_-]+"\])\}"/\1/g' $file
# local.name
$SED_CMD 's/"\$\{(local\.[0-9a-zA-Z_-]+)\}"/\1/g' $file
# local.name["index"]
$SED_CMD 's/"\$\{(local\.[0-9a-zA-Z_-]+\["[0-9a-zA-Z_-]+"\])\}"/\1/g' $file
# data.type.name.attribute
$SED_CMD 's/"\$\{(data\.[0-9a-zA-Z_-]+\.[0-9a-zA-Z_-]+\.[0-9a-zA-Z_-]+)\}"/\1/g' $file
# data.type.name.*.attribute
$SED_CMD 's/"\$\{(data\.[0-9a-zA-Z_-]+\.[0-9a-zA-Z_-]+\.\*\.[0-9a-zA-Z_-]+)\}"/\1/g' $file
# module.name.attribute
$SED_CMD 's/"\$\{(module\.[0-9a-zA-Z_-]+\.[0-9a-zA-Z_-]+)\}"/\1/g' $file
# some_resource.name.attribute
$SED_CMD 's/"\$\{([0-9a-zA-Z_-]+\.[0-9a-zA-Z_-]+\.[0-9a-zA-Z_-]+)\}"/\1/g' $file
# function(some.thing).. a never ending story..
# $SED_CMD 's/"\$\{([a-z]+\([0-9a-zA-Z_-]+(\.[0-9a-zA-Z_-]+)+\))\}"/\1/g' $file
# echo Continue?
# read
done
find . -name *.bak | xargs rm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment