【问题标题】:Terraform how to interpolate map types in a template_file?Terraform 如何在 template_file 中插入地图类型?
【发布时间】:2019-05-19 00:18:16
【问题描述】:

我正在尝试将映射变量传递到模板文件中,并被抛出此错误:

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

    data "template_file" "app" {

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

        vars {
                container = "${var.container-configuration}"
        }
    }

变量.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"
                  }
    }

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

【问题讨论】:

标签: terraform


【解决方案1】:

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

templatefile("${path.module}/app_template.tpl", {
  container = var.container-configuration
})

Terraform v0.11 及更早版本无法使用非字符串值呈现模板。由于用于在配置中表示地图值的协议的性质,存在限制:在 Terraform v0.12 中引入的新协议之前,它只能表示字符串的地图。

【讨论】:

  • 这实际上是如何使用的,例如在local-exec中?
【解决方案2】:

尚不支持传递地图,请参阅template_file 文档。

来自那篇文章:

变量必须都是基元。直接引用列表或地图会导致验证错误。

这意味着你需要一个一个地单独传递变量。

【讨论】:

    猜你喜欢
    • 2021-09-30
    • 2016-11-25
    • 2021-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-23
    • 2019-09-30
    相关资源
    最近更新 更多