Terraform:错误查找:参数1应为type map,类型为string:

时间:2019-02-26 20:15:16

标签: terraform terraform-provider-aws

解析json时,Terraform lookup()函数失败。我正在尝试将值从JSON文件获取到资源参数。参数很多,我想从JSON文件或简单的文本文件传递它们。

module main.tf
resource "aws_ssm_parameter" "default" {
  count           = "${var.enabled == "true" ? length(var.parameter_write) : 0}"
  name            = "${lookup(var.parameter_write[count.index], "name")}"
  description     = "${lookup(var.parameter_write[count.index], "description", lookup(var.parameter_write[count.index], "name"))}"
  type            = "${lookup(var.parameter_write[count.index], "type", "SecureString")}"
  value           = "${lookup(var.parameter_write[count.index], "value")}"
  overwrite       = "${lookup(var.parameter_write[count.index], "overwrite", "false")}"
  allowed_pattern = "${lookup(var.parameter_write[count.index], "allowed_pattern", "")}"
  tags            = "${var.tags}"
}

根模块main.tf

data "template_file" "env" {
  template = "${file("${path.module}/env.tpl")}"
}

module "parameter-store" {
  source = "../../modules/ssm"

  parameter_write = ["${data.template_file.env.rendered}"]

  tags = {
    ManagedBy = "Terraform"
  }
}

模板文件

[
  {
    name      = "NAME_1"
    value     = "1440"
    type      = "String"
    overwrite = "true"
  },
  {
    name      = "NAME_2"
    value     = "100000"
    type      = "String"
    overwrite = "true"
  },
  {
    name        = "NAME_3"
    value       = "10080"
    type        = "String"
    overwrite   = "true"
    description = "example variable"
  },
]

错误:

module.parameter-store.aws_ssm_parameter.default: At column 3, line 1: lookup: argument 1 should be type map, got type string in:

${lookup(var.parameter_write[count.index], "value")}

实现这种用例的理想方法是什么?

1 个答案:

答案 0 :(得分:1)

您是否将模块变量parameter_write定义为列表?就是这样。

"${lookup(element(var.parameter_write, count.index), "value"}"

如果这不起作用,您正在使用什么版本的terraform?因为看起来数组值返回的是字符串而不是map。如果是这样,您可以

 data "external" "map" {
      program = ["echo", "${element(var.parameter_write, count.index)}"]
 }

   output "ssm_value" {
      value = "${data.external.map.value}"
   }