Terraform: Can't use programmatic resource names

Created on 17 Nov 2014  ยท  6Comments  ยท  Source: hashicorp/terraform

resource "aws_instance" "mongo-cfg01.${var.env_name}.${var.domain_name}" {
    ami = "${var.ami_id}"
    instance_type = "m1.small"
    tags {
        Name = "mongo-cfg01.${var.env_name}.${var.domain_name}"
    }
}
Douglass-MacBook-Pro:terraform doug$ terraform plan -var-file dev.tfvars 
There are warnings and/or errors related to your configuration. Please
fix these before continuing.

Warnings:

  * aws_instance.mongo-cfg01.${var.env_name}.${var.domain_name}: module name can only contain letters, numbers, dashes, and underscores.
This will be an error in Terraform 0.4

Seems I can't use programmatic resource names. We've mapped environments (qa, dev, prod etc) to domain names, e.g.: mongo-cfg01.prod.foobar.com, mongo-cfg01.qa.foobar.com and so on. This means we'd have to have one file per environment, which does not scale.

Most helpful comment

The design itself has a flaw if you cannot parametrize it.

All 6 comments

The names in Terraform are just logical for the purposes of referring to them. You want to parameterize the values inside the resource, and not the name itself, so this is by design.

Multiple environments are supported by using different variable and state files.

Not a bug.

Hi! After I've started using modules, this is a problem once again.
For google_dns_record_set managed_zone argument is required.
So after defining zone as module, I cant reference it in records for different zones.

modules/dns-zone/main.tf

resource "google_dns_managed_zone" "dns-zone" {
  name        = "${var.name}"
  dns_name    = "${var.dns_name}"
  description = "${var.description}"
}

dns.tf

module "test0-dns-zone" {
  source      = "modules/dns-zone"
  name        = "test0-zone"
  dns_name    = "test0.im."
  description = "Developer test0 DNS zone"
}
module "test1-dns-zone" {
  source      = "modules/dns-zone"
  name        = "test1-zone"
  dns_name    = "test1.im."
  description = "Developer test1 DNS zone"
}

records.tf

resource "google_dns_record_set" "test1_oktanium_im" {
  name = "test1.${google_dns_managed_zone.dns-zone.dns_name}"
  type = "A"
  ttl  = 60
  managed_zone = "${google_dns_managed_zone.dns-zone.name}"
  rrdatas = [
    "${google_compute_instance.somevm.0.network_interface.0.access_config.0.assigned_nat_ip}",
  ]
}

So, basically all my zones will have dns-zone name.

Maybe modules approach is not the best for zones/records? Or maybe zones as modules should be named.

The design itself has a flaw if you cannot parametrize it.

This should be reopened...

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.

Was this page helpful?
0 / 5 - 0 ratings