I am trying to have backup initiated for Azure VM with terraform but it is not taking the source_vm_id parameter. I have it listed as below
#https://www.terraform.io/docs/providers/azurerm/r/backup_protected_vm.html
resource "azurerm_backup_protected_vm" "BackupProtected_app_vm_01" {
resource_group_name = "${azurerm_resource_group.resource_group.name}"
recovery_vault_name = "z1-r-op-rsv-01"
#source_vm_id = "${azurerm_virtual_machine.app_vm_01[count.index].id}"
source_vm_id = [element(azurerm_virtual_machine.app_vm_01.*.id, count.index)]
backup_policy_id = "${azurerm_backup_policy_vm.BackupPolicy_app_vm_01.id}"
}
I am getting the following error: Error: Reference to "count" in non-counted context
on AGC.tf line 264, in resource "azurerm_backup_protected_vm" "BackupProtected_app_vm_01": 264: source_vm_id = [element(azurerm_virtual_machine.app_vm_01.*.id, count.index)]
The "count" object can be used only in "resource" and "data" blocks, and only when the "count" argument is set.
I tried to change it without using "element" but it is still throwing out error.
Here is what I have for VM defined:
resource "azurerm_virtual_machine" "app_vm_01" {
count = var.app_vm_01_count
name = "${var.reg}${var.dash}${var.env}${var.dash}${var.app}${var.dash}${var.app_vm_01}${var.dash}${format("%02d", count.index+1)}"
location = var.location
resource_group_name = azurerm_resource_group.resource_group.name
network_interface_ids = [element(azurerm_network_interface.app_vm_01.*.id, count.index)]
availability_set_id = azurerm_availability_set.app_vm_01.id
vm_size = "Standard_D2s_v3"
license_type = "Windows_Server"
delete_os_disk_on_termination = true
delete_data_disks_on_termination = true
boot_diagnostics {
enabled = "true"
storage_uri = var.dia_uri
}