Getting length of list from Terraform Map
Getting length of list from Terraform Map
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.
name = "$element(var.ssm[names],count.index)"
1 Answer
1
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)"
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Why have you doubled up your dollar signs? These should be single. eg.
name = "$element(var.ssm[names],count.index)"
– ydaetskcoR
Aug 28 at 9:59