Skip to main content
tostring(v) converts v to a string. Numbers and booleans are formatted in their natural text representation. Strings pass through unchanged.

Signature

tostring(v any) string

Example

Convert a numeric port variable to a string to use it as a map key or pass it to functions that require strings:
variable "replicas" {
  description = "Number of deployment replicas"
  type        = number
  default     = 2
}

computed "replica_label" {
  description = "Replica count as a Kubernetes label value"
  expression  = tostring(var.replicas)
}

task "deploy" {
  description = "Deploy with the replica count label"
  commands = [
    "kubectl apply -f k8s/",
    "kubectl label deployment/app replica-count=${computed.replica_label} --overwrite",
  ]
}
2 becomes "2", which is a valid Kubernetes label value.

Notes

  • tostring is most useful when a typed value (number, bool) needs to be used in a context that requires a string, such as a function argument, label value, or map key.
  • In command strings, string interpolation already converts values automatically. tostring is needed explicitly only in expression contexts.