Error:: Unsupported attribute. This object does not have an attribute named "nsg_name"

Error:: Unsupported attribute. This object does not have an attribute named "nsg_name"

│ Error: Unsupported attribute
│
│   on nsgs.tf line 27, in resource "azurerm_network_security_rule" "example":
│   27:   network_security_group_name = module.nsg-networkcore.nsg_name
│     ├────────────────
│     │ module.nsg-networkcore is a object, known only after apply
│
│ This object does not have an attribute named "nsg_name".

一切但网络安全规则在运行 TF Apply

时创建

样板文件

        |_ providers.tf
        |_ locals.tf
        |_ resource_groups.tf
        |_ networking_vnets.tf
        |_ nsgs.tf
        |_ subnets.tf
        |_ terraform.tfvars
        |_ variables.tf
    
    modules
    |_ resource-group
              |_ main.tf
              |_ outputs.tf
              |_ variables.tf

    |_ virtual-network
              |_ main.tf
              |_ outputs.tf
              |_ variables.tf

          |_ subnet
              |_ main.tf
              |_ outputs.tf
              |_ variables.tf

          |_ nsg
              |_ main.tf
              |_ outputs.tf
              |_ variables.tf

modules/terraform-azure-module-resourcegroup/main.tf

resource "azurerm_resource_group" "rg" {
  name     = var.resource_group_name
  location = var.location
  tags     = var.tags
}

modules/terraform-azure-module-resourcegroup/outputs.tf

# Output Variables of the module

output "resource_group_name" {
  value       = azurerm_resource_group.rg.name
  description = "name of resource group"
}

output "location" {
  value       = azurerm_resource_group.rg.location
  description = "location of resource group"
}

modules/terraform-azure-module-resourcegroup/variables.tf

# Input Variables of the module

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

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

variable "tags" {
  type    = map(any)
  default = {}
}

modules/terraform-azure-module-network/virtual-network/main.tf

# Create the Virtual Network
resource "azurerm_virtual_network" "vnet" {
  name                = var.vnet_name
  location            = var.vnet_location
  resource_group_name = var.resource_group_name
  address_space       = var.vnet_address_space
  dns_servers         = var.dns_servers
}

modules/terraform-azure-module-network/virtual-network/outputs.tf

 # Vnet Outputs

output "vnet_id" {
  value       = azurerm_virtual_network.vnet.id
  description = "Virutal Network id"
}

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" {
  value       = azurerm_virtual_network.vnet.address_space
  description = "Virutal Network address_space"
}

output "dns_servers" {
  value       = azurerm_virtual_network.vnet.dns_servers
  description = "Virutal Network dns_servers"
}

output "resource_group_name" {
  value       = azurerm_virtual_network.vnet.resource_group_name
  description = "Virutal Network resource_group_name"

}

modules/terraform-azure-module-network/virtual-network/variables.tf

# Vnet Variables

variable "vnet_location" {
  type        = string
  description = "Location of environment"
}

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

variable "vnet_name" {
  type        = string
  description = "Name of Virtual Network"
}

variable "vnet_address_space" {
  type        = list(any)
  description = "Address space of Virtual Network"
}

variable "dns_servers" {
  type        = list(any)
  description = "Dns servers for Virtual Network"
}

modules/terraform-azure-module-network/virtual-network/subnet/main.tf

# Create the Subnet
resource "azurerm_subnet" "subnet" {
  name                 = var.subnet_name
  resource_group_name  = var.resource_group_name
  virtual_network_name = var.vnet_name
  address_prefixes     = var.subnet_address_prefixes
}

modules/terraform-azure-module-network/virtual-network/subnet/outputs.tf

output "subnet_name" {
  value = azurerm_subnet.subnet.name
}

output "subnet_id" {
  value = azurerm_subnet.subnet.id
}

output "subnet_address_prefixes" {
  value = azurerm_subnet.subnet.address_prefixes
}

modules/terraform-azure-module-network/virtual-network/subnet/variables.tf

variable "subnet_name" {
type        = string
}

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

variable "subnet_address_prefixes" {
  type                 = list(any)
  description          = "Address prefixes of Subnet"
}

variable "vnet_name" {
  type                 = string
  description          = "Name of Virtual Network"
}

modules/terraform-azure-module-network/virtual-network/nsg/main.tf

resource "azurerm_network_security_group" "nsg" {
  name                = var.nsg_name
  location            = var.nsg_location
  resource_group_name = var.resource_group_name
}

modules/terraform-azure-module-network/virtual-network/nsg/outputs.tf

output "nsg_id" {
  value = azurerm_network_security_group.nsg.*.id
}

output "nsg_name" {
  value = azurerm_network_security_group.nsg.name
}

modules/terraform-azure-module-network/virtual-network/nsg/variables.tf

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

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

variable "nsg_name" {
  type        = string
  description = "name of nsg group"

subscriptions/boilerplate/providers.tf

# Terraform Block
terraform {
  required_version = ">= 1.0.0"
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = ">= 2.0"
    }
  }
}

# Provider Block
provider "azurerm" {
  subscription_id = "*"
  features {}
}

subscriptions/boilerplate/locals.tf

locals {
  resource_group_name     = "rg-${var.environment}-${var.project_office}-${var.asset_name}"
  vnet_name               = "vn-${var.environment}-${var.project_office}-coreservice"
  location                = var.location
}

subscriptions/boilerplate/resource_groups.tf

module "rg-networkcore" {
  # source = "../../modules/terraform-azure-module-resourcegroup"
  source                  = "git::ssh://git@ssh.dev.azure.com/v3/*/*/terraform-azure-module-resourcegroup"
  resource_group_name     = "rg-d-lxr-network"
  resource_group_location = local.location

}

module "rg-ansiblecontroller" {
  # source = "../../modules/terraform-azure-module-resourcegroup"
  source                  = "git::ssh://git@ssh.dev.azure.com/v3/*/*/terraform-azure-module-resourcegroup"
  resource_group_name     = local.resource_group_name
  resource_group_location = local.location
  tags                    = var.tags

}

subscriptions/boilerplate/networking_vnets.tf

module "vnet-networkcore" {
  source              = "git::ssh://git@ssh.dev.azure.com/v3/*/*/terraform-azure-module-network//virtual-network"
  vnet_name           = local.vnet_name
  vnet_location       = module.rg-networkcore.location
  resource_group_name = module.rg-networkcore.resource_group_name
  vnet_address_space  = var.vnet_address_space
  dns_servers         = var.dns_servers

  depends_on = [module.rg-networkcore]
}

module "subnet-networkcore" {
  source = "git::ssh://git@ssh.dev.azure.com/v3/*/*/terraform-azure-module-network//virtual-network/subnet"
  resource_group_name     = module.rg-networkcore.resource_group_name
  vnet_name               = module.vnet-networkcore.vnet_name
  subnet_name             = var.subnet_name
  subnet_address_prefixes = var.subnet_address_prefixes

  depends_on = [
    module.rg-networkcore,
    module.vnet-networkcore
  ]
}

subscriptions/boilerplate/nsgs.tf

module "nsg-networkcore" {
  # source = "../../modules/terraform-azure-module-resourcegroup"
  source              = "git::ssh://git@ssh.dev.azure.com/v3/*/*/terraform-azure-module-network//virtual-network/nsg"
  nsg_name            = var.nsg_name
  nsg_location        = local.location
  resource_group_name = local.resource_group_name
  #   tags                    = var.tags

  depends_on = [
    module.subnet-networkcore
  ]
}


resource "azurerm_network_security_rule" "example" {
  name                        = "test123"
  priority                    = 100
  direction                   = "Outbound"
  access                      = "Allow"
  protocol                    = "Tcp"
  source_port_range           = "*"
  destination_port_range      = "*"
  source_address_prefix       = "*"
  destination_address_prefix  = "*"
  resource_group_name         = module.rg-networkcore.resource_group_name
  # network_security_group_name = module.nsg-networkcore.nsg_name
  network_security_group_name = module.nsg-networkcore.nsg_name

  depends_on = [
    module.nsg-networkcore
  ]
}

subscriptions/boilerplate/terraform.tfvars

location       = "Australia Southeast"
environment    = "d"
project_office = "lxr"
asset_name     = "ansiblecontroller"

tags = {
  env     = "dev"
  project = "ansible controller"
}

vnet_address_space = ["10.1.0.0/16"]
dns_servers        = ["1.1.1.1", "4.4.4.4"]

subnet_address_prefixes = ["10.1.0.0/24"]

subnet_name = "Ansible"

nsg_name = "nsg-ansible"

subscriptions/boilerplate/variables.tf

   # Resource Group Variables

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

variable "tags" {
  type    = map(any)
  default = {}
}

variable "project_office" {
  description = "Project Office Name"
  type        = string
}

variable "environment" {
  description = "Environment Name"
  type        = string
}

variable "asset_name" {
  description = "Project Office Name"
  type        = string
}

# Vnet & Subnet Variables

variable "vnet_address_space" {
  type        = list(any)
  description = "Address space of Virtual Network"
}

variable "dns_servers" {
  type        = list(any)
  description = "Dns servers for Virtual Network"
}

variable "subnet_address_prefixes" {
  type                 = list(any)
  description          = "Address prefixes of Subnet"
}

variable "subnet_name" {
type        = string
}

# NSG Variables

variable "nsg_name" {
  type        = string
  description = "name of nsg group"
}

Terraform plan/apply 在我使用变量时有效,如下所示:

  network_security_group_name = var.nsg_name

我不确定我引用该模块时出现了什么问题,因为它确实包含一个名为“nsg_name”的属性

希望得到一些帮助 谢谢

抱歉,伙计们,这很容易解决。当我应该 运行 terraform init 时,我正在 运行ning terraform get。菜鸟错误