Hi, im new in HCL. I am creating a simple configuration with the following files main.tf, variables.tf, x.auto.tfvars. When i run apply, it creates the RG in the Azure Portal but throws the error for the vnet. I resolve this by running ‘terraform apply’ again. Why this keeps happening? I dont want to add depends_on and i dont want to have to run apply twice. Please find below the whole code and error. (please ignore the lack of indentations, its not like that in my code)
Error:
│ Error: creating/updating Virtual Network (Subscription: ""
│ Resource Group Name: "rg-hub-prod-eus-01"
│ Virtual Network Name: "vnet-hub-prod-eus-01"): network.VirtualNetworksClient#CreateOrUpdate: Failure sending request: StatusCode=404 -- Original Error: Code="ResourceGroupNotFound" Message="Resource group 'rg-hub-prod-eus-01' could not be found."
│
│ with azurerm_virtual_network.vnets["hub"],
│ on main.tf line 11, in resource "azurerm_virtual_network" "vnets":
│ 11: resource "azurerm_virtual_network" "vnets" {
│
main.tf
resource "azurerm_resource_group" "rgs" {
for_each = var.rgs
name = each.value["name"]
location = var.location
}
resource "azurerm_virtual_network" "vnets" {
for_each = var.vnets
name = each.value["name"]
location = var.location
address_space = each.value["address_space"]
resource_group_name = each.value["resource_group_name"]
}
variables.tf
variable "location" {
description = "The desired region for resources"
type = string
default = "east us"
}
variable "rgs" {
description = "Map of Resource Group objects"
type = map(object({
name = string
location = string
#tags
}))
}
variable "vnets" {
description = "Map of Virtual Networks"
type = map(object({
name = string
location = string
address_space = list(string)
resource_group_name = string
}))
}
x.auto.tfvars
rgs = {
hub = {
name = "rg-hub-prod-eus-01"
location = ""
#tags
}
}
vnets = {
hub = {
name = "vnet-hub-prod-eus-01"
location = ""
address_space = ["10.0.0.0/20"]
resource_group_name = "rg-hub-prod-eus-01"
}
}