Skip to content

Instantly share code, notes, and snippets.

@GusAntoniassi
Created October 6, 2020 21:16
Show Gist options
  • Save GusAntoniassi/2c3687ab190965e2da5ec57a7a394fdf to your computer and use it in GitHub Desktop.
Save GusAntoniassi/2c3687ab190965e2da5ec57a7a394fdf to your computer and use it in GitHub Desktop.
Terraform external data provider - Shell script with input without jq

This is an example workaround for getting Terraform input variables when you won't have jq available in the executing machine.

Uses sed to parse the input JSON to extract a certain key from the input JSON.

Warning: this hasn't really been well-tested, it will pretty much work only with string values and will likely break for strings that have quotes. Might give you an idea of how to improve it for your implementation though.

data "external" "foobar" {
program = [
"bash",
"${path.module}/script.sh.sh"
]
query = {
my_terraform_variable = "Hello, world!"
}
}
#!/bin/bash
# Gets input from stdin, store it in a variable to allow it to be used multiple times
input=$(cat)
# Parse the input JSON with sed to find the value for the terraform variable key
MY_BASH_VARIABLE="$(sed -r -e 's|^.*"my_terraform_variable":"(.*)?".*?$|\1|g' <<< $input)"
# You can now use the value in your script
echo "{\"value\": \"$MY_BASH_VARIABLE\"}"
@nielsbaltodanomatrix
Copy link

eval "$(jq -r '@sh "MY_BASH_VARIABLE=\(.my_bash_variable)"')" echo "MY_BASH_VARIABLE is ${MY_BASH_VARIABLE}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment