terraform - 如何从字符串映射中添加条件块,使用映射键作为参数名称

问题描述 投票:0回答:0

如果可选映射变量具有键/值对,我正在尝试向资源添加条件块。如果地图不为空,我想将参数添加为键,将值添加为值。

目前这就是我正在尝试做的;

variable "aws_attributes" {
    type = optional(map(string))
    default = {"arg1":"val1", "arg2":"val2"}
}

dynamic "aws_attributes" {
    for_each = { for key, value in var.aws_attributes: key => value }
    content {
       key = value
    }
}

我得到的问题是:

Error: Invalid reference

  on modules/dlt_jobs/main.tf line 33, in resource "databricks_pipeline" "this":
  33:                for_each = {for key in var.aws_attributes: key => value}

A reference to a resource type must be followed by at least one attribute access, specifying the resource name.

Error: Unsupported argument

  on modules/dlt_jobs/main.tf line 37, in resource "databricks_pipeline" "this":
  37:                      key = value

An argument named "key" is not expected here.

我玩过很多不同的方式,但都没有成功。如果地图为空,则根本不应创建“aws_attributes”块,如果它不为空,则应使用地图变量 (aws_attributes) 中的键/值进行填充。

有人有什么想法吗?

dynamic terraform block
© www.soinside.com 2019 - 2024. All rights reserved.