How to get a substring of a variable?

7,548 views
Skip to first unread message

MCraig

unread,
Nov 21, 2016, 1:19:22 PM11/21/16
to Terraform
Hello,

I don't see a built in function to return a substring of a variable. Is there a method for this?
For example:

# Specify the Zone
variable "zone" {
  description = "Specify the Enviroment Zone"
  type        = "string"
  default     = "Lab"
}



Then in an ELB configuration, shown in red below, I only want the first letter of this variable to use as a prefix:


resource "aws_elb" "ELB-EXTERNAL" {
  name                        = "${var.zone}-ELB-EXTERNAL"
  security_groups             = ["${aws_security_group.elb_allow_web_traffic_http.id}"]
  subnets                     = ["${aws_subnet.public.*.id}"]
  cross_zone_load_balancing   = true
  idle_timeout                = 400
  connection_draining         = true
  connection_draining_timeout = 300

  listener {
    instance_port     = "${var.default-elb-instance_port}"
    instance_protocol = "${var.default-elb-instance_protocol}"
    lb_port           = "${var.default-elb-lb_port}"
    lb_protocol       = "${var.default-elb-lb_protocol}"
  }

  health_check {
    healthy_threshold   = "${var.default-elb-healthy_threshold}"
    unhealthy_threshold = "${var.default-elb-unhealthy_threshold}"
    timeout             = "${var.default-elb-timeout}"
    target              = "${var.default-elb-target}"
    interval            = "${var.default-elb-interval}"
  }
}


Thank you.

Paddy Foran

unread,
Nov 21, 2016, 2:26:01 PM11/21/16
to terrafo...@googlegroups.com
If you use the `split` function, with no delimiter (so `split("", var.myvar)`) you'll get a list of each individual character in a string.

Hope that helps!

--
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/5abdcc92-c047-4520-b663-1bcad825ddbe%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Paddy Foran

unread,
Nov 21, 2016, 2:29:56 PM11/21/16
to terrafo...@googlegroups.com
Oops, sorry, forgot a part.

So split works, but then you want to access a specific index, so you can use `element` for that. This example works for me:

`element(split("", var.testing), 0)`

That outputs `h` when `var.testing` is set to `hello, world`.

As an aside, `terraform console` (in 0.8.0-beta2) is super useful, and I'd recommend playing around with it if you can do so safely!

Mike Craig

unread,
Nov 21, 2016, 3:27:00 PM11/21/16
to terrafo...@googlegroups.com
Thank you! Will try that out.

You received this message because you are subscribed to a topic in the Google Groups "Terraform" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/terraform-tool/eOgxoJ4y-no/unsubscribe.
To unsubscribe from this group and all its topics, 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/CA%2BBw9CgzAbOE5BxZsqbETLY8JqLx%2BXnjG5jjXhHKB9BOzThunw%40mail.gmail.com.

Anum Sheraz

unread,
Apr 9, 2018, 11:28:03 AM4/9/18
to Terraform
I especially joined this group to Thank you for such a useful answer. God bless you :) 
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.

Anum Sheraz

unread,
Apr 9, 2018, 12:35:08 PM4/9/18
to Terraform
How do we define this as variable in .tf file. I do this like:

variable WAP_subnet {
    type = "string"
    default = ${element(split("", var.testing), 2)}
}

but I get 

Failed to load root config module: Error parsing /home/anum/test/test.tf: At 9:25: illegal char

On Monday, November 21, 2016 at 7:29:56 PM UTC, Paddy Foran wrote:
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.

Samuel Gendler

unread,
Apr 9, 2018, 3:32:30 PM4/9/18
to terrafo...@googlegroups.com
I think that you don't.  If you want it to be from a variable that is output by other terraform code, then you don't declare it as a variable. You can get the value either by direct reference to the resource's output, if it is in the same template, or else use terraform_remote_state to pull the value from the outputs of another template, then you can pass that to the split() function to grap the first char.  If you want to use it in a bunch of places, define a locals block and set the variable up in there.  Then you can refer to it as local.my_prefix_char or whatever you named the local variable.  That way, if you ever update the method by which you compute the value, you only have to change the one place.  But if it is a static string that needs to be passed in, I think your only option is to pass it in as a string, since I don't think it does interpolation on values of variables except in a 'locals' block.


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/4b3c8c4a-56d4-41e8-8a7c-70c784c5140f%40googlegroups.com.

Paddy Carver

unread,
Apr 9, 2018, 6:25:20 PM4/9/18
to terrafo...@googlegroups.com
Apart from not being able to use interpolations in default values for variables, you need to quote both the variable name and the default value. So

variable WAP_subnet {
    type = "string"
    default = ${element(split("", var.testing), 2)}
}


should be

variable "WAP_subnet" {
    type = "string"
    default = "${element(split("", var.testing), 2)}"
}
Reply all
Reply to author
Forward
0 new messages