Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using output from bash script as variable within Terraform

I was wondering if someone could point me in the right direction, I am looking at verbalising our Kubernetes version within our Terraform module as currently its hard coded.

Every now and then we get an error saying that our "orchestration version" isn't valid when we deploy our cluster - this is due to Azure dropping our Kubernetes version... (We deploy our dev/uat envs daily)

So I came up with the idea of using a variable for our version so we can always deploy avoiding the "invalid orchestration" error. I scripted the bash script for what I need;

az aks get-versions --location westeurope --query 'orchestrators' -o tsv | awk '{print $3}' | tail -2 | head -n 1

But now I wish to use the output from ^^^ to use as our Kubernetes version within our Terraform module we deploy daily.

Can anyone point me in the right direction?

I've had a look at using build-args within the docker container.

like image 371
Matt Taylor Avatar asked Apr 09 '19 11:04

Matt Taylor


1 Answers

Use external datasource: https://www.terraform.io/docs/providers/external/data_source.html.

The only trouble is that the output and (optional) input must be valid Jsons with some limitiations.

Example:

data "external" "myjson" {
  program = [
    "${path.module}/scriptWhichReturnsJson.sh",
  ]
  result {

  }
}

...
resource ...  {
  value = data.external.myjson.property
}

like image 80
Mateusz Stefek Avatar answered Oct 12 '22 22:10

Mateusz Stefek