The trimprefix function is unavailable in your Terraform version. This error occurs when using Terraform versions older than 0.12.16 that don't support the trimprefix built-in function for string manipulation.
Terraform provides built-in functions for string manipulation, including trimprefix, which removes a specified prefix from the beginning of a string. When Terraform encounters a call to trimprefix but the function isn't available, it throws the "There is no function named trimprefix" error. This occurs because the trimprefix function was introduced in Terraform 0.12.16. If you're using an earlier version, Terraform's HCL parser doesn't recognize this function and treats it as an undefined function call. Additionally, if you're working with modules or configuration files that expect this function to be available, version mismatches can cause this error.
Run the version command to see what version of Terraform you have installed:
terraform versionThis will output your Terraform version, for example: Terraform v0.12.15 or Terraform v1.5.0. If your version is 0.12.15 or earlier, you need to upgrade.
Download and install a newer version of Terraform from the official HashiCorp releases. The trimprefix function is available in all versions from 0.12.16 onwards.
For most users, upgrading to Terraform 1.x is recommended:
# On macOS with Homebrew
brew upgrade terraform
# On Linux, download from https://www.terraform.io/downloads
# Extract to your PATH, typically /usr/local/binAfter upgrading, verify the new version:
terraform versionIf you're using modules from the Terraform Registry or a Git repository, verify they don't require a newer Terraform version than what you have. Check the module's README or source for a required_version constraint.
If a module requires a newer version, either upgrade Terraform or switch to an older version of the module:
# Example: specify an older module version
module "example" {
source = "terraform-aws-modules/vpc/aws"
version = "~> 2.0" # Use older version compatible with your Terraform
}Once you've upgraded to Terraform 0.12.16 or later, verify your trimprefix usage is correct:
# Syntax: trimprefix(string, prefix)
locals {
resource_name = "dev-my-database"
without_prefix = trimprefix(local.resource_name, "dev-") # Returns "my-database"
}
output "name" {
value = local.without_prefix
}Run terraform validate to check for syntax errors:
terraform validateThe trimprefix function is part of Terraform's string manipulation functions introduced in version 0.12.16. Related functions include trimsuffix (removes suffix), trim (removes from both ends), and trimspace (removes whitespace). If you need to work with older Terraform versions, you can use the replace() function with regex as a workaround: replace(local.resource_name, "/^dev-/", ""). However, upgrading to a supported Terraform version is strongly recommended for better compatibility, security updates, and access to new features.
Error: Error rendering template: template not found
How to fix "template not found" error in Terraform
Error: Error generating private key
How to fix 'Error generating private key' in Terraform
Error creating Kubernetes Service: field is immutable
How to fix "field is immutable" errors in Terraform
Error: Error creating local file: open: permission denied
How to fix "Error creating local file: permission denied" in Terraform
Error: line endings have changed from CRLF to LF
Line endings have changed from CRLF to LF in Terraform