Terraform 如何在 template_file 中插入地图类型?

Joh*_*her 7 terraform

我试图将映射变量传递到 template_file 中,但抛出此错误:

vars (varsname): '' 预期类型 'string',得到不可转换类型 'map[string]interface {}'

    data "template_file" "app" {

        template = "${file("./app_template.tpl")}"

        vars {
                container = "${var.container-configuration}"
        }
    }
Run Code Online (Sandbox Code Playgroud)

变量.tf

    variable "container-configuration" {
        description = "Configuration for container"
        type        = "map"
        default     = {
                    image          = "blahblah.dkr.ecr.us-east-2.amazonaws.com/connect"
                    container-port = "3000"
                    host-port      = "3000"
                    cpu            = "1024"
                    memory         = "2048"
                    log-group      = "test"
                    log-region     = "us-east-2a"
                  }
    }
Run Code Online (Sandbox Code Playgroud)

有没有办法将地图传递到模板文件中进行插值?我在文档中没有找到任何明确的内容。

Mar*_*ins 8

Terraform v0.12 引入了该templatefile函数,它吸收了数据源的主要用例template_file,并接受任何类型的值:

templatefile("${path.module}/app_template.tpl", {
  container = var.container-configuration
})
Run Code Online (Sandbox Code Playgroud)

Terraform v0.11 及更早版本没有任何方法来渲染具有非字符串值的模板。由于用于表示配置中地图值的协议的性质而存在这种限制:它只能表示字符串地图,直到 Terraform v0.12 中引入新协议为止。


Mak*_*hev 0

尚不支持传递映射,请参阅template_file文档。

摘自那篇文章:

变量必须全部是原语。直接引用列表或地图将导致验证错误。

这意味着您需要逐一传递变量。