HCL Language – 26 – Custom Functions in HCL

Custom Functions in HCL

HashiCorp Configuration Language (HCL) provides a powerful and flexible way to define infrastructure configurations. While HCL offers a rich set of built-in functions and operators, you may encounter scenarios where you need custom functions tailored to your specific needs. This guide explores the concept of creating and using custom functions in HCL, enhancing the language’s capabilities for infrastructure as code (IAC) projects.

Why Custom Functions in HCL?

Custom functions in HCL serve several important purposes:

  • Abstraction: Custom functions allow you to abstract complex logic, making your HCL configurations more readable and maintainable.
  • Reusability: Once defined, custom functions can be reused across multiple configurations, reducing duplication of code.
  • Extendability: You can extend HCL’s capabilities by creating custom functions to address specific requirements not covered by built-in functions.
Creating Custom Functions

In HCL, custom functions are typically defined using the function keyword, followed by the function name, parameters, and the function body. Here’s an example of a custom function that calculates the square of a number:


# Define a custom function to calculate the square of a number
function square(n) {
  return n * n
}

# Usage of the custom function
variable "input_number" {
  type    = number
  default = 5
}

output "result" {
  value = square(var.input_number)
}
    

In this example, the custom function square accepts one parameter (n) and returns the square of that number. The function is then used to calculate the square of the input number, which is defined as a variable.

Function Parameters and Return Values

Custom functions can have parameters and return values just like built-in functions. Parameters are enclosed in parentheses, and you can specify their names and types. Return values are defined using the return statement.

Example: Custom Function with Multiple Parameters

# Define a custom function to calculate the area of a rectangle
function calculate_area(length, width) {
  return length * width
}

# Usage of the custom function
variable "rect_length" {
  type    = number
  default = 10
}

variable "rect_width" {
  type    = number
  default = 5
}

output "area" {
  value = calculate_area(var.rect_length, var.rect_width)
}
    

In this example, the custom function calculate_area takes two parameters: length and widthUsing Custom Functions Across Modules

Custom functions defined in HCL can be used across different modules, making them a powerful tool for modular and reusable configurations.

Best Practices for Custom Functions

When creating custom functions in HCL, it’s essential to follow best practices:

  • Modularity: Design your custom functions to be modular, so they can be easily integrated into different projects and configurations.
  • Documentation: Provide clear and concise documentation for your custom functions to help other team members understand their purpose and usage.
  • Testing: Test your custom functions with a variety of input values to ensure they work correctly in different scenarios.
  • Versioning: Consider versioning your custom functions to manage changes and updates effectively.
Summary

Custom functions in HCL offer a powerful way to extend the language’s capabilities and create more maintainable and reusable infrastructure configurations. By abstracting complex logic into custom functions, you can enhance the readability and efficiency of your IAC projects, making them more adaptable to specific requirements.