1
votes

I currently have this map in a test.tfvars file:

ssm = {
    names = ["Terraform-1","Terraform-2","Terraform-3"]
    values = ["tf-1","tf-2","tf-3"]
}

And what I want to do is the following:

resource "aws_ssm_parameter" "parameter_store" {
  count = 3
  name = "$${element(var.ssm[names],count.index)}"
  type = "String"
  value = "$${element(var.ssm[values],count.index)}"
}

But instead of count=3, I would like the count to be based off of the length of the names list from my ssm map. I've tried this:

"${length(var.ssm[names])}"

But I'm getting the error:

Error: aws_ssm_parameter.parameter_store: resource count can't reference variable: names

Can anyone point me in the right direction with solving this error? I'm not too sure what I'm doing wrong.

1
Why have you doubled up your dollar signs? These should be single. eg. name = "${element(var.ssm[names],count.index)}"ydaetskcoR

1 Answers

4
votes

The current terraform version (0.11.x) behaves sometimes a bit strange, when it needs to handle lists nested in a map. This might be fixed with the new version 0.12.x, but maybe there is a better solution for that...

Why do you not restructure your map like this:

ssm = {
    "Terraform-1" = "tf-1"
    "Terraform-2" = "tf-2"
    "Terraform-3" = "tf-3"
}

Your resource would now look like this:

resource "aws_ssm_parameter" "parameter_store" {
  count = "${length(var.ssm)}"
  name  = "${keys(var.ssm)[count.index]}"
  type  = "String"
  value = "${values(var.ssm)[count.index]}"
}