模块依赖于Terraform 0.11的变通办法不起作用

时间:2020-01-25 16:35:06

标签: terraform

我正在尝试在main.tf模块中创建隐式模块依赖关系,以便在jenkins模块之后创建kube模块。

module "jenkins" {
  install_jenkins = "${var.install_jenkins}"
  env_name = "${var.env_name}"
  source        = "../../../../modules-terraform/jenkins_module"
  sync_var = "${module.kube.cluster_name}"
}

module "kube" {
  source        = "../../../../modules-terraform/kube_module"
  cluster_count = "${var.gke_cluster_create}"

}

因此(从上面的代码中可以看出),我为模块sync_var创建了(不需要)jenkins

这在模块variables.tf

jenkins中声明
variable "sync_var" {
  description = "Dummy variable used for synchronization with kube_module"
  default     = ""
}

并且cluster_name当然在模块output.tf

kube
output "cluster_name" {
  value = "${google_container_cluster.k8s.0.name}"
}

然而,apply过程(其中main.tf包含本文开头的初始tf代码段)似乎没有考虑到上述问题。

这两个模块的资源是并行创建的。

使用Terraform 0.11.14

知道为什么会这样吗?

1 个答案:

答案 0 :(得分:1)

您没有在问题中提及对var.sync_var的任何提及。如果没有至少一个依赖该变量的资源,则其依赖项将不会有任何有用的效果,因为变量本身没有外部可见的副作用。

对于Terraform 0.12,只需直接在depends_on中引用变量即可实现对变量的显式依赖,如下所示:

resource "example" "example" {
  # This resource depends on anything that
  # var.sync_var depends on.
  depends_on = [var.sync_var]

  # ...
}

在Terraform 0.11中,depends_on仅直接与资源一起使用,因此我们需要引入额外的资源以将变量之间的依赖关系桥接到其他资源的depends_on

resource "null_resource" "example" {
  triggers = {
    # By referring to the variable, this resource
    # implicitly depends on anything var.sync_var
    # depends on.
    dependency = "${var.sync_var}"
  }
}


resource "example" "example" {
  # This resource depends on anything that
  # null_resource.example depends on, which
  # includes everything that var.sync_var
  # depends on.
  depends_on = ["null_resource.example"]

  # ...
}