1

I am having difficulty in creating resources in azure using terraform

  1. Vnet is already present and it is in rg group
  2. Resource group is present and vnet is in that group
  3. I am creating 1 subnets new resources in another existing resource group name MB-TB-Dev
  4. I Will be creating next 2 vm one is Linux red hat and one is windows

I am using the code below:

// Configure the Microsoft Azure Provider

provider "azurerm" {
    features {}
    subscription_id = "xxxxxxxxxx"
}

// Source code for the Resource Group i want my subnet in that
data "azurerm_resource_group" "rg_name" {
    name = "MB-Tb-Dev"   
}

output "id" {
    value = data.azurerm_resource_group.rg_name.id
}

// vnet already define already present in another resource group
data "azurerm_virtual_network" "vnet" {
    name                  = "sknet"
    resource_group_name   = "rg"
}

output "virtual_network_id" {
    value = data.azurerm_virtual_network.vnet.id
}

// Subnet creation
resource "azurerm_subnet" "subnet1" {
    name                 = "FrontEnd"
    resource_group_name  = "${data.azurerm_resource_group.rg_name.name}"
    virtual_network_name = "${data.azurerm_virtual_network.vnet.id}"
    address_prefixes     = ["10.0.1.0/24"]
}

I am having error when I run the terraform apply:

Error: creating Subnet: (Name "FrontEnd" / Virtual Network Name "/subscriptions/XXXXXX-
XXXXXXXX-a/resourceGroups/rg/providers/Microsoft.Network/virtualNetworks/sknet" / Resource
Group "MB-Tb-Dev"): network.SubnetsClient#CreateOrUpdate: Failure sending request: 
StatusCode=404 -- Original Error: Code="ResourceNotFound" Message="The Resource
Microsoft.Network/virtualNetworks/subscriptions' under resource group 'MB-Tb-Dev' was not found. 
│
│                     with azurerm_subnet.subnet1,
│                     on subnet-main.tf line 34, in resource "azurerm_subnet" "subnet1":
│                     34: resource "azurerm_subnet" "subnet1" {
│
╵
Kombajn zbożowy
  • 8,755
  • 3
  • 28
  • 60
  • Personally, I would if possible just create all the resources in Terraform. If that's not an option then check the resources under Azure to see if they exist for import. – cody.codes Feb 21 '22 at 00:33
  • 2
    I cant import because vnet is used by many resource group and if you use import than it will destroy the vnet form other resources. I am using data source. – Serendipty7 Feb 21 '22 at 01:41
  • Do you get any value from the outputs, i.e., from `virtual_network_id` and `id `? – Marko E Feb 21 '22 at 07:16

1 Answers1

-1

Have you checked the available resource and the location details in the rg resource group name? Simply it might have some issues defining the relevant parameters. You may check the relevant relevant resource on this.