0
votes

Im trying to import the resources and and create VM from the existing image on Azure. All the imports are successful but when I run terraform apply, I get the following error: enter image description here

here is the code:

provider "azurerm" {
    version = "2.0.0"
    subscription_id = var.subscriptionID

    features {}
}
resource "azurerm_resource_group" "TerraformResourceGroup" {

}

resource "azurerm_network_security_group" "TerraformSecurityGroup" {

}

resource "azurerm_virtual_network" "TerraformVirtualNetwork" {

}

resource "azurerm_network_interface" "TerraformNetworkInterface" {

}

resource "azurerm_virtual_machine" "TerraformVM" {
  name                  = var.VM
  location              = var.location
  resource_group_name   = azurerm_resource_group.TerraformResourceGroup.name
  network_interface_ids = [azurerm_network_interface.TerraformNetworkInterface.id]
  vm_size               = "Standard_DS1_v2"

 storage_image_reference {
    id  = "/subscriptions/--/resourceGroups/TerraformResourceGroup/providers/Microsoft.Compute/images/TerraformVM-image-20210201164126"
}

  storage_os_disk {
    name              = "disk1"
    caching           = "ReadWrite"
    create_option     = "FromImage"
    managed_disk_type = "Standard_LRS"
    }

  os_profile {
    computer_name  = var.name
    admin_username = var.username
    admin_password = var.password
  }

  os_profile_windows_config {
     provision_vm_agent = true
  }

  tags = {
    environment = "staging"
  }
}

Please help

Ok, Let me explain a bit clear. I created a rg and deployed a vm using terraform, then captured the VM. now I need to deploy a vm using that image in same rg. Do I need to use import or data sources? if I use data source I'm getting the following:Error deleting Virtual Network "xx" (Resource Group "yy"): network.VirtualNetworksClient#Delete: Failure sending request: StatusCode=400 -- Original Error: Code="InUseSubnetCannotBeDeleted" Message="Subnet xx is in use by /subscriptions/--/resourceGroups/yy/providers/Microsoft.Network/networkInterfaces/xx/ipConfigurations/internal and cannot be deleted. In order to delete the subnet, delete all the resources within the subnet. Code using data sources

provider "azurerm" {
    version = "2.0.0"
    subscription_id = var.subscriptionID

    features {}
}
data "azurerm_resource_group" "TerraformResourceGroup" {
  name     = var.resourceGroupName
}

data "azurerm_network_security_group" "TerraformSecurityGroup" {
  name                = var.securityGroup
  resource_group_name = data.azurerm_resource_group.TerraformResourceGroup.name
}

data "azurerm_virtual_network" "TerraformVirtualNetwork" {
  name                = var.virtualNetwork
  resource_group_name = data.azurerm_resource_group.TerraformResourceGroup.name
}

data "azurerm_subnet" "TerraformSubNet" {
  name                 = var.subnet
  virtual_network_name = var.virtualNetwork
  resource_group_name  = data.azurerm_resource_group.TerraformResourceGroup.name
}

# Create public IPs
resource "azurerm_public_ip" "test" {
    name                         = var.publicIP
    location                     = "${data.azurerm_resource_group.TerraformResourceGroup.location}"
    resource_group_name          = "${data.azurerm_resource_group.TerraformResourceGroup.name}"
    allocation_method            = "Static"

}

# create a network interface
resource "azurerm_network_interface" "TerraformNetworkInterface" {
  name                = var.networkInterface
  location            = "${data.azurerm_resource_group.TerraformResourceGroup.location}"
  resource_group_name = "${data.azurerm_resource_group.TerraformResourceGroup.name}"

  ip_configuration {
    name                          = "testconfiguration1"
    subnet_id                     = "${data.azurerm_subnet.TerraformSubNet.id}"
    private_ip_address_allocation = "dynamic"
   // public_ip_address_id          = "${azurerm_public_ip.test.id}"
  }
}

resource "azurerm_virtual_machine" "TerraformVM" {
  name                  = var.VM
  location              = var.location
  resource_group_name   = data.azurerm_resource_group.TerraformResourceGroup.name
  network_interface_ids = [azurerm_network_interface.TerraformNetworkInterface.id]
  vm_size               = "Standard_DS1_v2"

 storage_image_reference {
    id  = "/subscriptions/----/resourceGroups/TerraformResourceGroup/providers/Microsoft.Compute/images/TerraformVM-image-20210202111108"
    }

  storage_os_disk {
    name              = "Imagedisk"
    caching           = "ReadWrite"
    create_option     = "FromImage"
    managed_disk_type = "Standard_LRS"
    }

  os_profile {
    computer_name  = var.name
    admin_username = var.username
    admin_password = var.password
  }

  os_profile_windows_config {
     provision_vm_agent = true
  }

  tags = {
    environment = "staging"
  }
}

Could you please help

2

2 Answers

0
votes

From the error message, you are missing parameters in azurerm_network_interface.

You need to add location and resource_group_name in resource "azurerm_network_interface":

Here is an example:

    provider "azurerm" {
        version = "2.0.0"
        subscription_id = var.subscriptionID
    
        features {}
    }

    variable "prefix" {
         default = "tfvmex"
     }

    resource "azurerm_resource_group" "TerraformResourceGroup" {
      name     = "${var.prefix}-resources"
      location = "West US 2"
    }
   resource "azurerm_network_security_group" "TerraformSecurityGroup" {

   }
    
    resource "azurerm_virtual_network" "TerraformVirtualNetwork" {
      name                = "${var.prefix}-network"
      address_space       = ["10.0.0.0/16"]
      location            = azurerm_resource_group.main.location
      resource_group_name = azurerm_resource_group.main.name
    }
    
    resource "azurerm_network_interface" "TerraformNetworkInterface" {
      name                = "${var.prefix}-nic"
      location            = azurerm_resource_group.main.location
      resource_group_name = azurerm_resource_group.main.name
    

    }
    
    ......

For more detailed info, you could refer to this doc about Terraform azurerm_virtual_machine.

0
votes

The Terraform import command does not generate a Terraform resource configuration. Instead, it imports your existing resources into Terraform’s state.

After importing your existing resources into the state file. You need to manually create a corresponding resource configuration file referring to the information provided by the state and manage your existing infrastructure with Terraform.

For example, you can run terraform show, which provides human-readable output from a state or plan file. enter image description here

Then you can copy the attributes of name and location and contents to fill the block of

resource "azurerm_resource_group" "TerraformResourceGroup" {
    location = "westeurope"
    name     = "nancyexample-resources"
}

You can fill the remaining blocks in the same way. After that, you can check for errors in your configuration by running the terraform plan command. Terraform plan shows you the changes that would take place if you were to apply the configurations with the terraform apply command. If there are any arguments missing or not set, you can modify your configuration file to meet the requirements.

Once you have verified the configurations, you can run terraform apply to create the new resources. For more information, see Import Terraform configuration.