0
votes

terraform version: v0.12.24 provider.azurerm v2.49.0

I am trying to build a vnet by having all the line items as variables and then populate them in the variables.auto.tfvars file.

for my main.tf section:

resource "azurerm_virtual_network" "vnet" {
  name                   = var.vnet_name
  resource_group_name    = var.rg_grp_name
  address_space          = var.vnet_cidr
  location               = var.region_location #match RG location
  dns_servers            = var.vnet_dns_servers




#tags stuff here
}
Resource "azurerm_subnet" "subnet" {
  for_each               = var.subnets
  name                   = lookup(each.value, "name")
  resource_group_name    = var.rg_grp_name
  virtual_network_name   = azurerm_virtual_network.vnet.name
  address_prefix         = lookup(each.value, "cidr")
  
}

and in my variables.tf, which i am really unsure about;

###vnet section###################
#variable for defining what region
variable "name" {
  type = string
}
#variable for defining what region
variable "resource_group_name" {
  type = string
}
#variable for defining what region
variable "vnet_cidr" {
  description = "address space"
  type = list
}

#variable for defining what region
variable "region_location" {
  type = string
}
#variable for defining what region
variable "vnet_dns_servers" {
  type = list
}
#variable for defining what region
variable "subnets" {
   type = map(object({
   name = string
   cidr = string

}))
}
##end subnet block


#variable for defining what region
variable "azregion" {
  type = string
}

#####end vnet section#############

I dont have the variables.auto.tfvars section yet...

whats the best way to go about this?

Thanks

1

1 Answers

0
votes

We can create the input variables by variable blocks but reference them as attributes on an object named var. We can access its value from within expressions as var.<NAME>, where <NAME> matches the label given in the declaration block.

So, according to the expressions in your main.tf file, the variable declaration block shoule be like this:

variable "vnet_name" {
  description = "Name of the vnet to create"
  type        = string
  # default     = "acctvnet"
}

variable "rg_grp_name" {
  description = "Name of the resource group to be imported."
  type        = string
  # default = "testrg"
}

variable "vnet_cidr" {
  type        = list(string)
  description = "The address space that is used by the virtual network."
  # default     = ["10.0.0.0/16"]
}

variable "region_location" {
  type  = string
  # default = "eastus"
}


# If no values specified, this defaults to Azure DNS 
variable "vnet_dns_servers" {
  description = "The DNS servers to be used with vNet."
  type        = list(string)
  # default     = []
}

variable "subnets" {
   type = map(object({
   name = string
   cidr = string

}))
# default = {
#   subnet1 = {
#     name = "subnet1"
#     cidr = "10.0.1.0/24"
#   },
#   subnet2 = {
#     name = "subnet2"
#     cidr = "10.0.2.0/24"
#   }
# }
}

If you would like to make terraform automatically load a number of variable definitions files, you can input the default values for that variable as the expression variable_name = default_value in the variables.auto.tfvarsfile .

vnet_name = "acctvnet"
rg_grp_name = "testrg"
vnet_cidr = ["10.0.0.0/16"]
region_location = "eastus"
vnet_dns_servers = []

subnets = {
  subnet1 = {
    name = "subnet1"
    cidr = "10.0.1.0/24"
  },
  subnet2 = {
    name = "subnet2"
    cidr = "10.0.2.0/24"
  }
}

You can check the source code in the vnet module with terraform for more details.