azureterraformterraform-provider-azureterraform0.12+terraform-template-file

In terraform tfvars file can we put value somewhere else and then simply we can just recall?


Can we put route table value somewhere else eg other tfvars file or varibles/local and then just in the tfvars file. we can call route table route_table =""

networks = {
  umms-security-vnet = {
    resource_group_name = "testrg" #required
    location            = "eastus"                      #required
    addressSpace = [
      "10.229.192.0/20" #required
    ]
    dnsServers = [
      "10.231.18.4",
      "146.189.24.10",
      "172.26.40.125"
    ]
    subnets = {
      GatewaySubnet = {
        addressPrefix = "10.229.192.0/24"
      }
      app-1-subnet = {
        addressPrefix                     = "10.229.198.0/24" #required
        route_table = {                                        #optional
          security-rt = {
            disableBgpRoutePropagation = false
            routes = [
              {
                name             = "default-udr"
                addressPrefix    = "0.0.0.0/0"
                nextHopType      = "VirtualAppliance"
                nextHopIpAddress = "10.231.10.100"
              },
              {
                name             = "network-146.189.0.0-16-udr"
                addressPrefix    = "146.189.0.0/16"
                nextHopType      = "VirtualAppliance"
                nextHopIpAddress = "10.231.10.200"
              }
            ]
          }
        }

Solution

  • Can we put route table value somewhere else eg other tfvars file or varibles/local and then just in the tfvars file. we can call route table route_table

    Yes, you can store the value of the route table in variables instead of directly in the tfvars file. This allows you to define the route table value in variables and reference it in tfvars file.

    Here is the updated Terraform code.

    Main.tf

    provider "azurerm" {
      features {}
    }
    
    resource "azurerm_resource_group" "vnetrg" {
      name     = "VNET-RG"
      location = "eastus"
    }
    
    resource "azurerm_virtual_network" "security-vnet" {
      name                = "security-vnet"
      address_space       = var.address_space
      location            = azurerm_resource_group.vnetrg.location
      resource_group_name = azurerm_resource_group.vnetrg.name
    }
    
    resource "azurerm_subnet" "subnet-without-rt" {
      name                 = "subnet-without-rt"
      address_prefixes     = [var.subnet_prefixes[0]]
      virtual_network_name = azurerm_virtual_network.security-vnet.name
      resource_group_name  = azurerm_resource_group.vnetrg.name
    }
    
    resource "azurerm_subnet" "subnet-with-rt" {
      name                 = "subnet-with-rt"
      address_prefixes     = [var.subnet_prefixes[1]]
      virtual_network_name = azurerm_virtual_network.security-vnet.name
      resource_group_name  = azurerm_resource_group.vnetrg.name
    }
    
    resource "azurerm_subnet_route_table_association" "subnet-with-rt-association" {
      subnet_id      = azurerm_subnet.subnet-with-rt.id
      route_table_id = azurerm_route_table.security-rt.id
    }
    
    resource "azurerm_route_table" "security-rt" {
      name                = "security-rt"
      location            = azurerm_resource_group.vnetrg.location
      resource_group_name = azurerm_resource_group.vnetrg.name
    
      dynamic "route" {
        for_each = var.route_table["security-rt"].routes
        content {
          name             = route.value.name
          address_prefix   = route.value.addressPrefix
          next_hop_type    = route.value.nextHopType
          next_hop_in_ip_address = route.value.nextHopIpAddress
        }
      }
    }
    

    variables.tf

    variable "address_space" {
      description = "Address space for the virtual network"
      type        = list(string)
    }
    
    variable "subnet_prefixes" {
      description = "Prefixes for the subnets"
      type        = list(string)
    }
    variable "route_table" {
      description = "Route table configuration"
      type        = map(object({
        disableBgpRoutePropagation = bool
        routes = list(object({
          name             = string
          addressPrefix    = string
          nextHopType      = string
          nextHopIpAddress = string
        }))
      }))
      default     = {
        security-rt = {
          disableBgpRoutePropagation = false
          routes = [
            {
              name             = "default-udr"
              addressPrefix    = "0.0.0.0/0"
              nextHopType      = "VirtualAppliance"
              nextHopIpAddress = "10.231.10.100"
            },
            {
              name             = "network-146.189.0.0-16-udr"
              addressPrefix    = "146.189.0.0/16"
              nextHopType      = "VirtualAppliance"
              nextHopIpAddress = "10.231.10.200"
            }
          ]
        }
      }
    }
    

    terraform.tfvars

    address_space = ["10.229.192.0/20"]
    
    subnet_prefixes = ["10.229.192.0/24", "10.229.198.0/24"]
    
    route_table = {
      "security-rt" = {
        disableBgpRoutePropagation = false
        routes = [
          {
            name             = "default-udr"
            addressPrefix    = "0.0.0.0/0"
            nextHopType      = "VirtualAppliance"
            nextHopIpAddress = "10.231.10.100"
          },
          {
            name             = "network-146.189.0.0-16-udr"
            addressPrefix    = "146.189.0.0/16"
            nextHopType      = "VirtualAppliance"
            nextHopIpAddress = "10.231.10.200"
          }
        ]
      }
    }
    

    Theroute_table variable is defined in variables.tf block, and its value is referenced in the tfvars file using var.route_table["security-rt"].

    Terraform Apply

    enter image description here

    Once the ran above Terraform code, the resources are created successfully as below.

    enter image description here

    Reference: azurestack_virtual_network