This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| terraform:list [2024/10/21 14:06] – created kamaradski | terraform:list [2024/10/21 14:37] (current) – [for_each] kamaradski | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Lists ====== | ====== Lists ====== | ||
| - | . | + | In Terraform, lists are one of the fundamental data types used to store an ordered collection of values. They are particularly useful when you need to manage multiple items of the same type, such as a list of IP addresses, server names, or AWS availability zones. |
| + | |||
| + | * Ordered: Elements are stored in the sequence they are defined. | ||
| + | * Index-based Access: You can access elements using zero-based indices. | ||
| ====== Defining Lists ====== | ====== Defining Lists ====== | ||
| - | . | + | Lists can be defined using square brackets [] and separating elements with commas. |
| + | |||
| + | <code hcl> | ||
| + | variable " | ||
| + | type = list(string) | ||
| + | default = [" | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | * Type Declaration: | ||
| + | * Default Value: The list of server names. | ||
| + | |||
| + | ====== Accessing List Elements ====== | ||
| + | |||
| + | You can access elements using their index. | ||
| + | |||
| + | <code hcl> | ||
| + | output " | ||
| + | value = var.server_names[0] | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ====== Iterating Over Lists ====== | ||
| + | |||
| + | Lists are often used with loops to create multiple resources. | ||
| + | |||
| + | ===== Using the count Parameter ===== | ||
| + | |||
| + | <code hcl> | ||
| + | resource " | ||
| + | count = length(var.server_names) | ||
| + | instance_type = " | ||
| + | ami = " | ||
| + | tags = { | ||
| + | Name = var.server_names[count.index] | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | * count: Creates one instance per element in the list. | ||
| + | * count.index: | ||
| + | |||
| + | ===== for_each ===== | ||
| + | |||
| + | See also: [[terraform: | ||
| + | |||
| + | ====== List Functions ====== | ||
| + | |||
| + | Terraform provides built-in functions to manipulate lists. | ||
| + | |||
| + | ===== length(list) ===== | ||
| + | |||
| + | Returns the number of elements in a list. | ||
| + | |||
| + | <code hcl> | ||
| + | locals { | ||
| + | num_servers = length(var.server_names) | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ===== concat(list1, | ||
| + | |||
| + | Concatenates two or more lists. | ||
| + | |||
| + | <code hcl> | ||
| + | locals { | ||
| + | all_servers = concat(var.web_servers, | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ===== element(list, | ||
| + | |||
| + | Safely retrieves an element at a given index. | ||
| + | |||
| + | <code hcl> | ||
| + | locals { | ||
| + | server = element(var.server_names, | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ===== slice(list, start, end) ===== | ||
| + | |||
| + | Extracts a sublist. | ||
| + | |||
| + | <code hcl> | ||
| + | locals { | ||
| + | first_two_servers = slice(var.server_names, | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ====== Using Lists with for Expressions ====== | ||
| + | |||
| + | You can transform lists using for expressions. | ||
| + | |||
| + | <code hcl> | ||
| + | locals { | ||
| + | uppercased_servers = [for name in var.server_names : upper(name)] | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | * Result: [" | ||
| + | |||
| + | ====== Dynamic Blocks with Lists ====== | ||
| + | |||
| + | Dynamic blocks can generate nested configurations based on lists. | ||
| + | |||
| + | <code hcl> | ||
| + | resource " | ||
| + | name = " | ||
| + | |||
| + | dynamic " | ||
| + | for_each = var.allowed_ports | ||
| + | content { | ||
| + | from_port | ||
| + | to_port | ||
| + | protocol | ||
| + | cidr_blocks = [" | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | * var.allowed_ports: | ||
| + | * Dynamic Ingress Rules: Creates an ingress rule for each port. | ||
| + | |||
| + | ====== Best Practices ====== | ||
| + | |||
| + | ===== Type Constraints ===== | ||
| + | |||
| + | Always specify the type of variables to avoid unexpected behavior. | ||
| + | |||
| + | <code hcl> | ||
| + | variable " | ||
| + | type = list(string) | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ===== Use Descriptive Variable Names ===== | ||
| + | |||
| + | Names should reflect the content or purpose of the list. | ||
| + | |||
| + | * Good: allowed_ips, | ||
| + | * Bad: list1, my_list | ||
| + | |||
| + | ===== Avoid Hardcoding Indices ===== | ||
| + | |||
| + | Instead of accessing elements by index, use loops or functions to handle lists dynamically. | ||
| + | |||
| + | ===== Validate Inputs ===== | ||
| + | |||
| + | Use validation blocks to ensure lists meet certain criteria. | ||
| + | |||
| + | variable " | ||
| + | type = list(string) | ||
| + | validation { | ||
| + | condition | ||
| + | error_message = "At least two subnets are required." | ||
| + | } | ||
| + | } | ||
| + | |||
| + | ====== Common Use Cases ====== | ||
| + | |||
| + | ===== Managing Multiple Resources ===== | ||
| + | |||
| + | Creating multiple resources based on a list of inputs. | ||
| + | |||
| + | <code hcl> | ||
| + | resource " | ||
| + | count = length(var.availability_zones) | ||
| + | vpc_id | ||
| + | cidr_block = cidrsubnet(var.vpc_cidr_block, | ||
| + | availability_zone = var.availability_zones[count.index] | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ===== Passing Lists to Modules ===== | ||
| + | |||
| + | Modules can accept lists to configure resources. | ||
| - | ====== Using Lists ====== | + | <code hcl> |
| + | module " | ||
| + | source | ||
| + | subnets | ||
| + | } | ||
| + | </ | ||
| - | . | + | ====== Advanced List Manipulation ====== |
| - | ====== Lists in Terraform Modules ====== | + | ===== Flattening Nested |
| - | . | + | <code hcl> |
| + | locals { | ||
| + | nested_list = [[" | ||
| + | flat_list | ||
| + | } | ||
| + | </ | ||
| - | ====== Nested | + | ===== Filtering |
| - | . | + | <code hcl> |
| + | locals { | ||
| + | numbers | ||
| + | even_numbers = [for num in local.numbers : num if num % 2 == 0] | ||
| + | } | ||
| + | </ | ||
| - | ====== Examples ====== | + | Result: [2, 4] |
| - | ===== Example 1: Simple List ===== | + | ====== Debugging Lists ====== |
| - | . | + | Use output blocks or the terraform console for debugging. |
| - | ===== Example 2: Complex List ===== | + | output " |
| + | value = var.server_names | ||
| + | } | ||
| - | . | ||