2
votes

main.tf wrote:

terraform {
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = ">= 2.26"
    }
  }
}

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "rg" {
  name     = "Product-RG"
  location = var.location
}

resource "azurerm_virtual_network" "vnet" {
  resource_group_name = azurerm_resource_group.rg.name
  name          = "Product-VNet"
  address_space = [lookup(var.vnetAddress, var.location)]
  location      = var.location

  subnet {
    name           = "Web-Sub1"
    address_prefix = ["${lookup(var.subnetAddress[var.location], "web1")}"]
  }
  subnet {
    name           = "Web-Sub2"
    address_prefix = [lookup(var.subnetAddress[var.location], "web2")]
  }

In Web-Sub1, i originally brought address_prefix like Web-Sub2, but now i'm trying like address_prefix on Web-Sub1 after the error occurred.

An error has occurred as below.

Error: Incorrect attribute value type

on main.tf line 27, in resource "azurerm_virtual_network" "vnet": 27: address_prefix = ["${lookup(var.subnetAddress[var.location], "web1")}"]

Inappropriate value for attribute "address_prefix": string required.

Error: Incorrect attribute value type

on main.tf line 31, in resource "azurerm_virtual_network" "vnet": 31: address_prefix = [lookup(var.subnetAddress[var.location], "web2")]

Inappropriate value for attribute "address_prefix": string required.

variable.tf wrote:

variable "location" {}

variable "vnetAddress" {
  type = map

  default = {
    westus = "192.168.1.0/27"
    eastus = "192.168.11.0/27"
  }
}

variable "subnetAddress" {
  type = map

  default = {
    westus = {
      web1 = "192.168.1.0/27"
      web2 = "192.168.1.32/27"
      was1 = "192.168.1.64/27"
      was2 = "192.168.1.96/27"
      db1 = "192.168.1.128/27"
      db2 = "192.168.1.160/27"
    }

    eastus = {
      web1 = "192.168.11.0/27"
      web2 = "192.168.11.32/27"
      was1 = "192.168.11.64/27"
      was2 = "192.168.11.96/27"
      db1 = "192.168.11.128/27"
      db2 = "192.168.11.160/27"
    }
  }
}

I wonder why there is an error that needs to be written in string format and why I can't bring the data.

1

1 Answers

2
votes

You are almost there, just that address_prefix argument needs to be a string and you are passing a list of strings address_prefix = [lookup(var.subnetAddress[var.location], "web2")]

  subnet {
    name           = "Web-Sub1"
    address_prefix = lookup(var.subnetAddress[var.location], "web1")
  }
  subnet {
    name           = "Web-Sub2"
    address_prefix = lookup(var.subnetAddress[var.location], "web2")
  }

This should work.

Refer azurerm_virtual_network resource, address_prefix is passed as a string rather a list of strings.