How to format numeric variable in Terraform

2 min read 06-10-2024
How to format numeric variable in Terraform


Formatting Numeric Variables in Terraform: A Guide to Precision and Clarity

Terraform, the popular infrastructure-as-code tool, excels at managing complex infrastructure deployments. However, sometimes we need to format numeric variables within our configuration files for readability or compatibility with external systems. This article will guide you through various techniques for formatting numeric variables in Terraform, empowering you to create cleaner, more organized configurations.

The Need for Formatting: A Scenario

Let's imagine you're configuring a cloud service where you need to specify a disk size in gigabytes. You have a variable disk_size storing the size in raw gigabytes, but the service expects a string representation in the format "1000GB."

variable "disk_size" {
  default = 1000
}

resource "aws_ebs_volume" "main" {
  # This will fail as it expects a string
  size = var.disk_size 
}

This code won't work because the size attribute expects a string. We need to format our numeric variable disk_size to meet the service's requirements.

Terraform's Formatting Arsenal: Leveraging Built-in Functions

Terraform offers powerful built-in functions for manipulating data types, including formatting numeric variables. Let's explore some of these functions:

1. format() function:

The format() function is your primary tool for shaping strings from any data type. It allows you to combine text with values, providing great flexibility:

resource "aws_ebs_volume" "main" {
  size = format("%dGB", var.disk_size)
}

Here, the %d placeholder is replaced with the value of var.disk_size, resulting in "1000GB".

2. formatlist() function:

If you need to format multiple numeric values into a specific format, the formatlist() function comes in handy.

variable "disk_sizes" {
  default = [100, 250, 500]
}

resource "aws_ebs_volume" "main" {
  size = formatlist("%dGB", var.disk_sizes) 
}

This example will generate a list of strings: ["100GB", "250GB", "500GB"]

3. format_list() function:

While formatlist() allows you to format each element in a list, format_list() allows you to combine multiple list elements within a single formatted string.

variable "disk_sizes" {
  default = [100, 250, 500]
}

output "disk_size_string" {
  value = format_list("Disk sizes: %sGB, %sGB, %sGB", var.disk_sizes) 
}

This will output: "Disk sizes: 100GB, 250GB, 500GB"

Beyond Basic Formatting: Advanced Techniques

1. Formatting with Precision:

For scenarios where you need to control the precision of decimal numbers, you can use the sprintf() function.

variable "price" {
  default = 12.345678
}

output "price_formatted" {
  value = sprintf("%.2f", var.price)
}

This will round the price variable to two decimal places, outputting "12.35".

2. Formatting with Thousands Separators:

For improved readability, especially when dealing with large numbers, use the format function with appropriate placeholders.

variable "population" {
  default = 1000000
}

output "population_formatted" {
  value = format("%d", var.population)
}

This will output "1,000,000".

Best Practices for Formatting in Terraform

  • Consistency: Employ consistent formatting throughout your Terraform configurations for a professional and maintainable codebase.
  • Clarity: Prioritize formatting that makes your code easily understandable, even to others.
  • Readability: Ensure that your formatted output is human-readable and avoids ambiguity.

Conclusion

Mastering numeric formatting in Terraform empowers you to write cleaner, more robust infrastructure definitions. By utilizing the various built-in functions and following best practices, you can ensure your configurations are both functional and aesthetically pleasing, leading to a more efficient and enjoyable infrastructure management experience.