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