Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a null_resource in terraform at the start of the script

I have a use case where I am taking all variables from locals in terraform as shown below, but before that, I want to run a null_resource block which will run a python script and update all the data into the local's file.

So my use case in simple words is to execute a null_resource block at the start of the terraform script and then run all the other resource blocks

My current code sample is as follows:

// executing script for populating data in app_config.json
resource "null_resource" "populate_data" {
  provisioner "local-exec" {
    command = "python3 scripts/data_populate.py"
  }
}


// reading data variables from app_config.json file
locals {
  config_data = jsondecode(file("${path.module}/app_config.json"))
}

How do I achieve that? All I have tried is adding a triggers command inside locals as follows but even that did not work.

locals {
  triggers = {
    order = null_resource.populate_data.id
  }
  
  config_data = jsondecode(file("${path.module}/app_config.json"))
}

like image 581
Farhaan Patel Avatar asked Sep 02 '25 14:09

Farhaan Patel


1 Answers

You can use depends_on

resource "null_resource" "populate_data" {
  provisioner "local-exec" {
    command = "python3 scripts/data_populate.py"
  }
}


// reading data variables from app_config.json file
locals {
  depends_on = [null_resource.populate_data]
  config_data = jsondecode(file("${path.module}/app_config.json"))
}

Now locals will get executed after populate_data always.

like image 96
Atul Sharma Avatar answered Sep 05 '25 15:09

Atul Sharma