Reputation: 2491
Terraform lookup() function is failing while parsing json. I am trying to get the values from a JSON file to a resource parameter. There are many parameters and I wanted to pass them from JSON file or a simple text file.
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}"
}
root module 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"
}
}
template file
[
{
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"
},
]
Error:
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")}
What would be the ideal way to achieve this sort of use case?
Upvotes: 0
Views: 3251
Reputation: 2175
Are you defining the module variable parameter_write as a list? is so try this.
"${lookup(element(var.parameter_write, count.index), "value"}"
If this does not work, what version of terraform are you using? because it looks like the array value is returning a string instead of map. If that is so, you can
data "external" "map" {
program = ["echo", "${element(var.parameter_write, count.index)}"]
}
output "ssm_value" {
value = "${data.external.map.value}"
}
Upvotes: 1