patternterraformMinor
Terraform | The "count" object can be used only in "resource" and "data" blocks,
Viewed 0 times
cantheblocksresourceusedandcountobjectdataonly
Problem
I'm trying to upgrade out Terraform seeing as 0.12 (stable) came out recently. I've ran into some issues with a certain pair of resource blocks. I'm getting the below error message when trying to do terraform plan, if I remove the count index it gives me a different error and says that I need the count index but that gives this error:
Here is the terraform stripped down to only the relevant parts.
What am I missing here? This is Terraform 0.12 stable using Azure as the provider.
The "count" object can be used only in "resource" and "data" blocks, and only
when the "count" argument is set.Here is the terraform stripped down to only the relevant parts.
resource "azurerm_network_interface" "network_interface" {
count = "${var.vm_server_count}"
location = "${var.location}"
}
}
Problematic resource block
resource "azurerm_network_interface_backend_address_pool_association" "network_interface" {
network_interface_id = "${azurerm_network_interface.network_interface[count.index]}"
ip_configuration_name = "example"
backend_address_pool_id = "${azurerm_lb_backend_address_pool.network_interface.id}"
}What am I missing here? This is Terraform 0.12 stable using Azure as the provider.
Solution
The
Given the specific pair of resource types you're using here, I expect your intent was to create one association for each network interface, in which case you'd add the following
Although this didn't return an error on Terraform 0.11, it did not behave as I expect you intended: it would've created only one
If associating only the first one was your intent instead, then replace the
resource "azurerm_network_interface_backend_address_pool_association" "network_interface" block doesn't have the count argument set, so count.index is not meaningful in that block.Given the specific pair of resource types you're using here, I expect your intent was to create one association for each network interface, in which case you'd add the following
count argument to the second block:count = length(azurerm_network_interface.network_interface)Although this didn't return an error on Terraform 0.11, it did not behave as I expect you intended: it would've created only one
azurerm_network_interface_backend_address_pool_association for the first network interface, and left the others unassociated.If associating only the first one was your intent instead, then replace the
count.index in the second block with 0 to be explicit that it's using only the first one:network_interface_id = azurerm_network_interface.network_interface[0]Code Snippets
count = length(azurerm_network_interface.network_interface)network_interface_id = azurerm_network_interface.network_interface[0]Context
StackExchange DevOps Q#8369, answer score: 6
Revisions (0)
No revisions yet.