尚未在模块中声明托管资源“ azurerm_virtual_network”

时间:2020-09-26 23:14:09

标签: terraform

模块中未声明托管资源“ azurerm_virtual_network”

这是我得到的错误:

     1      2      3     4    .....    13
 0   0.3    0     0.2    0    .....     0
 1   0.45   0.3     0    0    .....     0
                    .....
9k    0     0       0    0    .....     0

我与地形相关的文件夹结构是:

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.

如果我注释掉所有子网资源,则会创建2个RG和2个Vnet,但是一旦再次包含它们,我将收到该错误消息。

我对Terraform还是很陌生,将不胜感激:)

nonprod / resource_groups.tf

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

nonprod / vnets.tf

module "rg_app1" {
  source                    = "git::ssh://git@ssh.dev.azure.com/v3/user/my_code/terraform_modules//resource_group"
  rg_name                   = "tf-nonprod"
  rg_location               = "Australia Southeast"
}


module "rg_app2" {
  source                    = "git::ssh://git@ssh.dev.azure.com/v3/user/my_code/terraform_modules//resource_group"
  rg_name                   = "tf-nonprod2"
  rg_location               = "Australia Southeast"
}

nonprod / subnets.tf

module "vnet_app1" {
  source                  = "git::ssh://git@ssh.dev.azure.com/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://git@ssh.dev.azure.com/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"]
}

modules / resource_group / main.tf

  module "subnet1_app1" {
  source                  = "git::ssh://git@ssh.dev.azure.com/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 / variables.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 / outputs.tf

variable "rg_location" {
    type          = string
    description   = "location of resource group"
}

variable "rg_name" {
    type          = string
    description   = "name of resource group"
}

modules / network / vnet / main.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 / outputs.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 / variables.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 / subnet / main.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 / variables.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 / outputs.tf 没事

1 个答案:

答案 0 :(得分:0)

在module.subnet1_app1中,我猜测该模块被称为“模块/网络/子网/main.tf” ...

模块代码中的这一行看起来不正确。

  virtual_network_name                = azurerm_virtual_network.vnet.name

如果这是一个有效的模块,那么您必须检查最近是否对上述行进行了任何更改...

该值似乎是从->模块“ subnet1_app1”传递的,如下所示。

  vnet_name               =   module.vnet_app1.vnet_name

我不是Azure专家,所以 GUESSING 应该像下面这样。检查一下您的地形计划是否按预期完成...

  virtual_network_name                = var.vnet_name