Terraform - data depends-on data

12,175

If your external data source outputs the filename you could use Terraform's interpolation to force a dependency between the 2 data sources.

So assuming the output of python XXXXX.py fileName gives {"filename": "dashboardData.json"} or similar then you could just use something like this:

data "external" "example" {
  program = ["python", "XXXXX.py", "${var.fileName}"]
}

data "local_file" "dashboard" {
  filename = "${path.module}/${data.external.example.result.filename}"
}

Because the local_file data source now refers to the external data source it will force Terraform to wait for the external data source to complete.

An alternative is to set an explicit dependency between them by using depends_on:

data "external" "example" {
  program = ["python", "XXXXX.py", "${var.fileName}"]
}

data "local_file" "dashboard" {
  filename   = "${path.module}/dashboardData.json"
  depends_on = [data.external.example]
}
Share:
12,175

Related videos on Youtube

Manzoor
Author by

Manzoor

Updated on June 04, 2022

Comments

  • Manzoor
    Manzoor almost 2 years

    I am using data external and data local_file in my terraform code. data external executes a script and create a json file. Now my data local_file has to read the json file.

    data "external" "example" {
       program = ["python", "XXXXX.py", "${var.fileName}"]
    }
    
    data "local_file" "dashboard" {
        filename = "${path.module}/dashboardData.json"
    }
    

    Here data local_file is dependent on data external for the json file.

    Is there a work aroud ?

  • Manzoor
    Manzoor over 5 years
    Out of two the first one worked as expected. The second one is throwing some error. Thanks for your help.
  • peter_v
    peter_v almost 4 years
    The second form should be depends_on = [data.external.example] the error was A static list expression is required., which tells that you need a LIST Ref. terraform.io/docs/configuration/…