we have below resource creation and I need to control only the creating of this orahome resource based on var.azure_db_server_create_orahome_disk
when its false it should not create and when true it should create.
`resource "azurerm_managed_disk" "dbhome" {
count = var.azure_db_server_create_orahome_disk ? 1 : 0
name = "${local.db_server_name}-orahome-0"
location = data.azurerm_resource_group.rg.location
resource_group_name = data.azurerm_resource_group.rg.name
storage_account_type = "Premium_LRS"
create_option = "Empty"
disk_size_gb = var.dbdisksize_dbhome
zones = local.availability_zone
}`
So by adding the line count = var.azure_db_server_create_orahome_disk ? 1 : 0
its hapenning properly and fine.Please correct if this method also not correct.
Problem is, while VM creation its still trying to attached that disk and find a managed_disk_id even I put count = var.azure_db_server_create_orahome_disk ? 1 : 0
on it. I need to fully ignore that disk creation and attachment and continue but other disk should proceed as same as earlier.
`resource "azurerm_virtual_machine" "oracle19c" {
name = local.db_server_name
location = azurerm_network_interface.nic.location
resource_group_name = data.azurerm_resource_group.rg.name
network_interface_ids = [azurerm_network_interface.nic.id]
vm_size = contains(var.vm_nonsupported_regions, var.region) ? var.dbvmsize_fallback : var.dbvmsize
delete_os_disk_on_termination = true
delete_data_disks_on_termination = true
storage_data_disk {
count = var.azure_db_server_create_orahome_disk ? 1 : 0
name = "${local.db_server_name}-orahome-0"
caching = "ReadOnly"
managed_disk_id = azurerm_managed_disk.dbhome.id
create_option = "Attach"
lun = 1
disk_size_gb = var.dbdisksize_dbhome
}
}`
I added count = var.azure_db_server_create_orahome_disk ? 1 : 0
in azurerm_managed_disk creation and also attachment of that disk. but no luck with the main requirment
2
Answers
Issue has been resloved by making dynamic "storage_data_disk" {} and also with for_each inside that.
The managed disk is created, and the data disk is attached. If
var.azure_db_server_create_orahome_disk
isfalse
, neither the managed disk is created nor the data disk is attached. The use of thefor_each
block helps control the number of instances based on the condition.I am trying to create an
Azure VM
with a managed data disk using the following code.Terraform Apply:
When I set
default = true
, a managed disk is created and attached to the VM as below.When I set
default = false
, a managed disk is not created