2
votes

I have two modules.

  1. one is called module-azure-vnet
  2. second is called module-azure-vm-instance-zabbix-proxy

I want to pass Outputs from module module-azure-vnet to module module-azure-vm-instance-zabbix-proxy. First module is working properly, but second needs inputs for subnet, security group and route.

main.tf

module "module-azure-vnet-dtap" {
  source = "/Users/username/project/module-azure-vnet-dtap"
}

variable "subnets_id_wan" {}

module "module-azure-vm-instance-zabbix-proxy" {
  source = "/Users/username/project/module-azure-vm-instance-zabbix-proxy"
  azurerm_subnet = "${module.module-azure-vnet-dtap.azurerm_subnet.wan.id}"
}

Error: resource 'azurerm_network_interface.no_public_ip' config: unknown resource 'azurerm_subnet.wan' referenced in variable azurerm_subnet.wan.id

Error: resource 'azurerm_network_security_rule.security_rule_default' config: unknown resource 'azurerm_network_security_group.wan' referenced in variable azurerm_network_security_group.wan.name

Error: module "module-azure-vm-instance-zabbix-proxy": "azurerm_subnet" is not a valid argument

In module module-azure-vnet-dtap

resource "azurerm_subnet" "wan" {
  count                     = "${var.enable_wan_subnet ? 1 : 0}"
  provider                  = "azurerm.base"
  name                      = "${format("%s-%s-%s", var.environment_name, "WAN", "Subnet")}"
  virtual_network_name      = "${azurerm_virtual_network.this.name}"
  resource_group_name       = "${azurerm_resource_group.this.name}"
  address_prefix            = "${cidrsubnet(var.cidr_block,5,count.index)}"
  route_table_id            = "${azurerm_route_table.wan.id}"
  network_security_group_id = "${azurerm_network_security_group.wan.id}"
}

resource "azurerm_network_security_group" "wan" {
  count               = "${var.enable_wan_subnet ? 1 : 0}"
  provider            = "azurerm.base"
  name                = "${format("%s-%s", var.environment_name, "WAN-Subnet-Security-Group")}"
  location            = "${azurerm_resource_group.this.location}"
  resource_group_name = "${azurerm_resource_group.this.name}"
}

output "subnets_id_wan" {
  value = "${azurerm_subnet.wan.*.id}"

  depends_on = [
    "azurerm_subnet.wan",
  ]
}
2
Try commenting out the module "module-azure-vm-instance-zabbix-proxy" block and then run terraform init && terraform refresh && terraform show | less and look at all the output names and values to see if the module is outputting the correct value or if you're passing in the correct output to the zabbix proxy module.SomeGuyOnAComputer

2 Answers

2
votes

you should reference output like this:

"${module.MODULE_NAME.OUTPUT_NAME}"

in your case it should be:

"${module.module-azure-vnet-dtap.subnets_id_wan}"

also, you can only access outputs, not resources from the module (afaik).

1
votes

I've found solution myself, here is the whole structure what is needed in order to connect two modules.

set variables in VNET module example:

variable "resource_group_name" {
      default     = "default_resource_group"
      }

      variable "region_name" {
      default     = "ukwest"
      }

declare outputs:

output "security_groups_id_wan" {
      value = "${element(concat(azurerm_network_security_group.wan.*.id, list("")),0)}"

        depends_on = [
          "azurerm_subnet.wan",
          ]
      }

you need variables in zabbix proxy module to receive values>

variable "resource_group_name" {
        default = ""
        }
        variable "resource_group_location" {
        default = ""
        } 

and in external folder where is the main.tf for modules>

module "module-azure-vm-instance-zabbix-proxy" {
        source = "/Users/username/project/module-azure-vm-instance-zabbix-proxy"
        resource_group_name = "${module.module-azure-vnet-dtap.resource_group_name}"
        resource_group_location = "${module.module-azure-vnet-dtap.resource_group_location}"
        subnets_id_wan = "${module.module-azure-vnet-dtap.subnets_id_wan}"
        security_groups_name = "${module.module-azure-vnet-dtap.security_groups_name_dmz}"
        environment_name = "${module.module-azure-vnet-dtap.environment_name}"
      }

      module module-azure-vnet-dtap {
        source = "/Users/username/project/module-azure-vnet-dtap"
      }