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.





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


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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế