chew224
chew224

Reputation: 501

Terraform, convert user input variable to type list

In Terraform, I want a user to enter values for a variable (type list) without having to worry too much about the syntax for a list variable. For example, Terraform requires the following syntax for lists:

Enter value: ["value1", "value2", "value3"]

It would be nice if the user just needed to enter a comma separated list without having to worry about adding quotations and the brackets. For example:

Enter value: value1, value2, value3

From the comma separated string, I would like to convert it to type list with the correct syntax.

My current code looks like this, I don't think I am even close to figuring it out. Any help is appreciated!

variable "subnetNames" {
   description = "Enter value:"
   default     = "value1, value2, value3"
}


output "test" {
  value = "${join(",", list("[", var.subnetNames, "]"))}"
}

Upvotes: 1

Views: 2184

Answers (1)

SomeGuyOnAComputer
SomeGuyOnAComputer

Reputation: 6258

You want to use the split function.

variable "subnetNames" {
  default     = "value1,value2,value3"
}

output "test" {
  value = "${split(",", var.subnetNames)}"
}
$ terraform init && terraform apply

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

test = [
    value1,
    value2,
    value3
]

Upvotes: 2

Related Questions