terraform - 错误 : Unsupported attribute. 此对象没有名为 "nsg_name"的属性

标签 terraform

NSG 创建得很好,所以我在 env/dev 和 modules 文件夹中创建并输入 nsg 规则的所有配置

我运行 terraform plan,这是我得到的错误:
错误:不支持的属性
在 nsg_rules.tf 第 6 行,模块“nsgrules_app1”中:
6:nsg_name = 模块.nsg_app1.nsg_name
此对象没有名为“nsg_name”的属性。

我知道我的代码不正确,我只是不确定如何使用映射变量编写 nsg_rule 模块,然后将其附加到我的另一个模块中的 NSG。
任何援助将不胜感激 :)

我的 terraform 相关文件夹结构是:

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
开发/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
}
开发/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/v​​ariables.tf
variable "rg_name" {
  description = "name of resource group"
}

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

variable "nsg_name" {
  description = "name of nsg group"
}
模块/nsg_rule/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_rule/variables.tf
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_app1没有nsg_name属性。这意味着它 不输出 其中有这样一个变量。
要么你必须修改 module.nsg_app1输出此类变量的模块,或在 module.nsgrules_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" }
  # }
}

关于terraform - 错误 : Unsupported attribute. 此对象没有名为 "nsg_name"的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66019421/

相关文章:

amazon-web-services - Sagemaker 项目已成功创建,但没有链接的管道或存储库

azure - Terraform 提示 azurerm_linux_function_app 无法支持 storage_account block ,但文档另有说明

database - 在 AWS RDS 集群实例上使用 Terraform 预置多个逻辑数据库

godep save ./... 似乎不适用于 https ://github. com/hashicorp/terraform

amazon-web-services - terraform 给出错误 : unsupported argument in module when running terraform plan?

azure - 股票.Client#GetProperties : Failure sending request: StatusCode=0 -- Original Error: context deadline exceeded

amazon-ec2 - 无法保存状态 : open terraform. tfstate:权限被拒绝

sql-server - Azure Terraform 将 bacpac 导入 SQL Server 并禁用公共(public)网络访问

Terraform:如何从对象列表创建 API 网关端点和方法?

azure - 如何生成多个名称/使用结果输出?