Using Advanced Functions
1. Using alltrue and anytrue Functions
| Function | Description | Example |
|---|---|---|
| alltrue | Returns true if all elements in a list are true, otherwise false. | alltrue([true, true, false]) // Returns false |
| anytrue | Returns true if any element in a list is true, otherwise false. | anytrue([false, false, true]) // Returns true |
Note: These functions are useful for conditional logic in Terraform configurations, particularly when evaluating lists of boolean values.
2. Using can and try Functions
| Function | Description | Syntax | Notes |
|---|---|---|---|
| can() | Checks if a given expression can be evaluated without error | can(expression) |
Returns true if expression evaluates successfully, false otherwise |
| try() | Attempts to evaluate an expression and returns a default value if it fails | try(expression, default_value) |
If expression fails, returns default_value; otherwise, returns the result of expression |
Note:
can() and try() are useful for handling optional or potentially missing attributes in configurations.
3. Using coalesce and coalescelist Functions
| Function | Description | Examples | Notes |
|---|---|---|---|
| coalesce | Returns the first non-null value from a list of arguments. | Example: Basic usage |
Useful for providing fallback values in configurations. |
| coalescelist | Returns the first non-empty list from a list of lists. | Example: Basic usage |
Useful for merging lists with fallbacks. |
Note: Both functions return the first non-null or non-empty value respectively, and will throw an error if all arguments are null or empty.
4. Using compact Function
| Function | Signature | Description |
|---|---|---|
| compact | compact(list) |
Returns a list with all null and empty string elements removed. |
Example: Using compact Function
locals {
mixed_list = [null, "", "value1", null, "value2", ""]
compacted = compact(local.mixed_list)
}
output "compacted_list" {
value = local.compacted
}
Note: The
compact function removes both null and empty strings ("") from the list. It does not remove other falsy values like false or 0.
Usage Summary
- Input: A list that may contain
nullor empty string values. - Output: A new list with
nulland empty string elements removed. - Use Case: Filtering out invalid or placeholder values from lists before processing.
5. Using one Function
| Function | Description | Syntax | Example |
|---|---|---|---|
| element | Returns the element at a given index in a list | element(list, index) |
Example: Retrieving an element element(["apple", "banana", "cherry"], 1) // Returns "banana" |
| length | Returns the number of elements in a list or string | length(list) |
Example: Getting list length length(["one", "two", "three"]) // Returns 3 |
| upper | Converts a string to uppercase | upper(string) |
Example: Converting to uppercase upper("hello") // Returns "HELLO" |
6. Using matchkeys Function
| Argument | Description | Example |
|---|---|---|
| keys | List of keys to match against | ["us-east-1", "us-west-1", "eu-west-1"] |
| source | List of values to match from | ["prod", "staging", "dev"] |
| result | List of values to return based on match | ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"] |
Example: Using matchkeys to select subnet CIDR blocks
data "aws_subnets" "selected" {
ids = data.aws_subnet_ids.selected.ids
}
locals {
subnet_cidrs = matchkeys(
data.aws_subnets.selected.ids,
data.aws_subnets.selected.tags,
[data.aws_subnets.selected.tags]
)
}
Note: The
matchkeys function is useful for selecting values from one list based on matching keys in another list.
7. Using setintersection and setunion Functions
The setintersection() and setunion() functions in Terraform are used to perform set operations on lists, which is particularly useful when working with dynamic configurations or when you need to combine or filter lists of values.
| Function | Description | Example |
|---|---|---|
| setintersection() | Returns the set intersection of two or more lists, preserving the order from the first list. | setintersection("A", "B", "C"), setintersection("B", "C", "D") |
| setunion() | Returns the set union of two or more lists, preserving the order from the first list and removing duplicates. | setunion("A", "B", "C"), setunion("B", "C", "D") |
Example: Using setintersection()
locals {
list1 = ["A", "B", "C"]
list2 = ["B", "C", "D"]
intersection = setintersection(local.list1, local.list2)
}
Result: ["B", "C"]
Example: Using setunion()
locals {
list1 = ["A", "B", "C"]
list2 = ["B", "C", "D"]
union = setunion(local.list1, local.list2)
}
Result: ["A", "B", "C", "D"]
8. Using setsubtract and setproduct Functions
| Function | Signature | Description | Example |
|---|---|---|---|
| setsubtract | setsubtract(set1, set2) |
Returns the difference between two sets, removing elements from the first set that are present in the second. | setsubtract([1, 2, 3], [2, 4]) // Returns [1, 3] |
| setproduct | setproduct(set1, set2) |
Returns the Cartesian product of two sets as a list of tuples. | setproduct([1, 2], [a, b]) // Returns [[1, a], [1, b], [2, a], [2, b]] |
Example: Using setsubtract to filter resources
resource "aws_instance" "filtered_instances" {
count = length(setsubtract(var.all_instances, var.excluded_instances))
instance_type = "t3.micro"
}
Example: Using setproduct to generate combinations
locals {
environments = [ "dev", "staging", "prod" ]
regions = [ "us-east-1", "eu-west-1" ]
combinations = setproduct(local.environments, local.regions)
}
resource "aws_s3_bucket" "bucket" {
count = length(local.combinations)
bucket = "${local.combinations[count.index][0]}-bucket-${local.combinations[count.index][1]}"
}
9. Using sum and product Functions
| Function | Description | Example | Notes |
|---|---|---|---|
| sum() | Returns the sum of all elements in a list of numbers | sum([1, 2, 3, 4]) |
Accepts a list of numbers; returns a single number |
| product() | Returns the product of all elements in a list of numbers | product([2, 3, 4]) |
Accepts a list of numbers; returns a single number |
10. Using sensitive and nonsensitive Functions
| Function | Description | Usage | Notes |
|---|---|---|---|
| sensitive | Marks a value as sensitive, preventing it from being logged or displayed in plain text. | sensitive(value) |
Useful for hiding secrets in logs or UIs. The result is still accessible within Terraform. |
| nonsensitive | Removes the sensitive marking from a value, allowing it to be logged or displayed. | nonsensitive(value) |
Can be used to unwrap sensitive values when they are no longer needed in a sensitive context. |