Objective: I am trying to create azure resources with Terraform
Code I used in main.tf:
resource "azurerm_subnet" "clientdata_snet" {
count = var.clientdata_subnet_address_space != null ? 1 : 0
name = "ClientDataSubnet"
resource_group_name = var.resource_group_name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefixes = ["${var.clientdata_subnet_address_space}"]
service_endpoints = var.service_endpoints
}
and now subsequently want to use client_snet.id for creating storage endpoint
resource "azurerm_private_endpoint" "sa_pe_blob" {
name = "pe-stdlorpcbcntldevwe-blob-${random_string.postfix.result}"
location = var.location
resource_group_name = var.resource_group_name
subnet_id = azurerm_subnet.clientdata_snet.id
Error I get is:
Error: Missing resource instance key
│
│ on main.tf line 470, in resource "azurerm_private_endpoint" "sa_pe_blob":
│ 470: subnet_id = azurerm_subnet.clientdata_snet.id
│
│ Because azurerm_subnet.clientdata_snet has "count" set, its attributes must be accessed on specific instances.
│
│ For example, to correlate with indices of a referring resource, use:
│ azurerm_subnet.clientdata_snet[count.index]
Then I referred to some posts here.. where I need to use like below:
subnet_id = azurerm_subnet.clientdata_snet[count.index].id
then its giving me this error:
Error: Reference to "count" in non-counted context
│
│ on main.tf line 470, in resource "azurerm_private_endpoint" "sa_pe_blob":
│ 470: subnet_id = azurerm_subnet.clientdata_snet[count.index].id
│
│ The "count" object can only be used in "module", "resource", and "data" blocks, and only when the "count" argument is set.
Really confused, both ways its giving me error. I have only root module, I dont have any other modules.Can someone suggest what is correct way to do it ?