【发布时间】: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