In this article, we’ll explain how this function works and show some practical examples.
What is the Terraform startswith function?
The startswith function in Terraform checks whether a given string begins with a specific prefix. It returns true if the prefix matches the start of the string, and false otherwise.
It’s most often used it in validation rules, conditional logic, naming standards, tagging decisions, and list filtering.
This function is available in Terraform and OpenTofu version 1.3 and later.
Example 1: Validating a resource name prefix
You can use the startswith function to enforce naming standards across your infrastructure. Imagine that your organization requires all virtual machines to begin with the prefix prod so that production workloads are easy to identify.
In this example, a variable validation rule checks the resource name before Terraform creates anything.
If the value passed to the variable does not start with prod the configuration will fail early. This approach helps ensure clean cluster-wide naming consistency in every environment.
variable "vm_name" { type = string validation { condition = startswith(var.vm_name, "prod") error_message = "The virtual machine name must start with prod" } }
Example 2: Conditional logic in resource creation
The startswith function is also helpful when you want your configuration to behave differently depending on naming patterns.
In this example, a label input determines whether a resource should be treated as internal.
Any string that starts with int is considered internal, and an internal tag value of “true” is automatically applied. When the prefix is not matched, the tag value is “false”.
locals { is_internal = startswith(var.label, "int") } resource "aws_s3_bucket" "example" { bucket = var.label tags = { environment = var.label internal = local.is_internal ? "true" : "false" } }
Example 3: Filtering a list of names based on prefix
Terraform also allows you to combine startswith with list filtering to create dynamic sets of resources.
In this example, you have a list of service names, and only those that begin with api should be deployed. The filter keeps the configuration clean and accurate because you no longer need to manually manage two separate lists.
Terraform evaluates the list and keeps only the names that begin with api. The resulting subset drives further resource creation.
locals { services = ["api_auth", "api_billing", "frontend", "worker"] api_services = [\ for s in local.services : s\ if startswith(s, "api")\ ] } output "api_services" { value = local.api_services }
Key points
Using the Terraform startswith function helps you create consistent names, smarter conditions, and dynamic lists in your configurations. With this simple function, you gain more control and clarity across your Terraform projects.
Terraform is really powerful, but to achieve an end-to-end secure GitOps approach, you need to use a product that can run your Terraform workflows. Spacelift takes managing Terraform to the next level by giving you access to a powerful CI/CD workflow and unlocking features such as:
- Policies (based on Open Policy Agent)
- Multi-IaC workflows
- Self-service infrastructure
- Integrations with any third-party tools
If you want to learn more about Spacelift, create a free account today or book a demo with one of our engineers.
Note: New versions of Terraform are placed under the BUSL license, but everything created before version 1.5.x stays open-source. OpenTofu is an open-source version of Terraform that expands on Terraform’s existing concepts and offerings. It is a viable alternative to HashiCorp’s Terraform, being forked from Terraform version 1.5.6.