Skip to content

Can't use programmatic resource names #571

Closed

Description

@dgarstang
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.

Activity

  1. armon commented on Nov 18, 2014

    @armon
    Contributor

    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.

  2. dgarstang commented on Nov 19, 2014

    @dgarstang
    Author

    Not a bug.

  3. yellowmegaman commented on Sep 7, 2018

    @yellowmegaman

    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.

  4. ameyaagashe commented on Feb 18, 2019

    @ameyaagashe

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

  5. dimisjim commented on Apr 10, 2019

    @dimisjim

    This should be reopened...

  6. ghost locked and limited conversation to collaborators on Aug 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions