A managed resource "azurerm_virtual_network" has not been declared in module
this is the error im getting:
Error: Reference to undeclared resource
on .terraform\modules\subnet1_app1\network\subnet\main.tf line 5, in resource "azurerm_subnet" "subnet":
5: virtual_network_name = azurerm_virtual_network.vnet.name
A managed resource "azurerm_virtual_network" "vnet" has not been declared in
module.subnet1_app1.
my terraform relevant folder structure is:
nonprod
|_ backend.tf
|_ outputs.tf
|_ provider.tf
|_ resource_groups.tf
|_ storage.tf
|_ subnets.tf
|_ variables.tf
|_ vnets.tf
modules
|_ resource_group
|_ outputs.tf
|_ variables.tf
|_ main.tf
|_ storage
|_ outputs.tf
|_ variables.tf
|_ main.tf
|_ network
|_ vnet
|_ outputs.tf
|_ variables.tf
|_ main.tf
|_ subnet
|_ outputs.tf
|_ variables.tf
|_ main.tf
The 2 x RGs and 2 x Vnets do get created if I comment out all Subnet resources, however once I include them again I receive that error message.
I am still quite new to Terraform, any assistance would be appreciated :)
nonprod/resource_groups.tf
module "rg_app1" {
source = "git::ssh://[email protected]/v3/user/my_code/terraform_modules//resource_group"
rg_name = "tf-nonprod"
rg_location = "Australia Southeast"
}
module "rg_app2" {
source = "git::ssh://[email protected]/v3/user/my_code/terraform_modules//resource_group"
rg_name = "tf-nonprod2"
rg_location = "Australia Southeast"
}
nonprod/vnets.tf
module "vnet_app1" {
source = "git::ssh://[email protected]/v3/user/my_code/terraform_modules//network/vnet"
vnet_name = "vnet-app1"
vnet_location = module.rg_app1.rg_location
rg_name = module.rg_app1.rg_name
vnet_address_space = ["10.100.0.0/16"]
dns_servers = ["1.1.1.1","4.4.4.4"]
}
module "vnet_app2" {
source = "git::ssh://[email protected]/v3/user/my_code/terraform_modules//network/vnet"
vnet_name = "vnet-app2"
vnet_location = module.rg_app2.rg_location
rg_name = module.rg_app2.rg_name
vnet_address_space = ["10.200.0.0/16"]
dns_servers = ["1.1.1.1","4.4.4.4"]
}
nonprod/subnets.tf
module "subnet1_app1" {
source = "git::ssh://[email protected]/v3/user/my_code/terraform_modules//network/subnet"
subnet_name = "subnet1"
rg_name = "tf-nonprod"
vnet_name = module.vnet_app1.vnet_name
vnet_location = "Australia Southeast"
subnet_address_prefixes = ["10.100.1.0/24"]
}
modules/resource_group/main.tf
terraform {
required_version = ">= 0.12"
}
#create resource group
resource "azurerm_resource_group" "rg" {
name = var.rg_name
location = var.rg_location
}
modules/resource_group/variables.tf
variable "rg_location" {
type = string
description = "location of resource group"
}
variable "rg_name" {
type = string
description = "name of resource group"
}
modules/resource_group/outputs.tf
output "rg_location" {
value = azurerm_resource_group.rg.location
description = "location of resource group"
}
output "rg_name" {
value = azurerm_resource_group.rg.name
description = "name of resource group"
}
modules/network/vnet/main.tf
# Create the Virtual Network
resource "azurerm_virtual_network" "vnet" {
name = var.vnet_name
location = var.vnet_location
resource_group_name = var.rg_name
address_space = var.vnet_address_space
dns_servers = var.dns_servers
}
modules/network/vnet/outputs.tf
output "vnet_name" {
description = "The Name of the newly created vNet"
value = azurerm_virtual_network.vnet.name
}
output "vnet_location" {
description = "The location of the newly created vNet"
value = azurerm_virtual_network.vnet.location
}
output "vnet_address_space" {
description = "The address space of the newly created vNet"
value = azurerm_virtual_network.vnet.address_space
}
modules/network/vnet/variables.tf
variable "vnet_location" {
type = string
description = "Location of environment"
}
variable "rg_name" {
type = string
description = "name of resource group"
}
variable "vnet_name" {
type = string
description = "Name of Virtual Network"
}
variable "vnet_address_space" {
type = list
description = "Address space of Virtual Network"
}
variable "dns_servers" {
type = list
description = "Dns servers for Virtual Network"
}
modules/network/subnet/main.tf
# Create the Subnet
resource "azurerm_subnet" "subnet" {
name = var.subnet_name
resource_group_name = var.rg_name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefixes = var.subnet_address_prefixes
}
modules/network/subnet/variables.tf
variable "rg_name" {
type = string
description = "name of resource group"
}
variable "subnet_name" {
type = string
description = "Subnet Name of Virtual Network"
}
variable "subnet_address_prefixes" {
type = list
description = "Address prefixes of Subnet"
}
# variable "virtual_network_name" {
# default = azurerm_virtual_network.vnet.name
# }
variable "vnet_location" {
type = string
description = "Location of environment"
}
variable "vnet_name" {
type = string
description = "Name of Virtual Network"
}
modules/network/subnet/outputs.tf nothing
variable "virtual_network_name"
? You have to pass it to your module. – Marcin