Terraform - access root module script from child module

限于喜欢 提交于 2021-01-29 04:04:42

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/65151558/terraform-access-root-module-script-from-child-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!