Terraform 错误:不支持的属性。此对象没有名为“的属性”;nsg“U名称”;

Terraform 错误:不支持的属性。此对象没有名为“的属性”;nsg“U名称”;,terraform,Terraform,NSG创建得很好,因此我在env/dev和modules文件夹中创建并输入NSG规则的所有配置 我运行terraform plan,这是我得到的错误: 错误:不支持的属性 在nsg_rules.tf第6行的模块“nsgrules_app1”中: 6:nsg\u name=module.nsg\u app1.nsg\u name 此对象没有名为“nsg_name”的属性 我知道我的代码不正确,只是不知道如何使用map变量编写nsg_规则模块,然后将其附加到另一个模块中的nsg。 如有任何帮助,

NSG创建得很好,因此我在env/dev和modules文件夹中创建并输入NSG规则的所有配置
我运行terraform plan,这是我得到的错误:

错误:不支持的属性

在nsg_rules.tf第6行的模块“nsgrules_app1”中:

6:nsg\u name=module.nsg\u app1.nsg\u name

此对象没有名为“nsg_name”的属性


我知道我的代码不正确,只是不知道如何使用map变量编写nsg_规则模块,然后将其附加到另一个模块中的nsg。 如有任何帮助,将不胜感激:)

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

dev
    |_ backend.tf
    |_ outputs.tf
    |_ provider.tf
    |_ resource_groups.tf
    |_ nsg.tf
    |_ nsg_rules.tf
    |_ storage.tf
    |_ subnets.tf
    |_ variables.tf
    |_ vnets.tf
    |_ vms_lin.tf

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

|_ nsg_rules
          |_ outputs.tf
          |_ variables.tf
          |_ main.tf

|_ 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
dev/nsg.tf

module "nsg_app1" {
  source                    = "git::ssh://git@ssh.dev.azure.com/v3/myorg/my_code/terraform_modules//nsg"
  nsg_name                  = "nsg-ansible"
  rg_name                   = module.rg_app1.rg_name
  location                  = module.rg_app1.rg_location
}
dev/nsg_rules.tf

module "nsgrules_app1" {
  source                    = "git::ssh://git@ssh.dev.azure.com/v3/myorg/my_code/terraform_modules//nsg_rule"
  rg_name                   = module.rg_app1.rg_name
  nsg_name                  = module.nsg_app1.nsg_name
  # rules_map                 = var.rules_map     
  # rules_map = {
  #   http_inbound  = { priority = 150, direction = "Inbound", access = "Allow", protocol = "TCP", destination_port_range = "80" },
  #   https_inbound = { priority = 151, direction = "Inbound", access = "Allow", protocol = "TCP", destination_port_range = "443" }
  # }
}
模块/nsg/main.tf

resource "azurerm_network_security_group" "nsg" {
  name                = var.nsg_name
  location            = var.location
  resource_group_name = var.rg_name
}
模块/nsg/variables.tf

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

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

variable "nsg_name" {
  description = "name of nsg group"
}
variable "rg_name" {
  description = "name of resource group"
}

variable "default_ip_whitelist" {
  description = "List of IPs to whitelist on all RDP | SSH enabled NSG rules."
  default     = []
}

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

variable "rules_map" {
  type    = map
  default = {
        rule1 = {priority = 105, direction = "Inbound", access = "Allow", protocol = "TCP", source_port_range = "*", destination_port_range = "*",source_address_prefix = "*", destination_address_prefix = "*"  } ,
        rule2 = {priority = 105, direction = "Outbound", access = "Deny", protocol = "TCP", source_port_range = "*", destination_port_range = "*",source_address_prefix = "*", destination_address_prefix = "*"  }    
    }

}
模块/nsg_规则/main.tf

resource "azurerm_network_security_rule" "nsg-rule-rdp" {
    
  name                        = "RDP"
  priority                    = "105"
  direction                   = "Inbound"
  access                      = "Allow"
  protocol                    = "TCP"
  source_port_range           = "*"
  destination_port_range      = "3389"
  source_address_prefixes     = var.default_ip_whitelist
  destination_address_prefix  = "*"
  resource_group_name         = var.rg_name
  network_security_group_name = var.nsg_name
}
模块/nsg_规则/variables.tf

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

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

variable "nsg_name" {
  description = "name of nsg group"
}
variable "rg_name" {
  description = "name of resource group"
}

variable "default_ip_whitelist" {
  description = "List of IPs to whitelist on all RDP | SSH enabled NSG rules."
  default     = []
}

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

variable "rules_map" {
  type    = map
  default = {
        rule1 = {priority = 105, direction = "Inbound", access = "Allow", protocol = "TCP", source_port_range = "*", destination_port_range = "*",source_address_prefix = "*", destination_address_prefix = "*"  } ,
        rule2 = {priority = 105, direction = "Outbound", access = "Deny", protocol = "TCP", source_port_range = "*", destination_port_range = "*",source_address_prefix = "*", destination_address_prefix = "*"  }    
    }

}

您正在使用的模块
module.nsg\u app1
没有
nsg\u name
属性。这意味着它不会在其内部输出这样一个变量

您必须修改
模块.nsg\u app1
模块以输出此类变量,或者在
模块.nsgrules\u app1
中硬编码名称:

module "nsgrules_app1" {
  source                    = "git::ssh://git@ssh.dev.azure.com/v3/myorg/my_code/terraform_modules//nsg_rule"
  rg_name                   = module.rg_app1.rg_name
  nsg_name                  = "nsg-ansible"
  # rules_map                 = var.rules_map     
  # rules_map = {
  #   http_inbound  = { priority = 150, direction = "Inbound", access = "Allow", protocol = "TCP", destination_port_range = "80" },
  #   https_inbound = { priority = 151, direction = "Inbound", access = "Allow", protocol = "TCP", destination_port_range = "443" }
  # }
}

非常感谢Marcin,当我硬编码它的值时。输出仍然有点让我困惑。想要尽可能地自动化这个,你能帮我写出来吗:)@Cyborganizer你可以创建一个变量
nsg\u name
,如果你需要的话可以使用
var.nsg\u name
。感谢buddy对值进行了硬编码,稍后会创建变量on@Cyborganizer没问题。如果答案有帮助,我们将不胜感激。