null_resource.triggers.string的Terraform输出,具体取决于random_string.result

时间:2018-07-09 09:09:23

标签: terraform

以下Terraform片段按资源相互依赖关系分解,感觉资源应该可以解决。

resource "random_string" "random-string" {
  length = 8
}

resource "null_resource" "null-resource" {
  triggers = {
    string = "foobar-${random_string.random-string.result}-barfoo"
  }
  depends_on = ["random_string.random-string"]
}

output "output-value" {
  value = "${null_resource.null-resource.triggers.string}"
}

错误响应错误似乎暗示着(试图解决)输出值而没有先创建资源:-

output.output-value: Resource 'null_resource.null-resource' does not have attribute 'triggers.string' for variable 'null_resource.null-resource.triggers.string'

这里是否有替代方法来实现类似目的?

作为参考,Terraform v0.11.7

2 个答案:

答案 0 :(得分:2)

@ydaetskcoR在对原始帖子的评论中建议,最合理的方法似乎是使用locals

resource "random_string" "random-string" {
  length = 8
}

locals {
  random_string = "${random_string.random-string.result}"
  secondary_string = "foobar-${local.random_string}-barfoo"
}

output "output-value" {
  value = "${local.secondary_string}"
}

答案 1 :(得分:1)

在terrain null_resource中,唯一导出的属性是id。触发器是参数而不是导出的属性。它指定何时重新创建此null_resource,因此我们不能将其用作属性来打印。

我们可以在下面做一些打印

resource "random_string" "random-string" {
  length = 8
}

resource "null_resource" "null-resource" {
  triggers = {
    string = "foobar-${random_string.random-string.result}-barfoo"
  }
  depends_on = ["random_string.random-string"]
}

data "null_data_source" "discovery" {
  inputs = {
    string1 = "foobar-${random_string.random-string.result}-barfoo"
  }
}

output "output-value" {
  value = "${data.null_data_source.discovery.inputs}"
}

希望这会有所帮助。

问候 苏达喀尔