0

Terraform 0.12.8|Azure Provider 1.33.1|Linux

I have a network module as shown below-

main.tf:

resource "azurerm_virtual_network" "vnet" {
  name                = "${var.vnet_name}"
  location            = "${var.location}"
  address_space       = ["${var.address_space}"]
  resource_group_name = "${var.resource_group_name}"
  dns_servers         = "${var.dns_servers}"
  tags                = "${var.tags}"
}


resource "azurerm_subnet" "subnet" {
  name                      = "${var.subnet_names[count.index]}"
  virtual_network_name      = "${azurerm_virtual_network.vnet.name}"
  resource_group_name       = "${var.resource_group_name}"
  address_prefix            = "${var.subnet_prefixes[count.index]}"
  network_security_group_id = 
"${lookup(var.nsg_ids,var.subnet_names[count.index],"")}"
  count                     = "${length(var.subnet_names)}"
}

variables.tf:

variable "vnet_name" {
  description = "Name of the vnet to create"
}


variable "resource_group_name" {
  description = "Default resource group name that the network will be 
created in."
}


variable "location" {
  description = "The location/region where the core network will be 
created. The full list of Azure regions can be found at 
https://azure.microsoft.com/regions"
}


variable "address_space" {
  description = "The address space that is used by the virtual network."
  default     = "10.0.0.0/16"
}


# If no values specified, this defaults to Azure DNS 
variable "dns_servers" {
  description = "The DNS servers to be used with vNet."
  default     = []
}


variable "subnet_prefixes" {
  description = "The address prefix to use for the subnet."
  default     = ["10.0.1.0/24"]
}


variable "subnet_names" {
  description = "A list of public subnets inside the vNet."
  default     = ["subnet1", "subnet2", "subnet3"]
}


variable "nsg_ids" {
  description = "A map of subnet name to Network Security Group IDs"
  type        = "map"


  default = {
    subnet1 = "nsgid1"
    subnet3 = "nsgid3"
  }
}


variable "tags" {
  description = "The tags to associate with your network and subnets."
  type        = "map"


  default = {
    tag1 = ""
    tag2 = ""
  }
}

And I have defined the network as shown below:

provider "azurerm" {
  version = "~> 1.27"
}
resource "azurerm_resource_group" "rg" {
        name = "net-rg"
        location = "canadacentral"
}


module "vnet" {
  source              = "./module"
  resource_group_name = azurerm_resource_group.rg.name
  location            = "canadacentral"
  vnet_name           = "${var.environment_prefix}-vnet"
  address_space       = "10.0.0.0/16"
  subnet_prefixes     = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
  subnet_names        = ["web", "app2", "db"]


  tags = {
    environment = "dev"
    costcenter  = "it"
  }
}

For the first time, if I run 'terraform apply' it creates the resource group, virtual network, and associated subnets. However, if try to change the subnet name, for example: 'app2' to 'app' it returns the following error.

Error: Error Creating/Updating Subnet "app" (Virtual Network "d-vnet" / 
Resource Group "net-rg"): network.SubnetsClient#CreateOrUpdate: Failure 
sending request: StatusCode=400 -- Original Error: 
Code="NetcfgInvalidSubnet" Message="Subnet 'app' is not valid in virtual 
network 'd-vnet'." Details=[]

Plan:

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  ~ update in-place
-/+ destroy and then create replacement


Terraform will perform the following actions:


  # module.vnet.azurerm_subnet.subnet[1] must be replaced
-/+ resource "azurerm_subnet" "subnet" {
        address_prefix       = "10.0.2.0/24"
      ~ id                   = 
"/subscriptions/<>/resourceGroups/net-rg/providers/Microsoft.Network/virtualNetworks/d-vnet/subnets/app2" 
-> (known after apply)
      ~ ip_configurations    = [] -> (known after apply)
      ~ name                 = "app2" -> "app" # forces replacement
        resource_group_name  = "net-rg"
      - service_endpoints    = [] -> null
        virtual_network_name = "d-vnet"
    }


  # module.vnet.azurerm_virtual_network.vnet will be updated in-place
  ~ resource "azurerm_virtual_network" "vnet" {
        address_space       = [
            "10.0.0.0/16",
        ]
        dns_servers         = []
        id                  = 
"/subscriptions/<>/resourceGroups/net-rg/providers/Microsoft.Network/virtualNetworks/d-vnet"
        location            = "canadacentral"
        name                = "d-vnet"
        resource_group_name = "net-rg"
      ~ tags                = {
          - "Creator"     = "Deepak" -> null
            "costcenter"  = "it"
            "environment" = "dev"
        }


        subnet {
            address_prefix = "10.0.1.0/24"
            id             = 
"/subscriptions/<>/resourceGroups/net-rg/providers/Microsoft.Network/virtualNetworks/d-vnet/subnets/web"
            name           = "web"
        }
        subnet {
            address_prefix = "10.0.2.0/24"
            id             = 
"/subscriptions/<>/resourceGroups/net-rg/providers/Microsoft.Network/virtualNetworks/d-vnet/subnets/app2"
            name           = "app2"
        }
        subnet {
            address_prefix = "10.0.3.0/24"
            id             = 
"/subscriptions/<>/resourceGroups/net-rg/providers/Microsoft.Network/virtualNetworks/d-vnet/subnets/db"
            name           = "db"
        }
    }


Plan: 1 to add, 1 to change, 1 to destroy.

For the subscription where the modification takes place, the Terraform generates the following plan for the same changes.

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
-/+ destroy and then create replacement

Terraform will perform the following actions:

  # module.vnet.azurerm_subnet.subnet[0] must be replaced
-/+ resource "azurerm_subnet" "subnet" {
        address_prefix       = "10.0.1.0/24"
      ~ id                   = "/subscriptions/<>/resourceGroups/net-rg/providers/Microsoft.Network/virtualNetworks/d-vnet/subnets/web" -> (known after apply)
      ~ ip_configurations    = [] -> (known after apply)
      ~ name                 = "app2" -> "app" # forces replacement
        resource_group_name  = "net-rg"
      - service_endpoints    = [] -> null
        virtual_network_name = "d-vnet"
    }

Plan: 1 to add, 0 to change, 1 to destroy.

The strange thing is that I can change it if I change the azure subscription to another one on which I am the owner of the directory. The subscription on which this doesn't work, I am the owner of the subscription but a member of the directory. I have authenticated to azure using 'az login' following 'az account set'. What could be an issue here?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.