PatrickHellman
PatrickHellman

Reputation: 79

Using data external to receive variable from bash script in terraform

I want to fetch sshkey with digital ocean token with get_sshkey.sh script:

do_token=$1


curl -X GET -s -H "Authorization: Bearer ${do_token//\"}" "https://api.digitalocean.com/v2/account/keys?page=1" | jq -r --arg queryname "User's key" '.ssh_keys[] | select(.name == $queryname).public_key'

I have a declared variable of DO token var.do-token, I am trying to use $1 bash concept to pass parameter to get_sshkey.sh and run it in terraform data "external" in following:

data "external" "fetchssh" {
  program = ["sh", "/input/get_sshkey.sh `echo "var.do-token" | terraform -chdir=/input console -var-file terraform.auto.tfvars`"]

}

but get the error: Expected a comma to mark the beginning of the next item.

Upvotes: 1

Views: 2205

Answers (1)

Martin Atkins
Martin Atkins

Reputation: 74289

In order to include literal quotes in your shell command, you'll need to escape them so that Terraform can see that you don't intend to end the quoted string template:

data "external" "fetchssh" {
  program = ["sh", "/input/get_sshkey.sh `echo \"var.do-token\" | terraform -chdir=/input console -var-file terraform.auto.tfvars`"]
}

Using the external data source in Terraform to recursively run another terraform is a pretty irregular thing to do, but as long as there's such a variable defined in that configuration I suppose it could work.

Upvotes: 1

Related Questions