It would be helpful to be able to truncate strings, because some providers limit the length of identifiers and such. A new built-in function like truncate(string, length)
would help users solve this problem.
Agree this would be useful.
if anyone finds themselves on this issue stuck until it is added, you can use the replace()
function to work around it, e.g. ${replace(var.foo, "/(.{0,10})(.*)/", "$1")}
Ah smart; hadn't noticed replace and regex...
this seems like an oversight in the interpolations toolbox. Yes we can hack around it, but it should really be there.
More concisely than replace + regex is the substr
function:
${substr(var.some_string, 0, 10)}"
Looks like that was added March this year: https://github.com/hashicorp/terraform/pull/12870
So, can probably close this now.
Excellent. The new substr
function solves this.
Apologies for posting to closed issue, but I've faced that substr()
don't accept input if it's shorter than offset+length
: substr: 'offset + length' cannot be larger than the length of the string in
. So I had to google around and found solution in this post: ${format("%.14s", var.project_name)}
Ran into this myself using aws_elasticache_cluster
, where the cluster_id
has to be 20 characters or under: programmatically shortening some variables using substr
will work, unless the variables being interpolated are under the desired length. Wrapping the substr
in a ternary also doesn't work as apparently both sides of the ternary get evaluating, causing an error.
Consider the example:
locals {
ClusterId = "${length(var.project) > 11 ? substr(var.project, 0, 12) : var.project}-${length(var.environment) > 4 ? substr(var.environment, 0, 4) : var.environment}-mem"
// ...
}
resource "aws_elasticache_cluster" "Memcached" {
cluster_id = "${local.ClusterId}"
// ...
}
If we give the variables environment
and project
the values of hi
and u
, we'll get an error:
Error: Error asking for user input: 1 error(s) occurred:
* module.memcached.local.ClusterId: local.ClusterId: substr: 'offset + length' cannot be larger than the length of the string in:
${length(var.project) > 11 ? substr(var.project, 0, 12) : var.project}-${length(var.environment) > 4 ? substr(var.environment, 0, 4) : var.environment}-mem
If the ternary branches were lazily evaluated, this technique would work. Fixing substr
to understand truncating strings shorted than the max length would be a better solution. I'm sure there's other reasons to not evaluate both branches of a ternary, however...
We ran into the ElastiCache case as part of a module, where the expected name length was unpredictable, but we found a different solution: we substr the entire string if it's not too long. I suppose it's a matter of opinion if that's better or hackier than a hypothetical ternary 😅
replication_group_id = "${substr("${var.cluster_name}", 0, min(length("${var.cluster_name}"), 20))}"
Just in case it helps anybody else.
I'm going to lock this issue because it has been closed for _30 days_ ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.
Most helpful comment
Apologies for posting to closed issue, but I've faced that
substr()
don't accept input if it's shorter thanoffset+length
:substr: 'offset + length' cannot be larger than the length of the string in
. So I had to google around and found solution in this post:${format("%.14s", var.project_name)}