Here I'm using data blocks for retrieve the information of Vnet if it already exist. If VNet is not existing than create a new one with the same name. I have defined everything properly.
data "azurerm_virtual_network" "existing_vnet" {
name = var.vnet_name
resource_group_name = data.azurerm_resource_group.existing_rg.name
}
locals {
vnet_exists = length(data.azurerm_virtual_network.existing_vnet) > 0
}
resource "azurerm_virtual_network" "my_vnet" {
count = local.vnet_exists ? 0 : 1
name = var.vnet_name
address_space = var.address_space
location = data.azurerm_resource_group.existing_rg.location
resource_group_name = data.azurerm_resource_group.existing_rg.name
}
But after running terraform apply
it shows me an error of:
Error: Error: Virtual Network: (Name "axis_VN" / Resource Group "training") was not found
│
│ with data.azurerm_virtual_network.existing_vnet,
│ on main.tf line 24, in data "azurerm_virtual_network" "existing_vnet":
│ 24: data "azurerm_virtual_network" "existing_vnet" {
can any one help me in this?
I was expecting that VNet would only create if it doesn't exit in the resource group
I tried to check with following code , to check for existing resources
data "azurerm_virtual_network" "existing_vnet" {
name = "myVnet"
resource_group_name = data.azurerm_resource_group.example.name
}
locals {
vnet_exists = length(data.azurerm_virtual_network.existing_vnet) > 0
}
output "vnetcout" {
value = local.vnet_exists
}
It gives error as "resource not found " if it does not exist already .
For this External Data source can be used : external_external | Data Sources | hashicorp/external | Terraform Registry as Terraform does not check for preexisting resources.
Here almost any programming language can be used. One such example i tried to create VM if it does not exist.
data "external" "vm_exists" {
program = ["Powershell.exe", "C:\\Users\\xx\\checkexistence.ps1"]
}
output "vmexistsornot" {
value = "${data.external.vm_exists.result.value}"
}
Powershell code: Checkexistence.ps1
$rgName = "myrg"
$vmName = "excpectedm"
$vmExists = Get-AzVM -Name $vmName -ResourceGroupName $rgName -ErrorAction SilentlyContinue
# $vmexistsmsg="VM exists"
# $vmnotexistsmsg="VM does not exist"
if (!$vmExists) {
Write-Output '{"value" : "true" }'
} else {
Write-Output '{"value" : "false" }'
}
Initially checked only for non existence:It has to return true.
Then if the Vm doesnot exist , it will be created.
resource "azurerm_network_interface" "example" {
name = "kaexample-nic"
location = data.azurerm_resource_group.example.location
resource_group_name = data.azurerm_resource_group.example.name
ip_configuration {
name = "example-ip-config"
subnet_id = azurerm_subnet.example.id
private_ip_address_allocation = "Dynamic"
}
}
resource "azurerm_virtual_network" "example" {
name = "kaexample-virtual-network"
address_space = ["10.0.0.0/16"]
location = data.azurerm_resource_group.example.location
resource_group_name = data.azurerm_resource_group.example.name
}
resource "azurerm_subnet" "example" {
name = "kkexample-subnet"
address_prefixes = ["10.0.0.0/24"]
virtual_network_name = azurerm_virtual_network.example.name
resource_group_name = data.azurerm_resource_group.example.name
}
resource "azurerm_virtual_machine" "new_vm" {
count = data.external.vm_exists.result.value == "true" ? 1 : 0
name = var.vm_name
resource_group_name = var.resource_group_name
location = "eastus"
vm_size = "Standard_D4as_v5"
network_interface_ids = [azurerm_network_interface.example.id]
storage_os_disk {
name = "my_os_storage_disk"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Standard_LRS"
}
os_profile {
computer_name = "myvm"
admin_username = "azureuser"
admin_password = "MyComplexPassword"
}
os_profile_windows_config {
}
# other configuration settings...
}
resource "azurerm_virtual_machine" "new_vm" {
count = data.external.vm_exists.result.value == true ? 0 : 1
name = var.vm_name
resource_group_name = var.resource_group_name
location = "eastus"
}