Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform - access root module script from child module

I have a ROOT_MODULE with main.tf:

#Root Module - Just run the script
resource "null_resource" "example" {
  provisioner "local_exec" {
    command = "./script.sh"
}

and script.sh:

echo "Hello world

now I have another directory elsewhere where I've created a CHILD_MODULE with another main.tf:

#Child Module
module "ROOT_MODULE" {
  source = "gitlabURL/ROOT_MODULE"
}

I've exported my planfile: terraform plan -out="planfile" however, when I do terraform apply against the planfile, the directory I am currently in no longer has any idea where the script.sh is. I need to keep the script in the same directory as the root module. This script is also inside a gitlab repository so I don't have a local path to call it. Any idea as to how I can get this script into my child module / execute it from my planfile?

Error running command './script.sh': exit status 1. Output: cannot access 'script.sh': No such file or directory
like image 954
J. Patwary Avatar asked Oct 29 '25 13:10

J. Patwary


1 Answers

You can access the path to the root module config to preserve pathing for files with the path.root intrinsic:

provisioner "local_exec" {
  command = "${path.root}/script.sh"
}

However, based on your question, it appears you have swapped the terminology for root module and child module. Therefore, that module appears to really be your child module and not root, and you need to access the path with the path.module intrinsic:

provisioner "local_exec" {
  command = "${path.module}/script.sh"
}

and then the pathing to the script will be preserved regardless of your current working directory.

These intrinsic expressions are documented here.

like image 196
Matt Schuchard Avatar answered Nov 01 '25 13:11

Matt Schuchard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!