Best way to truncate a string greater than a certain length

3,012 views
Skip to first unread message

Mark Snelling

unread,
Aug 3, 2017, 10:43:50 AM8/3/17
to Terraform
I'm hitting a possible limitation in Terraform interpolation where I'm attempting to truncate a string if it's longer than a certain number of characters. For example

variable name {
  default = "test"
}

resource "any_resource_type" "foo" {
  bar = ${length(var.name) > 15 ? substr(var.name, 0, 15) : var.name}
}

I keep getting the error "substr: 'offset + length' cannot be larger than the length of the string". It looks like the true part of the ternary is still getting evaluated even though the condition is false.

Am I doing something wrong or is there a better way to acheive this?

Thanks in advance,

Mark

Lowe Schmidt

unread,
Aug 3, 2017, 12:34:27 PM8/3/17
to terrafo...@googlegroups.com
Isn't just substringing without the condition nicer?

--
Lowe Schmidt | +46 723 867 157

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-tool+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/ea34a0a5-6a03-45f9-8e14-9dff71d1e377%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mark Snelling

unread,
Aug 4, 2017, 9:56:00 AM8/4/17
to Terraform
No, substr() returns an error if the length parameter is greater than the actual string length.

On Thursday, 3 August 2017 17:34:27 UTC+1, Lowe Schmidt wrote:
Isn't just substringing without the condition nicer?

--
Lowe Schmidt | +46 723 867 157

On 3 August 2017 at 16:43, Mark Snelling <ma...@bakedbeans.com> wrote:
I'm hitting a possible limitation in Terraform interpolation where I'm attempting to truncate a string if it's longer than a certain number of characters. For example

variable name {
  default = "test"
}

resource "any_resource_type" "foo" {
  bar = ${length(var.name) > 15 ? substr(var.name, 0, 15) : var.name}
}

I keep getting the error "substr: 'offset + length' cannot be larger than the length of the string". It looks like the true part of the ternary is still getting evaluated even though the condition is false.

Am I doing something wrong or is there a better way to acheive this?

Thanks in advance,

Mark

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.

Alexander Lehmann

unread,
Aug 4, 2017, 10:59:41 AM8/4/17
to Terraform
If terraform has a min function, you could do something like

bar = substr(var.name, 0, min(length(var.name, 15)))
Reply all
Reply to author
Forward
0 new messages