DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Enumerate and Zip in Python
  • Sliding Window
  • Adding Two Hours in DataWeave: Mule 4
  • Beyond Algorithms: The Human Element in AI-Driven Cybersecurity

Trending

  • How to Format Articles for DZone
  • Minimus Expands Enterprise Security Platform with General Availability of Advanced Supply Chain Controls
  • How to Interpret the Number of Spring ApplicationContexts in Integration Tests
  • How to Implement AI Agents in Rails With RubyLLM
  1. DZone
  2. Coding
  3. Tools
  4. Terraform Compact Function: Clean Up and Simplify Lists

Terraform Compact Function: Clean Up and Simplify Lists

The compact() function ensures that only valid, non-null elements are included, helping prevent runtime errors during the apply phase.

By 
Mariusz Michalowski user avatar
Mariusz Michalowski
·
Sep. 17, 25 · Analysis
Likes (6)
Comment
Save
Tweet
Share
3.4K Views

Join the DZone community and get the full member experience.

Join For Free

In Terraform, many configurations are dynamic, and you may build a list using conditional expressions that return null when not applicable. If those null values are passed directly to a resource (for example, in security_group_ids or depends_on), they can cause validation errors. 

The compact() function ensures that only valid, non-null elements are included, helping prevent such runtime errors during the apply phase. 

Terraform Compact Function

The compact() function in Terraform removes null and empty string ("") elements from a list and returns a new list containing only non-empty, non-null string elements. It is part of Terraform’s standard library and is especially useful when working with dynamic expressions that might introduce null values, such as conditional logic or interpolated variables. 

The syntax is simple: compact(list), where list is the input that may include null entries. For example, compact(["a", "", "b"]) returns ["a", "b"].

Note: The compact function only removes null and empty string ("") elements. It does not remove other falsy values like false, 0, or empty lists ([]).

When to Use the Compact Function

The compact function in Terraform helps create clean, reliable lists, especially in complex configurations that involve conditional logic or user inputs. Here are some common use cases:

  1. Conditional resource arguments: Use compact() to filter out nulls from lists with conditional elements, ensuring only valid values are passed.
  2. Merging variable defaults and overrides: When combining lists from multiple sources, compact() removes nulls to produce clean, merged values.
  3. Outputs or module arguments: Prevent errors in terraform apply by eliminating nulls from outputs or module inputs where only valid strings or IDs are expected.
  4. Conditional inclusion in loops: Within a for loop, return null for elements to skip, then wrap the expression with compact() to exclude them from the final list.
  5. Simplified filtering logic: Replace external if conditions with inline null returns in loop bodies, followed by compact() to simplify list filtering. For example:

Suppose var.names is equal to [“Alice”, “skip”, “Bob”, “skip”, “Charlie”]

Shell
 
[for item in var.names : item != "skip" ? item : null] -> this will transform the list in [“Alice”, null, “Bob”, null, “Charlie”]


can become:

Shell
 
compact([for item in var.names : item == "skip" ? null : item]) -> this will transform the list in [“Alice”, “Bob”, “Charlie”]


How to Use the Terraform Compact Function

Here are six examples of using the compact function, including some basic and more advanced ones.

One thing to remember before we move to the examples is that the compact() function only works on lists where all elements are strings (or are coercible to strings). If you pass complex types like objects or numbers, Terraform may produce a type error.

Example 1: Removing Null From a Simple List

This example shows a static list with some null values. The compact() function scans the list and filters out any null and "" entries, returning only ["value1", "value2", "value3"]. 

Shell
 
variable "input_list" {
  default = ["value1", null, "value2", null, "value3"]
}

output "cleaned_list" {
  value = compact(var.input_list)
}


This is the most straightforward use case and often appears in variable preprocessing.

Example 2: Dynamic List With Conditional Values

Here, the list includes a conditional value that may evaluate to null. 

If include_extra is false, the "optional" value is omitted. compact() ensures the final list doesn’t retain that null, yielding ["core", "final"]. This helps avoid issues in modules that do not tolerate null values in lists.

Shell
 
variable "include_extra" {
  default = false
}

locals {
  list = compact([
    "core",
    var.include_extra ? "optional" : null,
    "final"
  ])
}

output "result" {
  value = local.list
}


Example 3: Using Compact With Flattened Lists

In this example, we first flatten a list of lists using flatten(), then pass the result to compact() to remove any null values. The end result is a clean, flat list: ["a", "b", "c"]. 

Shell
 
locals {
  nested_lists = [
    ["a", null],
    [null, "b"],
    ["c"]
  ]

  flat_and_clean = compact(flatten(local.nested_lists))
}

output "flat_cleaned_list" {
  value = local.flat_and_clean
}


This approach is useful when handling outputs from modules or remote data sources that return nested structures.

Example 4: Combining Compact With Concat for Dynamic Lists

Terraform concat() function joins two lists — one static and one conditional. 

If add_debug is false, ["debug"] is replaced by [null], but compact() ensures the merged list excludes the unwanted null. 

Shell
 
variable "add_debug" {
  default = false
}

locals {
  base = ["main", "worker"]
  debug = var.add_debug ? ["debug"] : [null]

  final_roles = compact(concat(local.base, local.debug))
}

output "roles" {
  value = local.final_roles
}


Note: concat() requires each argument to be a list, even a placeholder like [null], not just null, or it will throw an error.

Example 5: Using Compact Inside a Module to Filter Inputs

Here, the root module passes a dynamically constructed list to a child module. The list includes optional elements based on input variables. compact() filters out null values before they’re passed to the module, ensuring services always receives a clean list. 

Shell
 
# root/main.tf
module "example" {
  source = "./modules/service"
  services = compact([
    "api",
    var.enable_cache ? "cache" : null,
    var.enable_auth ? "auth" : null
  ])
}

# root/variables.tf
variable "enable_cache" {
  default = true
}
variable "enable_auth" {
  default = false
}

# modules/service/variables.tf
variable "services" {
  type = list(string)
}

# modules/service/main.tf
output "services_used" {
  value = var.services
}


Example 6: Using compact in for_each to Skip Unwanted Elements

Terraform requires for_each keys to be non-null and unique, so using compact() here guarantees safe and valid iteration. 

Below, we loop over a list of usernames that may include null values. Before applying for_each, compact() ensures all null entries are excluded.

Shell
 
variable "users" {
  default = ["alice", null, "bob", null, "carol"]
}

resource "null_resource" "user_setup" {
  for_each = toset(compact(var.users))

  triggers = {
    user = each.key
  }
}


You can also use the Terraform toset() function to remove duplicates, so if preserving duplicates or order is important, use for loops over lists instead.

concat vs. compact in Terraform

The concat function in Terraform is used to combine multiple lists into a single list. It takes two or more list arguments and returns a new list containing all the elements in the given order.

Terraform compact() removes all null and empty string ("") values from a list. However, it does not alter other types of values, and it does not concatenate lists.

For example:

Plain Text
 
concat(["a"], ["b", null])         => ["a", "b", null]
compact(["a", null, "b"])          => ["a", "b"]


You can use concat to join lists together and compact to clean out empty strings from a list. These functions can also be combined when you need to merge lists and remove empty entries.

Key Points

The compact() function in Terraform helps streamline your code by removing any null or empty string values from lists, making your configurations safer, less error-prone, and easier to maintain.

Element Strings Terraform (software)

Published at DZone with permission of Mariusz Michalowski. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Enumerate and Zip in Python
  • Sliding Window
  • Adding Two Hours in DataWeave: Mule 4
  • Beyond Algorithms: The Human Element in AI-Driven Cybersecurity

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook