DEV Community

Amit Kushwaha
Amit Kushwaha

Posted on

->> Day-12 Terraform Functions Part-2

Overview

Terraform functions are built-in expressions that allow you to perform operations on data. Unlike general-purpose programming languages, Terraform does not support user-defined functions; however, it provides a comprehensive standard library. These lessons break them down into categories based on their utility.

Key Categories Covered
1. String Functions

These functions are essential for naming conventions, tagging, and formatting resource attributes.

upper & lower: Converts strings to specific cases. Useful for standardizing resource names.
e.g: S3 buckets which must be lowercase.

trim: Removes specified characters from the start and end of a string.

replace: Swaps substring or characters, often used to sanitize input.
e.g: replacing spaces with hyphens.

substr: Extracts a portion of a string based on an offset and length.

join: Concatenates a list of strings with a specified delimiter.
e.g: creating a comma-separated list of IPs.


2. Numeric Functions

Used for calculating values, scaling resources, or handling costs.

min & max: Returns the lowest or highest value from a set.

abs: returns the absolute value (useful for cost calculations ensuring positive numbers).

ceil & floor: Rounds numbers up or down to the nearest integer.


3. Collection Functions

These manipulate lists, maps, and sets, which are critical when dealing with multiple resources or dynamic blocks.

length: Returns the count of items in a list or characters in a string.

merge: combines multiple maps into a single map.

concat: Joins multiple lists into a single list.

lookup: Retrieves the value of a single element from a map using its key. The lesson emphasizes using the default value argument to prevent errors if a key is missing.

toset: Converts a list to a set, automatically removing duplicate values.

element: Retrieves a specific item from a list by index.


4. Filesystem Functions

These are used to load external data into Terraform.

file: Reads the contents of a file as a string. This is heavily used for importing user-data scripts for EC2 instances or loading IAM policies from JSON files.

fileexists: distinct from file, this checks if a file exists at a given path, returning a boolean.

dirname & basename: extracts the directory path or the filename from a full file path string.

5.Type Conversion & Validation

jsonencode/ jsondecode: Converts data between Terraform objects and JSON strings. This is standard practice for constructing AWS IAM policies or container definitions.

can & try: Used in Validation blocks to safely test if an expression works without halting execution.


Reference

>> Connect With Me

If you enjoyed this post or want to follow my #30DaysOfAWSTerraformChallenge journey, feel free to connect with me here:

πŸ’Ό LinkedIn: Amit Kushwaha

πŸ™ GitHub: Amit Kushwaha

πŸ“ Hashnode / Amit Kushwaha

🐦 Twitter/X: Amit Kushwaha

Top comments (0)