在 Terraform 中使用连接函数时出现格式错误

Format error while using join function in Terraform

我需要将 schemasREAD.usage_roles 中的值与字符串后缀“_R”连接起来,最终在资源“warehouse_grant_usageREAD_R”中创建一个名为“PROD_FINANCE_DELIVERY_COMMONDATA_READER_R”的角色,但出现以下错误使用“加入”功能时:

│   on schemas.tf line 62, in resource "snowflake_warehouse_grant" "warehouse_grant_usageREAD_R":
│   62:   roles         =  "${join("", [ "${each.value.usage_roles}" , "_R"])}" 
│     ├────────────────
│     │ each.value.usage_roles will be known only after apply
│ 
│ Inappropriate value for attribute "roles": set of string required.

结构如下:

locals {
  schemasREAD = {
    "COMMONDATA" = {
      database    = "PROD_FINANCE_DELIVERY"
      usage_roles = ["PROD_FINANCE_DELIVERY_COMMONDATA_READER"]
    }
  }
}
resource "snowflake_warehouse_grant" "warehouse_grant_usageREAD_R" {
  depends_on    = [snowflake_role.role]
  for_each      = local.schemasREAD
  warehouse_name = "PROD_FINANCE_DELIVERY_LOAD_WH"
  privilege     = "USAGE"
  roles         =  "${join("", [ "${each.value.usage_roles}" , "_R"])}" 
  with_grant_option = false
}

在这里使用 format 函数而不是 join 因为 _R 不是列表。

locals {
  schemasREAD = {
    "COMMONDATA" = {
      database    = "PROD_FINANCE_DELIVERY"
      usage_roles = ["PROD_FINANCE_DELIVERY_COMMONDATA_READER"]
    }
  }
}

output "val" {
  value = format("%s%s", local.schemasREAD.COMMONDATA.usage_roles[0], "_R")
}

连接字符串的输出::

[factory@docker-tests terraform-playground]$ terraform apply

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

val = PROD_FINANCE_DELIVERY_COMMONDATA_READER_R

您可能必须遍历 usage_roles 以获取所有角色并附加 _R 但通过上述方式使用 for_each..[=18= 很容易实现]