Terraform 上中间值的本地值和 null_data_source 之间的差异

Difference between local values and null_data_source for intermediate values on Terraform

我有一种情况需要存储一些中间值,以便我可以在根模块的其他部分重用它们。我知道本地值,我知道 null_data_source 除了我不知道哪个是保存可重用值的推荐选项。两个描述都和我有点相似

本地值 (https://www.terraform.io/docs/configuration/locals.html)

Local values can be helpful to avoid repeating the same values or expressions multiple times in a >configuration, but if overused they can also make a configuration hard to read by future >maintainers by hiding the actual values used.

null_data_source (https://www.terraform.io/docs/providers/null/data_source.html)

The primary use-case for the null data source is to gather together collections of intermediate >values to re-use elsewhere in configuration:

因此对于这种情况,两者似乎都是有效的选择。

这是我的示例代码

locals {
  my_string_A = "This is string A"
}

data "null_data_source" "my_string_B" {
  inputs = {
    my_string_B = "This is string B"
  }
}

output "my_output_a" {
  value = "${local.my_string_A}"
}

output "my_output_b" {
  value = "${data.null_data_source.my_string_B.outputs["my_string_B"]}"
}

您能否建议何时使用其中一种来保存中间值以及每种方法的 pros/cons 是什么?

谢谢

null_data_source 数据源是在本地值机制之前引入的,作为一种临时解决方案,可以在该功能成为语言中的第一个 class 之前满足该用例。继续支持它只是为了与使用它的现有配置向后兼容。

所有新配置都应改用本地值机制。它完全集成到 Terraform 语言中,支持任何类型的值(而 null_data_source 只能支持字符串),并且具有更多 concise/readable 语法。