Search⌘ K
AI Features

Type constraints - List

Explore how to define list type constraints in Terraform variables and use built-in functions like element and length. This lesson helps you understand how to manipulate lists in Terraform HCL and apply this knowledge to your projects.

List

A list is a type of a list. You can have a list of strings like ["foo", "bar"] or a list of numbers [2, 4, 7]. The type means that every element in the list will be of that type.

Project example

Consider the following example:

C++
variable "a" {
type = list(string)
default = ["foo", "bar", "baz"]
}
output "a" {
value = var.a
}
output "b" {
value = element(var.a, 1)
}
output "c" {
value = length(var.a)
}

In the above code, we define a list in variable "a" to be a list ...