Terraform: How to extract filename/extension from a path

Updated: February 4, 2024 By: Guest Contributor Post a comment

Introduction

Managing infrastructure as code requires dealing with numerous file paths, especially in Terraform, where configurations, state files, and various scripts often play a crucial role. Understanding how to manipulate and extract segments from these paths, such as the filename or extension, can significantly enhance your Terraform scripts’ flexibility and readability. This tutorial dives into practical ways to handle filename and extension extraction within Terraform, employing built-in functions and showcasing real-world scenarios.

Utilizing Terraform’s String Functions

Terraform, a powerful tool by HashiCorp for building, changing, and versioning infrastructure safely and efficiently, is imbued with a rich set of built-in functions that facilitate string manipulation among other capabilities. Before extracting filenames or extensions, familiarity with key string functions is essential.

basename

The basename function extracts the last component of a path, effectively giving you the file name with its extension.

basename('path/to/your/file.txt')
# Returns 'file.txt'

replace

The replace function allows for string substitution. It’s pivotal when manipulating paths to remove extensions or any other part of the string.

replace('file.txt', '.txt', '')
# Returns 'file'

Extracting Filenames and Extensions

Now let’s delve into the specifics of extracting filenames and extensions from a path in Terraform.

Extracting the Filename

The simplest approach to extract only the filename (without the extension) combines the use of basename and replace functions:

locals {
  full_path = 'path/to/your/file.txt'
  filename_with_extension = basename(local.full_path)
  filename_without_extension = replace(local.filename_with_extension, '.txt', '')
}
# Result: filename_without_extension = 'file'

This method strips the extension by directly replacing it with an empty string. However, this assumes you know the extension beforehand. To dynamically handle any extension, you can use regular expressions with the replace function:

locals {
  full_path = 'path/to/your/different.extension'
  filename_with_extension = basename(local.full_path)
  filename_without_extension = replace(local.filename_with_extension, /\.\w+$/, '')
}
# Result: filename_without_extension = 'different'

Extracting the Extension

While Terraform doesn’t have a function explicitly designed to extract file extensions, you can accomplish this by creatively using the replace function and regex. Here is a pattern that isolates the extension:

locals {
  full_path = 'path/to/your/file.txt'
  extension = replace(basename(local.full_path), /^.*\./, '')
}
# Result: extension = 'txt'

This method effectively discards everything before the last dot, leaving only the extension. It’s worth noting that this will return an empty string if the file has no extension.

Advanced Scenarios

Understanding the basic extraction of filenames and extensions lays the foundation for more complex manipulations and checks in your Terraform configuration. Consider scenarios where your infrastructure setup might depend on the presence or absence of certain file types. Here, sophisticated string manipulation and conditional logic in Terraform can guide dynamic resource creation or configuration.

Conditional File Handling

Imagine a situation where Terraform needs to apply different policies based on file types. Combining our knowledge of extracting file extensions with Terraform’s condition function enables powerful, dynamic configurations:

resource "aws_s3_bucket_object" "example" {
  for_each = { for f in local.files : basename(f) => f if replace(basename(f), /^.*\./, '') == 'txt' }
  bucket = aws_s3_bucket.example.id
  key    = each.value
  source = each.value
}
# This configuration dynamically includes only .txt files in an S3 bucket.

This snippet demonstrates filtering files dynamically based on their extension. It not only showcases the utility of Terraform’s string functions but also highlights how to use them in real-world scenarios for more efficient and condition-aware infrastructure configurations.

Conclusion

Extracting filenames and extensions in Terraform, though not directly supported through dedicated functions, is straightforward with the clever use of built-in string manipulation functions. Mastery of these techniques allows for more refined control over your infrastructure’s configuration files and scripts, making your Terraform code more flexible and maintainable. Whether for conditional configurations, dynamic resource definition, or mere organizational purposes, these skills are invaluable for any Terraform practitioner.

Being adept at these techniques positions you to harness the full power of Terraform, tailoring your infrastructure to precisely fit your application’s needs with code that’s as readable as it is robust.