Skip to content

Instantly share code, notes, and snippets.

@doytsujin
Forked from GusAntoniassi/README.md
Created March 13, 2024 19:11
Show Gist options
  • Save doytsujin/ba8bffcf9acf98d608a01e031b6c746a to your computer and use it in GitHub Desktop.
Save doytsujin/ba8bffcf9acf98d608a01e031b6c746a 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\"}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment