I have resource enable-hibernation
which is deployed conditionally and another resource delete-dsc-extension
which always needs to run, just in case enable-hibernation
exists, it needs to wait for it before starting. How I can use depends_on
keyword in delete-dsc-extension
resource without erroring in cases where enable-hibernation
was never created?
resource "azapi_update_resource" "enable-hibernation" {
count = var.wsl_enabled ? 0 : var.rdsh_count
type = "Microsoft.Compute/virtualMachines@2024-03-01"
resource_id = azurerm_windows_virtual_machine.avd_vm.*.id[count.index]
body = jsonencode({
)
}
resource "azapi_resource_action" "delete-dsc-extension" {
count = var.rdsh_count
type = "Microsoft.Compute/virtualMachines/extensions@2024-07-01"
resource_id = azurerm_virtual_machine_extension.vdi_onboard.*.id[count.index]
method = "DELETE"
response_export_values = ["*"]
depends_on = [
azapi_update_resource.enable-hibernation
]
}
1
To address your requirement, I believe you might be able to use a conditional depends_on
based on the var.wsl_enabled
variable. If enable-hibernation
is not created, depends_on
will be set to an empty list.
So I mean something like:
resource "azapi_update_resource" "enable-hibernation" {
count = var.wsl_enabled ? 0 : var.rdsh_count
type = "Microsoft.Compute/virtualMachines@2024-03-01"
resource_id = azurerm_windows_virtual_machine.avd_vm.*.id[count.index]
body = jsonencode({
})
}
resource "azapi_resource_action" "delete-dsc-extension" {
count = var.rdsh_count
type = "Microsoft.Compute/virtualMachines/extensions@2024-07-01"
resource_id = azurerm_virtual_machine_extension.vdi_onboard.*.id[count.index]
method = "DELETE"
response_export_values = ["*"]
depends_on = var.wsl_enabled ? [] : [azapi_update_resource.enable-hibernation]
}
1