Home > Blockchain >  What is needed to make Terraform tolist() to work for my code after the replacement of list()
What is needed to make Terraform tolist() to work for my code after the replacement of list()

Time:11-05

This is a simple question and I am trying to understand what documentation I can use to better understand this but this is what is happening. I'm looking to update my code of Terraform and I am running into an issue when it comes to using tolist(). My snippet of code:

subnet_ids = var.zone_awareness_enabled ? var.subnet_ids : tolist(var.subnet_ids[0])

When running my terraform plan, I get this error:

|     subnet_ids = var.zone_awareness_enabled ? var.subnet_ids : tolist(var.subnet_ids[0])
│     ├────────────────
│     │ var.subnet_ids[0] is "subnet-234324df3dfd"
│
│ Invalid value for "v" parameter: cannot convert string to list of any single type.

When using list(), I had no problems but this is confusing since it is giving me the correct subnet but it's unable to process the request. Would appreciate any help.

CodePudding user response:

Depending on the requirements it could only be a simple change to the code to look like the following:

subnet_ids = var.zone_awareness_enabled ? var.subnet_ids : [ var.subnet_ids[0] ]

On the other hand, if you take a look at the tolist documentation, it says the following:

Pass a set value to tolist to convert it to a list. Since set elements are not ordered, the resulting list will have an undefined order that will be consistent within a particular run of Terraform

So it is meant to convert a set (with no indices) to a list but it cannot convert a string to a list.

  • Related