8

NSG gets created fine, so then I create and enter all the config for the nsg rules in my env/dev and modules folders
I run terraform plan, this is the error I am getting:

Error: Unsupported attribute

  on nsg_rules.tf line 6, in module "nsgrules_app1":

   6:   nsg_name                  = module.nsg_app1.nsg_name


This object does not have an attribute named "nsg_name".

I know my code is incorrect, i'm just not sure how to write up the nsg_rule module by using map variables and then attach it to the NSG in my other module.

Any assistance would be appreciated :)

my terraform relevant folder structure is:

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" }
  # }
}

modules/nsg/main.tf

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

modules/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"
}

modules/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
}

modules/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 = "*"  }    
    }

}
devwebcl
  • 2,866
  • 3
  • 27
  • 46
Cyborganizer
  • 161
  • 1
  • 5
  • 12

1 Answers1

10

The module that you are using module.nsg_app1 does not have nsg_name attribute. This means that it does not output such a variable in its.

Either you have to modify module.nsg_app1 module to output such variable, or in module.nsgrules_app1 hard-code the name:

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
  • 215,873
  • 14
  • 235
  • 294
  • Thanks so much Marcin, when I hard code the value it works. Outputs are still little confusing to me. Want to automate this as much as possible, are you able to write it up for me :) – Cyborganizer Feb 03 '21 at 01:07
  • @Cyborganizer You can create a variable `nsg_name` and use `var.nsg_name` if places when you need it. – Marcin Feb 03 '21 at 01:09
  • Thanks buddy hard coding the value worked, will create the variable little later on – Cyborganizer Feb 03 '21 at 03:40