Not able to create Azure resources

I am learning terraform and i want to deploy linux vm on azure using terraform. When running terraform apply only some resources deploy . I am wondering about why it is doing .

Below is the directory structure of my configurations

[aniket@localhost Azure-IAC]$ pwd
/home/aniket/Azure-IAC
[aniket@localhost Azure-IAC]$ tree
.
├── main.tf
├── main.tfplan
├── modules
│   ├── network
│   │   └── app-nic.tf
│   ├── resource
│   │   └── resource_group.tf
│   ├── storage
│   │   └── storage.tf
│   └── vm
│       └── linux_vm.tf
├── terraform.log
├── terraform.tfstate
└── terraform.tfstate.backup

5 directories, 9 files

Output of each tf file
main.tf

terraform {
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = "4.26.0"
    }
  }
}

provider "azurerm" {
   features {}
  subscription_id   = "xxxxxxxxxxxxxxx"
  tenant_id         = "xxxxxxxxxxxxxxx"
  client_id         = "xxxxxxxxxxxxxxx"
  client_secret     = "xxxxxxxxxxxxxxx"
}
module "resource" {
  source="./modules/resource"
}
module "network" {
  source="./modules/network"
}
module "storage" {
  source="./modules/storage"
}

module "virtual_machine" {
  source="./modules/vm"
}

modules/resource/resource_group.tf

locals {
    location="Central India"
    resource_group_name="app_grp"
}
output "app-region"{
value=local.location
}
resource "azurerm_resource_group" "app_resource_group" {
  name     = local.resource_group_name
 location = local.location
}

output "resource_group"{
  value=azurerm_resource_group.app_resource_group

modules/network/app-nic.tf

module "test" {
    source="/home/aniket/Azure-IAC/modules/resource"
    #source="./resource/"
}

resource "azurerm_virtual_network" "app-network" {
  name                = "app-vnet"
  address_space       = ["10.0.0.0/16"]
  location            = module.test.app-region
  resource_group_name = "app_grp"
  depends_on = [module.test.resource_group]
}
resource "azurerm_subnet" "subA" {
  name                 = "subnetA"
  resource_group_name  = "app_grp"
  virtual_network_name = "app-vnet"
  address_prefixes     = ["10.0.2.0/24"]
  depends_on = [module.test.resource_group,azurerm_virtual_network.app-network,module.test.resource_group]
}

resource "azurerm_network_interface" "app-network-interface" {
  name                = "app-nic"
  location            = module.test.app-region
  resource_group_name = "app_grp"

  ip_configuration {
    name                          = "internal"
    subnet_id                     = azurerm_subnet.subA.id
    private_ip_address_allocation = "Dynamic"
    public_ip_address_id=azurerm_public_ip.app-pub-ip.id
  }
  depends_on=[module.test.resource_group]
}

resource "azurerm_public_ip" "app-pub-ip" {
  name                = "pub-ip"
  resource_group_name = "app_grp"
  location            = module.test.app-region
  allocation_method   = "Static"
  depends_on =[module.test.resource_group]
}
output "network-interface" {
value=azurerm_network_interface.app-network-interface
output "virtual-network" {
value=azurerm_virtual_network.app-network
}


output "subnets" {
value=azurerm_subnet.subA
}

modules/vm/linux_vm

resource "azurerm_network_security_group" "nsg" {
  name                = "app-nsg"
  location            = module.test.app-region
  resource_group_name = "app_grp"

  security_rule {
    name                       = "test123"
    priority                   = 100
    direction                  = "Inbound"
    access                     = "Allow"
    protocol                   = "Tcp"
    source_port_range          = "*"
    destination_port_range     = "22"
    source_address_prefix      = "*"
    destination_address_prefix = "*"
  }
depends_on = [module.test1.app-network,
              module.test1.network-interface
]

}

resource "azurerm_subnet_network_security_group_association" "nsglink" {
  subnet_id                 = module.test1.subnets.id
  network_security_group_id = azurerm_network_security_group.nsg.id
}

resource "azurerm_linux_virtual_machine" "linux-vm" {
  name                = "ElasticVm"
  resource_group_name = "app_grp"
  location            = module.test.app-region
  size                = "Standard_DS1_v2"
  admin_username      = "aniket"
  network_interface_ids = [
    module.test1.network-interface.id
  ]
depends_on = [module.test1.app-network,
              module.test1.network-interface
]
  admin_ssh_key {
    username   = "aniket"
    public_key = file("/home/aniket/myedprivatekey.pub")
  }

  os_disk {
    caching              = "ReadWrite"
    storage_account_type = "Standard_LRS"
  }

  source_image_reference {
    publisher = "RedHat"
    offer     = "RHEL"
    sku       = "87-gen2"
    version   = "latest"
  }
}

When running terraform plan -out main.tfplan it prints 15 plan to add


  # module.network.azurerm_network_interface.app-network-interface will be created
  + resource "azurerm_network_interface" "app-network-interface" {
      + accelerated_networking_enabled = false
      + applied_dns_servers            = (known after apply)
      + id                             = (known after apply)
      + internal_domain_name_suffix    = (known after apply)
      + ip_forwarding_enabled          = false
      + location                       = "centralindia"
      + mac_address                    = (known after apply)
      + name                           = "app-nic"
      + private_ip_address             = (known after apply)
      + private_ip_addresses           = (known after apply)
      + resource_group_name            = "app_grp"
      + virtual_machine_id             = (known after apply)

      + ip_configuration {
          + gateway_load_balancer_frontend_ip_configuration_id = (known after apply)
          + name                                               = "internal"
          + primary                                            = (known after apply)
          + private_ip_address                                 = (known after apply)
          + private_ip_address_allocation                      = "Dynamic"
          + private_ip_address_version                         = "IPv4"
          + public_ip_address_id                               = (known after apply)
          + subnet_id                                          = (known after apply)
        }
    }

  # module.network.azurerm_public_ip.app-pub-ip will be created
  + resource "azurerm_public_ip" "app-pub-ip" {
      + allocation_method       = "Static"
      + ddos_protection_mode    = "VirtualNetworkInherited"
      + fqdn                    = (known after apply)
      + id                      = (known after apply)
      + idle_timeout_in_minutes = 4
      + ip_address              = (known after apply)
      + ip_version              = "IPv4"
      + location                = "centralindia"
      + name                    = "pub-ip"
      + resource_group_name     = "app_grp"
      + sku                     = "Standard"
      + sku_tier                = "Regional"
    }

  # module.network.azurerm_subnet.subA will be created
  + resource "azurerm_subnet" "subA" {
      + address_prefixes                              = [
          + "10.0.2.0/24",
        ]
      + default_outbound_access_enabled               = true
      + id                                            = (known after apply)
      + name                                          = "subnetA"
      + private_endpoint_network_policies             = "Disabled"
      + private_link_service_network_policies_enabled = true
      + resource_group_name                           = "app_grp"
      + virtual_network_name                          = "app-vnet"
    }

  # module.network.azurerm_virtual_network.app-network will be created
  + resource "azurerm_virtual_network" "app-network" {
      + address_space                  = [
          + "10.0.0.0/16",
        ]
      + dns_servers                    = (known after apply)
      + guid                           = (known after apply)
      + id                             = (known after apply)
      + location                       = "centralindia"
      + name                           = "app-vnet"
      + private_endpoint_vnet_policies = "Disabled"
      + resource_group_name            = "app_grp"
      + subnet                         = (known after apply)
    }

  # module.resource.azurerm_resource_group.app_resource_group will be created
  + resource "azurerm_resource_group" "app_resource_group" {
      + id       = (known after apply)
      + location = "centralindia"
      + name     = "app_grp"
    }

  # module.virtual_machine.azurerm_linux_virtual_machine.linux-vm will be created
  + resource "azurerm_linux_virtual_machine" "linux-vm" {
      + admin_username                                         = "aniket"
      + allow_extension_operations                             = true
      + bypass_platform_safety_checks_on_user_schedule_enabled = false
      + computer_name                                          = (known after apply)
      + disable_password_authentication                        = true
      + disk_controller_type                                   = (known after apply)
      + extensions_time_budget                                 = "PT1H30M"
      + id                                                     = (known after apply)
      + location                                               = "centralindia"
      + max_bid_price                                          = -1
      + name                                                   = "ElasticVm"
      + network_interface_ids                                  = (known after apply)
      + patch_assessment_mode                                  = "ImageDefault"
      + patch_mode                                             = "ImageDefault"
      + platform_fault_domain                                  = -1
      + priority                                               = "Regular"
      + private_ip_address                                     = (known after apply)
      + private_ip_addresses                                   = (known after apply)
      + provision_vm_agent                                     = true
      + public_ip_address                                      = (known after apply)
      + public_ip_addresses                                    = (known after apply)
      + resource_group_name                                    = "app_grp"
      + size                                                   = "Standard_DS1_v2"
      + virtual_machine_id                                     = (known after apply)
      + vm_agent_platform_updates_enabled                      = (known after apply)

      + admin_ssh_key {
          + public_key = <<-EOT
                ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC0qgSFcC/BIXLP6/xq2KVCjJrYV/q9bGpm4YBcggQoDqlsTIGsM5mRNnZ9WOJZOc78oXcrK6u3azqSY5tEDkJX6Okdtq6dWDb5YWb9bBBkCwqsXcrR+pI8nlTj5ekQl8UqErgHkd81knZlY3iF/mTS/w0Ti5s72sYPzU/brKg7UFPMD3LHVB9JdPpuNRn87PwbgVh3ODTlRDsDowUyjO6hdygz9ps4/JxZSUkiYMg9pNClVXm+DNzG8NKLhBb724zUhSCRAGcTqhvSnxYD0N82Ukj5UA4hC3b2nxgULZDAYXzB+ZGjxM8euV1hQ++7Dg5diae7e3GVDEPMKo16+FUn0oGfH7JX0mbp7yjakNjTt7mlz69otqdBxTjlaed7tzp5jj+pnJPF8e6o3prBJo/azLnK34ZzUyxvi4tbBdAidjSPAgN2buvmPr7qEtJtRwmib9JxBnIq/xCi4XlJ21eY58o3JMS7zwAzkgBRuyq1Ac0OX8wxLONVfD8+50qREr0= aniket@Elastic
            EOT
          + username   = "aniket"
        }

      + os_disk {
          + caching                   = "ReadWrite"
          + disk_size_gb              = (known after apply)
          + id                        = (known after apply)
          + name                      = (known after apply)
          + storage_account_type      = "Standard_LRS"
          + write_accelerator_enabled = false
        }

      + source_image_reference {
          + offer     = "RHEL"
          + publisher = "RedHat"
          + sku       = "87-gen2"
          + version   = "latest"
        }

      + termination_notification (known after apply)
    }

  # module.virtual_machine.azurerm_network_security_group.nsg will be created
  + resource "azurerm_network_security_group" "nsg" {
      + id                  = (known after apply)
      + location            = "centralindia"
      + name                = "app-nsg"
      + resource_group_name = "app_grp"
      + security_rule       = [
          + {
              + access                                     = "Allow"
              + destination_address_prefix                 = "*"
              + destination_address_prefixes               = []
              + destination_application_security_group_ids = []
              + destination_port_range                     = "22"
              + destination_port_ranges                    = []
              + direction                                  = "Inbound"
              + name                                       = "test123"
              + priority                                   = 100
              + protocol                                   = "Tcp"
              + source_address_prefix                      = "*"
              + source_address_prefixes                    = []
              + source_application_security_group_ids      = []
              + source_port_range                          = "*"
              + source_port_ranges                         = []
                # (1 unchanged attribute hidden)
            },
        ]
    }

  # module.virtual_machine.azurerm_subnet_network_security_group_association.nsglink will be created
  + resource "azurerm_subnet_network_security_group_association" "nsglink" {
      + id                        = (known after apply)
      + network_security_group_id = (known after apply)
      + subnet_id                 = (known after apply)
    }

  # module.network.module.test.azurerm_resource_group.app_resource_group will be created
  + resource "azurerm_resource_group" "app_resource_group" {
      + id       = (known after apply)
      + location = "centralindia"
      + name     = "app_grp"
    }

  # module.virtual_machine.module.test.azurerm_resource_group.app_resource_group will be created
  + resource "azurerm_resource_group" "app_resource_group" {
      + id       = (known after apply)
      + location = "centralindia"
      + name     = "app_grp"
    }

  # module.virtual_machine.module.test1.azurerm_network_interface.app-network-interface will be created
  + resource "azurerm_network_interface" "app-network-interface" {
      + accelerated_networking_enabled = false
      + applied_dns_servers            = (known after apply)
      + id                             = (known after apply)
      + internal_domain_name_suffix    = (known after apply)
      + ip_forwarding_enabled          = false
      + location                       = "centralindia"
      + mac_address                    = (known after apply)
      + name                           = "app-nic"
      + private_ip_address             = (known after apply)
      + private_ip_addresses           = (known after apply)
      + resource_group_name            = "app_grp"
      + virtual_machine_id             = (known after apply)

      + ip_configuration {
          + gateway_load_balancer_frontend_ip_configuration_id = (known after apply)
          + name                                               = "internal"
          + primary                                            = (known after apply)
          + private_ip_address                                 = (known after apply)
          + private_ip_address_allocation                      = "Dynamic"
          + private_ip_address_version                         = "IPv4"
          + public_ip_address_id                               = (known after apply)
          + subnet_id                                          = (known after apply)
        }
    }

  # module.virtual_machine.module.test1.azurerm_public_ip.app-pub-ip will be created
  + resource "azurerm_public_ip" "app-pub-ip" {
      + allocation_method       = "Static"
      + ddos_protection_mode    = "VirtualNetworkInherited"
      + fqdn                    = (known after apply)
      + id                      = (known after apply)
      + idle_timeout_in_minutes = 4
      + ip_address              = (known after apply)
      + ip_version              = "IPv4"
      + location                = "centralindia"
      + name                    = "pub-ip"
      + resource_group_name     = "app_grp"
      + sku                     = "Standard"
      + sku_tier                = "Regional"
    }

  # module.virtual_machine.module.test1.azurerm_subnet.subA will be created
  + resource "azurerm_subnet" "subA" {
      + address_prefixes                              = [
          + "10.0.2.0/24",
        ]
      + default_outbound_access_enabled               = true
      + id                                            = (known after apply)
      + name                                          = "subnetA"
      + private_endpoint_network_policies             = "Disabled"
      + private_link_service_network_policies_enabled = true
      + resource_group_name                           = "app_grp"
      + virtual_network_name                          = "app-vnet"
    }

  # module.virtual_machine.module.test1.azurerm_virtual_network.app-network will be created
  + resource "azurerm_virtual_network" "app-network" {
      + address_space                  = [
          + "10.0.0.0/16",
        ]
      + dns_servers                    = (known after apply)
      + guid                           = (known after apply)
      + id                             = (known after apply)
      + location                       = "centralindia"
      + name                           = "app-vnet"
      + private_endpoint_vnet_policies = "Disabled"
      + resource_group_name            = "app_grp"
      + subnet                         = (known after apply)
    }

  # module.virtual_machine.module.test1.module.test.azurerm_resource_group.app_resource_group will be created
  + resource "azurerm_resource_group" "app_resource_group" {
      + id       = (known after apply)
      + location = "centralindia"
      + name     = "app_grp"
    }

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

after running terraform apply main.tfplan

6f6995d97b72/resourceGroups/app_grp/providers/Microsoft.Network/networkInterfaces/app-nic]
╷
│ Error: updating Public I P Address (Subscription: "xxxxxxxx"
│ Resource Group Name: "app_grp"
│ Public I P Addresses Name: "pub-ip"): polling after CreateOrUpdate: polling was cancelled: the Azure API returned the following error:
│
│ Status: "Canceled"
│ Code: "CanceledAndSupersededDueToAnotherOperation"
│ Message: "Operation was canceled.\nOperation PutPublicIpAddressOperation (0267e8cd-862e-49fc-ad6f-62d54a47833d) was canceled and superseded by operation PutPublicIpAddressOperation (0a30909f-ef76-4b09-873d-7e6fee36efa0)."
│ Activity Id: ""
│
│ ---
│
│ API Response:
│
│ ----[start]----
│ {"status":"Canceled","error":{"code":"Canceled","message":"Operation was canceled.","details":[{"code":"CanceledAndSupersededDueToAnotherOperation","message":"Operation PutPublicIpAddressOperation (0267e8cd-862e-49fc-ad6f-62d54a47833d) was canceled and superseded by operation PutPublicIpAddressOperation (0a30909f-ef76-4b09-873d-7e6fee36efa0)."}]}}
│ -----[end]-----
│
│
│   with module.virtual_machine.module.test1.azurerm_public_ip.app-pub-ip,
│   on .terraform/modules/virtual_machine.test1/app-nic.tf line 35, in resource "azurerm_public_ip" "app-pub-ip":
│   35: resource "azurerm_public_ip" "app-pub-ip" {
│

And in Azure portal i can see only below resources.

When i destroy the resources i got below error

╷
│ Error: deleting Subnet (Subscription: "1d902123-bc1c-4dcc-9779-6f6995d97b72"
│ Resource Group Name: "app_grp"
│ Virtual Network Name: "app-vnet"
│ Subnet Name: "subnetA"): performing Delete: unexpected status 400 (400 Bad Request) with error: InUseSubnetCannotBeDeleted: Subnet subnetA is in use by /subscriptions/1d902123-bc1c-4dcc-9779-6f6995d97b72/resourceGroups/APP_GRP/providers/Microsoft.Network/networkInterfaces/APP-NIC/ipConfigurations/INTERNAL and cannot be deleted. In order to delete the subnet, delete all the resources within the subnet. See aka.ms/deletesubnet.
│
│
╵

Does anyone know where i am doing mistakes.

it looks like there is connectivity issue between your app and other dependencies, so it caused The operation was canceled and ended up with timed out .

Hi @brucelok ,

Sometime i am able to create all resources but in night generally i face this type of issue.