0
votes

variables.tf

variable "env_name" {
  default = "some_client"
}

variable "azure_instance_names" {
  default = [
    "AD01",
    "AD01",
  ]
}

I', trying to create Public IP for as many instances as specified in azure_instance_names variable (2 in this case), i have issues with naming this resource, i want to create name by joining env_name and azure_instance_names variable. It must be one word, separated by -, so name should be in env_name-azure_instance_names for example:

Desired output

name=some_client-AD01 some_client-AD02

Actual output:

name=some_client AD01 some_client AD02

main.tf

resource "azurerm_public_ip" "datasourceip" {    
  count               = "${length(var.azure_instance_names)}"
  name                = "${join("-", list(format("%s %s", var.env_name, element(var.azure_instance_names, count.index))))}"
  location            = "${azurerm_resource_group.res_group.location}"
  resource_group_name = "${azurerm_resource_group.res_group.name}"
  allocation_method   = "Static"
}

In terraform apply i'm getting:

+ azurerm_public_ip.datasourceip[1]
      id:                      <computed>
      allocation_method:       "Static"
      fqdn:                    <computed>
      idle_timeout_in_minutes: "4"
      ip_address:              <computed>
      ip_version:              "IPv4"
      location:                "westeurope"
      name:                    "some_client AD01"
      resource_group_name:     "myrg1"
      sku:                     "Basic"
      tags.%:                  <computed>

Because Azure doesn't accept resource name in more than one word i', trying to join "-" to var.env_name, var.azure_instance_names so resource name should be some_client-AD01

Although i specified join function i', still getting same error:

azurerm_public_ip.datasourceip.1: Error Creating/Updating Public IP "some_client AD01" (Resource Group "myrg1"): network.PublicIPAddressesClient#CreateOrUpdate: Failure sending request: StatusCode=400 -- Original Error: Code="InvalidResourceName" Message="Resource name some_client LBA-P-EU2B-AD01 is invalid. The name can be up to 80 characters long. It must begin with a word character, and it must end with a word character or with ''. The name may contain word characters or '.', '-', ''." Details=[]

2

2 Answers

3
votes

Just use interpolation all the way:

name = "${var.env_name}-${var.azure_instance_names[count.index]}"

I also find ${var.foo[i]} easier to read than ${element(var.foo, i)}.

0
votes

Stupid me !!, solution was simple:

instead of putting both variables into join function, i just need to concatenate var.env_name and ${format("%s",element(var.azure_instance_names, count.index)) in this way:

name =  "${var.env_name}-${format("%s",element(var.azure_instance_names, count.index))}"



azurerm_public_ip.datasourceip[1]: Creating...
  allocation_method:       "" => "Static"
  fqdn:                    "" => "<computed>"
  idle_timeout_in_minutes: "" => "4"
  ip_address:              "" => "<computed>"
  ip_version:              "" => "IPv4"
  location:                "" => "westeurope"
  name:                    "" => "some_client-AD01"
  resource_group_name:     "" => "myrg1"
  sku:                     "" => "Basic"
  tags.%:                  "" => "<computed>"
azurerm_public_ip.datasourceip[0]: Creating...
  allocation_method:       "" => "Static"
  fqdn:                    "" => "<computed>"
  idle_timeout_in_minutes: "" => "4"
  ip_address:              "" => "<computed>"
  ip_version:              "" => "IPv4"
  location:                "" => "westeurope"
  name:                    "" => "some_client-AD02"
  resource_group_name:     "" => "myrg1"
  sku:                     "" => "Basic"
  tags.%:                  "" => "<computed>"
azurerm_public_ip.datasourceip[1]: Creation complete after 5s (ID: /subscriptions/2a325f88-e3dd-48c2-bf85-...PAddresses/some_client-AD01)
azurerm_public_ip.datasourceip[0]: Creation complete after 5s (ID: /subscriptions/2a325f88-e3dd-48c2-bf85-...PAddresses/some_client-AD02)

Apply complete! Resources: 2 added, 0 changed, 0 destroyed.