6

I have the following terraform file:

variable azure_network_interface_ip_configuration {
default = {
    name                          = "testconfiguration1"
    subnet_id                     = "1" #"${azurerm_subnet.test.id}"
    private_ip_address_allocation = "Static"
    private_ip_address            = "10.0.2.5"
    public_ip_address_id          = "1" #"${azurerm_public_ip.test.id}"
  }
  type = object({ name=string, subnet_id=string, private_ip_address_allocation=string, private_ip_address=string, public_ip_address_id=string })
}

resource "azurerm_resource_group" "test" {
  name     = "experiment"
  location = "westeurope"
}

resource "azurerm_virtual_network" "test" {
  name                = "test-network"
  address_space       = ["10.0.0.0/16"]
  location            = "${azurerm_resource_group.test.location}"
  resource_group_name = "${azurerm_resource_group.test.name}"
}

resource "azurerm_subnet" "test" {
  name                 = "acctsub"
  resource_group_name  = "${azurerm_resource_group.test.name}"
  virtual_network_name = "${azurerm_virtual_network.test.name}"
  address_prefix       = "10.0.2.0/24"
}

resource "azurerm_public_ip" "test" {
  name                    = "test-pip"
  location                = "${azurerm_resource_group.test.location}"
  resource_group_name     = "${azurerm_resource_group.test.name}"
  allocation_method       = "Dynamic"
  idle_timeout_in_minutes = 30

  tags = {
    environment = "test"
  }
}

resource "azurerm_network_interface" "test" {
  name                = "test-nic"
  location            = "${azurerm_resource_group.test.location}"
  resource_group_name = "${azurerm_resource_group.test.name}"

  ip_configuration    = var.azure_network_interface_ip_configuration
}

But when I try to validate it, I get the follwing output.

Error: Unsupported argument

on main.tf line 48, in resource "azurerm_network_interface" "test": 48: ip_configuration = var.azure_network_interface_ip_configuration

An argument named "ip_configuration" is not expected here. Did you mean to define a block of type "ip_configuration"?

I just would like to use a variable as a block. I am using terraform 0.12.8. I know that I could set each parameter individually, but it would be far easier for me, to just set the complete block.

Updated by Yurik: See related GitHub issue 25668

1 Answer 1

10

It isn't possible to populate the entire block in just one line. Instead, you must write out the block and assign each argument separately so that the transformation from an object value to a block is explicit and Terraform can validate the individual arguments:

  ip_configuration {
    name                          = var.azure_network_interface_ip_configuration.name
    subnet_id                     = var.azure_network_interface_ip_configuration.subnet_id
    private_ip_address_allocation = var.azure_network_interface_ip_configuration.private_ip_address_allocation
    private_ip_address            = var.azure_network_interface_ip_configuration.private_ip_address
    public_ip_address_id          = var.azure_network_interface_ip_configuration.public_ip_address_id
  }

This is an example of a Terraform design tradeoff that aims to make the configuration clearer to future readers at the expense of requiring a little more work on the part of the configuration author. In this case, the goal is to make it explicit which arguments are being set, rather than requring the reader to find and understand the declaration of var.azure_network_interface_ip_configuration in order to learn that.

Sign up to request clarification or add additional context in comments.

2 Comments

I wonder whether this was a conscious design choice or they just mindlessly stumbled into it.
yeah... seems since the "object" here is typed in the module, that tf could definitely assure all the parameters were set... seems a big limitation if there is an optional block param that you want to optionally include... and it gets super messy this way when you try to allow a block within a block to be passed in as a variable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.