1

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, "]"))}"
}

1 Answer 1

2

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
]
Sign up to request clarification or add additional context in comments.

2 Comments

You are awesome. Worked like a charm! Thank you.
No problem at all. You can see the outputs using terraform show as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.