【问题标题】:Terraform: Pass a variable containing a list of strings to a jsonencode sectionTerraform:将包含字符串列表的变量传递给 jsonencode 部分
【发布时间】:2019-12-04 17:20:58
【问题描述】:

我想设置一个 Terraform 模块,根据 Terraforms policy assignment example 将策略分配给 Azure 资源。

为了分配允许位置策略,我想将允许位置列表作为字符串列表从 variables.tf 文件传递​​到执行分配的 main.tf。

main.tf

#Allowed Locations Policy Assignment 
resource "azurerm_policy_assignment" "allowedlocations" {
  name                 = "allowed-locations"
  scope                = var.scope_allowedlocations
  policy_definition_id = var.policy_allowedlocations.id 
  description          = "This policy enables you to restrict the locations."
  display_name         = "Allowed Locations"

  parameters = <<PARAMETERS
  {
  "allowedLocations": {
    "value": ${var.listofallowedlocations}
    }
  } 
  PARAMETERS

}

变量.tf

# Scope of the Allowed Locations policy
variable "scope_allowedlocations" {
  description = "The scope of the allowed locations assignment."
  default     = "Subscription"
}

# Scope of the Allowed Locations policy
variable "policy_allowedlocations" {
  description = "The allowed locations policy (created by the policy-define module)."
  default     = "default"
}

# List of the Allowed Locations
variable "listofallowedlocations" {
  type = list(string)
  description = "The allowed locations list."
  default     = [ "West Europe", "North Europe", "East US" ]
}

使用terraform plan 执行会导致以下错误:

Error: Invalid template interpolation value

  on modules/policy-assign/main.tf line 16, in resource "azurerm_policy_assignment" "allowedlocations":
  12:
  13:
  14:
  15:
  16:     "value": ${var.listofallowedlocations}
  17:
  18:
  19:
    |----------------
    | var.listofallowedlocations is list of string with 3 elements

Cannot include the given value in a string template: string required.

因此,我不知道如何将变量文件中的列表准确传递到策略分配资源的 PARAMETERS 部分。在 Terraforms policy assignment example 中,该列表直接在 PARAMETERS 部分内联编码,并且可以正常工作。但是没有传递变量...:

parameters = <<PARAMETERS
{
  "allowedLocations": {
    "value": [ "West Europe" ]
  }
}
PARAMETERS

【问题讨论】:

    标签: json azure variable-assignment terraform policy


    【解决方案1】:

    当您将一个值插入字符串时,该值本身必须可转换为字符串,否则 Terraform 无法将各个部分连接在一起以产生单个字符串结果。

    这里有几种不同的选择,有不同的权衡。


    我个人在这里会选择的选项是根本不使用&lt;&lt;PARAMETERS 语法,而只使用jsonencode 构建整个值:

    parameters = jsonencode({
      allowedLocations = {
        value = var.listofallowedlocations
      }
    })
    

    这完全避免了您的配置需要处理任何 JSON 语法问题,并且(主观上)因此使意图更清晰,未来维护更容易。

    在结果是单个有效 JSON 值的任何情况下,我总是选择使用 jsonencode 而不是模板语言。为了完整起见,我将以下其他选项包括在内,以防将来的读者试图将集合值包含到 生成 JSON 的字符串模板中。


    第二种选择是编写一个表达式来告诉 Terraform 一种将列表值转换为合适格式的字符串值的方法。在您的情况下,您需要 JSON,因此 jsonencode 再次可能是最合适的选择:

    parameters = <<PARAMETERS
    {
      "allowedLocations": {
        "value": ${jsonencode(var.listofallowedlocations)}
      }
    } 
    PARAMETERS
    

    在其他非 JSON 情况下,当结果是一个简单的字符串列表时,join 函数可用于将所有字符串与固定分隔符连接在一起。

    任何产生单个字符串的Terraform's functions 都是此处的候选对象。 “字符串函数”和“编码函数”下的那些是最有可能的选择。


    最后,对于从集合值到结果字符串的映射是自定义的,没有标准函数可以处理的情况,您可以使用模板重复语法:

    parameters = <<CONFIG
    %{ for port in var.ports ~}
    listen 127.0.0.1:${port}
    %{ endfor ~}
    CONFIG
    

    在这种情况下,Terraform 将对var.ports 中的每个元素评估一次重复构造的主体,并将所有结果连接在一起以产生结果。您可以使用这种方法生成各种文本输出格式,但如果模板变得特别复杂,最好将其分解到单独的文件中并使用templatefile 函数对其进行评估。

    【讨论】:

    • 完美。第一个解决方案,使用 jsonencode 而不是 PARAMETERS 工作正常。非常感谢
    • 它也适用于inputaws_lambda_invocation。例如:data "aws_lambda_invocation" "my_lambda_invocation"{ function_name = lambda_function_name input = jsonencode({key1: value, key2: {innerKey1: innerValue1, innerKey2: innerValue2}}) }
    猜你喜欢
    • 2021-10-04
    • 1970-01-01
    • 1970-01-01
    • 2011-04-03
    • 2020-12-10
    • 2022-08-18
    • 2022-11-27
    • 2014-10-06
    • 2011-08-21
    相关资源
    最近更新 更多