Defining name from a variable

Hi,

I wrote a few plans using TF 0.12 and have now upgraded to 0.14 but am having an issue with variables.

Previously in variables.tf I was able to specify:

variable "deployment_label" {}

and then in a network.tf a section:

resource "google_compute_firewall" "fw1" {
  name         = "${var.deployment_label}-dmz-to-app"
  network     = google_compute_network.default.id
  target_tags = ["vm-app"]
  depends_on  = [google_compute_network.default]
  allow {
    protocol = "icmp"
  }
  allow {
    protocol = "tcp"
    ports    = ["80", "8080", "35001-35012"]
  }
  source_ranges = ["var.subnetwork-dmz-range"]
}

but now when I validate the plan it returns:

Error: "name" ("-dmz-to-app") doesn't match regexp "^(?:[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?)$"

  on network.tf line 32, in resource "google_compute_firewall" "fw1":
  32:   name        = "${var.deployment_label}-dmz-to-app"

So how does one now build a name using variables please ?

TIA

Hi @Phil-D,

The syntax for name you showed here looks correct to me. It seems like Terraform successfully parsed and evaluated it, but that var.deployment_label was an empty string for some reason. Perhaps the root cause is elsewhere in your configuration, in the definition of the value for that variable.


This is unrelated to your question but I wanted to point it out because you’ll probably see an error about this after you figure out what’s going on with name:

Your source_ranges argument is currently set to literally the string "var.subnetwork-dmz-range", rather than a reference to that variable. To refer to the variable, remove the quotes like this:

  source_ranges = [var.subnetwork-dmz-range]

Good day @apparentlymart,

Thank you for your reply and managed to fix the name issue :blush: It’s been a while since I have worked with TF so trying to find my way around again.

Appreciate pointing out the source_ranges issue as indeed just hit that problem :laughing:

All is working as expected now :+1: