Variable reference to "self" or "this"?

1,671 views
Skip to first unread message

Cameron Stokes

unread,
Dec 10, 2014, 9:24:40 PM12/10/14
to terrafo...@googlegroups.com
I'm looking at using Terraform to manage Route53 records and am having to duplicate name values over and over. Is it possible to not do this? Perhaps with a ${this.name} macro or something?

In the example below, I'd like to avoid duplicating the corresponding www value for each and every record.

resource "aws_route53_zone" "example" {
   name = "example.com"
}

resource "aws_route53_record" "www" {
   zone_id = "${aws_route53_zone.example.zone_id}"
   name = "www.${aws_route53_zone.example.name}"
   type = "A"
   ttl = "60"
   records = ["127.0.0.1"]
}

Is something like below possible?

resource "aws_route53_zone" "example" {
   name = "example.com"
}

resource "aws_route53_record" "www" {
   zone_id = "${aws_route53_zone.example.zone_id}"
   type = "A"
   ttl = "60"
   records = ["127.0.0.1"]
}

Armon Dadgar

unread,
Dec 12, 2014, 5:14:05 PM12/12/14
to Cameron Stokes, terrafo...@googlegroups.com
Hey Cameron,

I think this is interesting but I think there is a balance of syntactic sugar vs language complexity.
Curious to hear others chime in.

Best Regards,
Armon Dadgar
--
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.
To post to this group, send email to terrafo...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/e3d5733a-f8a3-4ce7-85e4-854ec3a1ca59%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dave Dash

unread,
Dec 12, 2014, 5:16:25 PM12/12/14
to Armon Dadgar, Cameron Stokes, terrafo...@googlegroups.com
I’m not sure if *that* is what I want, but I do know that I want something that lets me in a single definition create a route53 record for *all* the instances I’ve created in terraform following a specific pattern.

e.g.

${aws_instance.*.tag.Name}.mysite.net



Cameron Stokes

unread,
Dec 13, 2014, 12:42:35 PM12/13/14
to terrafo...@googlegroups.com, armon....@gmail.com, cam...@cameronstokes.com
I've not yet started managing instances with Terraform, but I could see this being valuable with instances as well.

Using the example from the docs:

resource "aws_instance" "HelloWorld" {
    ami = "ami-1234"
    instance_type = "m1.small"
    tags {
        Name = "${this.name}"
    }
}

William Valadez

unread,
Dec 15, 2014, 1:23:41 PM12/15/14
to terrafo...@googlegroups.com, armon....@gmail.com, cam...@cameronstokes.com
This would be extremely useful. We set the name tag for AWS instances, and use the name as part of our provisioning script as well. Currently we write out the resource name at least 3 times for every new AWS resource created. It would be much easier to self reference the name with a variable.

David Cunningham

unread,
Dec 15, 2014, 5:24:19 PM12/15/14
to William Valadez, terrafo...@googlegroups.com, Armon Dadgar, cam...@cameronstokes.com
It would be useful to get the Terraform name of a resource from inside that resource.  With GCP you end up duplicating the name of instances, as there is a GCP name and a Terraform name, and at least in my scripts they are always the same.  I could avoid that with something as simple as ${resource_name}.  Trivial example:

resource "google_compute_address" "test" {
    name = "test"
}

becomes 

resource "google_compute_address" "test" {
    name = "${resource_name}"
}

It would actually be enough for this case if the name attribute could default to the resource name.  However I'm not sure if that's possible within the existing provider model, and it also doesn't allow interpolation, so doesn't cover these AWS cases where you want to build a string out of the name.

I don't understand why you'd want to call it ${this.name} though.  I'd expect ${this.name} to return the name attribute of this, so the original example would be a self-referential infinite loop.  Is it suggested that all attributes be visible from this?  E.g. ${this.instance_type} ?




Reply all
Reply to author
Forward
0 new messages