Understanding Lists in Terraform

Published: 13 October 2024
on channel: vCloudBitsBytes
29
1

Unlock the power of Terraform with our latest video on working with lists in Terraform, tailored specifically for Azure environments! 🌐✨ Whether you're managing Azure locations, configuring virtual machines, or applying resource tags, understanding how to effectively use lists can streamline your infrastructure as code. I'll walk you through practical examples, including lists of strings, any types, maps, and objects, all within the Azure ecosystem. Perfect for beginners and seasoned pros alike!

🔗 Timestamps:
00:00 - INTRO
00:17 - What Are Lists in Terraform?
00:43 - EXAMPLE 1 - type = list(string)
01:26 - EXAMPLE 2: type = list(any)
02:57 - EXAMPLE 3: type = list(map(string))
04:06 - EXAMPLE 4: type = list(object)
05:45 - OUTRO

In Terraform, lists are collections of values accessed using a numerical index, starting from 0. All elements in a list must be of the same type, although the type can be flexible, depending on how the list is defined.

Example 1: List of Strings

A list can consist of values of a specific type, such as `string`. In the following example, a variable named `listofstrings` is defined as a list of strings. The default value is a collection of strings, and the output demonstrates accessing all elements in the list as well as a single element.

variable "listofstrings" {
type = list(string)
default = ["one", "two", "three", "four"]
}

output "listofstrings" {
value = var.listofstrings
}

output "singlevaluefromlistofstrings" {
value = var.listofstrings[1]
}

`listofstrings` outputs the entire list: `["one", "two", "three", "four"]`.
`singlevaluefromlistofstrings` outputs the value at index 1, which is `"two"` (indexing starts at 0).

Example 2: List of `any` Type

Lists can also use the `any` type, meaning the list can contain values of various types, provided that they are all compatible. In this example, different data types are used as default values, but Terraform converts them all to strings.

variable "listofhybriditems" {
type = list(any)
default = [10, "ritesh", true, "modi"]
}

output "listofhybriditems" {
value = var.listofhybriditems
}

output "singlevaluefromlistofhybriditems" {
value = var.listofhybriditems[1]
}

`listofhybriditems` outputs the entire list: `[10, "ritesh", true, "modi"]`.
`singlevaluefromlistofhybriditems` outputs `"ritesh"`, which is the element at index 1.

Example 3: List of Maps

Lists can also consist of maps. A map contains key-value pairs, and in this case, the map values are all strings. The following example defines a list of maps, where each map contains `name` and `age` keys.

variable "listofmaps" {
type = list(map(string))
default = [
{
name = "ritesh"
age = "20"
},
{
name = "avni"
age = "10"
}
]
}

Here, `listofmaps` contains two maps: one for "ritesh" with age "20" and another for "avni" with age "10".

Example 4: List of Objects

In Terraform, lists can also contain objects. An object can have multiple attributes of different types. The following example defines a list of objects with `location` (string) and `age` (number).

variable "listofobjects" {
type = list(object({
location = string
age = number
}))
default = [
{
location = "hyderabad"
age = 20
},
{
location = "kolkata"
age = 10
},
{
location = "bangalore"
age = 30
}
]
}

Each object in `listofobjects` has a `location` and `age`. For example, the first object has `"location": "hyderabad"` and `"age": 20`.

These examples show the versatility of Terraform lists, allowing for flexible data structures to fit various configuration needs.

📚 *Resources Mentioned:*

Terraform Documentation: https://www.terraform.io/docs
Azure Documentation: https://docs.microsoft.com/en-us/azure/

👍 *Like this video?*

Give it a thumbs up and share it with your Terraform and Azure colleagues!


Watch video Understanding Lists in Terraform online without registration, duration hours minute second in high quality. This video was added by user vCloudBitsBytes 13 October 2024, don't forget to share it with your friends and acquaintances, it has been viewed on our site 29 once and liked it 1 people.