hashicorp / terraform-provider-azurerm

Terraform provider for Azure Resource Manager
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs
Mozilla Public License 2.0
4.6k stars 4.64k forks source link

Modifying backup time in azurerm_backup_policy_vm fails #27444

Closed Greysi87 closed 1 month ago

Greysi87 commented 1 month ago

Is there an existing issue for this?

Community Note

Terraform Version

1.9

AzureRM Provider Version

3.116.0

Affected Resource(s)/Data Source(s)

azurerm_backup_policy_vm

Terraform Configuration Files

variable "timestamp-vm" {
  type        = string
  default     = "20:00"
}

variable "timezone" {
  type        = string
  default     = "W. Europe Standard Time"
}

variable "backup_resource_group_prefix" {
  type        = string
  default     = "rg-backup"
}

resource "azurerm_backup_policy_vm" "vm-gold-v2" {
  name                = "vm-gold-v2"
  resource_group_name = var.resource_group_name
  recovery_vault_name = azurerm_recovery_services_vault.rsv.name
  policy_type         = "V2"
  timezone            = var.timezone

  backup {
    frequency = "Daily"
    time      = var.timestamp-vm
  }
  instant_restore_retention_days = 3
  instant_restore_resource_group {
    prefix = var.backup_resource_group_prefix
  }

  retention_daily {
    count = 14
  }

  retention_weekly {
    count    = 4
    weekdays = ["Sunday"]
  }

  retention_monthly {
    count    = 12
    weekdays = ["Sunday"]
    weeks    = ["First"]
  }
}

Debug Output/Panic Output

Creating this policy works as expected. If I then change backup time to another time (e-g- from 20:00 to 21:00) and execute terraform apply, following planed change appears:

  ~ resource "azurerm_backup_policy_vm" "vm-gold-v2" {
        id                             = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-qua-chn-services-mgmt/providers/Microsoft.RecoveryServices/vaults/rsv-chn-services/backupPolicies/vm-gold-v2"
        name                           = "vm-gold-v2"
        # (5 unchanged attributes hidden)

      ~ backup {
          ~ time          = "20:00" -> "21:00"
            # (4 unchanged attributes hidden)
        }

        # (4 unchanged blocks hidden)
    }

If I approve this change, following error appears:
╷
│ Error: updating Backup Policy (Subscription: "00000000-0000-0000-0000-000000000000"
│ Resource Group Name: "rg-qua-chn-services-mgmt"
│ Vault Name: "rsv-chn-services"
│ Backup Policy Name: "vm-gold-v2"): protectionpolicies.ProtectionPoliciesClient#CreateOrUpdate: Failure responding to request: StatusCode=400 -- Original Error: autorest/azure: Service returned an error. Status=400 Code="BMSUserErrorInvalidPolicyInput" Message="Input for create or update policy is not in proper format. Please check format of parameters like schedule time, schedule days, retention time and retention days "
│
│   with azurerm_backup_policy_vm.vm-gold-v2,
│   on policy_vm.tf line 172, in resource "azurerm_backup_policy_vm" "vm-gold-v2":
│  172: resource "azurerm_backup_policy_vm" "vm-gold-v2" {
│
╵

Expected Behaviour

Backup time can be changed the same ways, as it can be set/created.

Actual Behaviour

If backup time should be changed, an error appears:

╷ │ Error: updating Backup Policy (Subscription: "00000000-0000-0000-0000-000000000000" │ Resource Group Name: "rg-qua-chn-services-mgmt" │ Vault Name: "rsv-chn-services" │ Backup Policy Name: "vm-gold-v2"): protectionpolicies.ProtectionPoliciesClient#CreateOrUpdate: Failure responding to request: StatusCode=400 -- Original Error: autorest/azure: Service returned an error. Status=400 Code="BMSUserErrorInvalidPolicyInput" Message="Input for create or update policy is not in proper format. Please check format of parameters like schedule time, schedule days, retention time and retention days " │ │ with azurerm_backup_policy_vm.vm-gold-v2, │ on policy_vm.tf line 172, in resource "azurerm_backup_policy_vm" "vm-gold-v2": │ 172: resource "azurerm_backup_policy_vm" "vm-gold-v2" { │ ╵

Initial creating backup policy for VMs works fine, only changes are affected.

Steps to Reproduce

  1. Define a VM backup policy (like example above) with backup {frequency = "Daily" time = "20:00" )
  2. Execute terraform apply to create this VM backup policy
  3. Change Backup Time (e.g. from "20:00" to "21:00"
  4. Execute terraform apply again and approve modification ==> Error happens.

Important Factoids

No response

References

This issue exists in azurerm version 3.112.0 to 4.2.0 (tested). probably also in previous versions (not tested).

Greysi87 commented 4 weeks ago

Unfortunately this bug has not been solved. Tested with azurerm 4.4.0 and tried to change backup time from 02:00 to 02:30 and got error:

~ resource "azurerm_backup_policy_vm" "vm-gold-v2" { id = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-qua-chn-services-mgmt/providers/Microsoft.RecoveryServices/vaults/rsv-chn-services/backupPolicies/vm-gold-v2" name = "vm-gold-v2"

(5 unchanged attributes hidden)

  ~ backup {
      ~ time          = "02:00" -> "02:30"
        # (4 unchanged attributes hidden)
    }

    # (4 unchanged blocks hidden)
}

Error: updating Backup Policy (Subscription: "00000000-0000-0000-0000-000000000000" │ Resource Group Name: "rg-qua-chn-services-mgmt" │ Vault Name: "rsv-chn-services" │ Backup Policy Name: "vm-gold-v2"): protectionpolicies.ProtectionPoliciesClient#CreateOrUpdate: Failure responding to request: StatusCode=400 -- Original Error: autorest/azure: Service returned an error. Status=400 Code="BMSUserErrorInvalidPolicyInput" Message="Input for create or update policy is not in proper format. Please check format of parameters like schedule time, schedule days, retention time and retention days " │ │ with module.recovery_services_vault.azurerm_backup_policy_vm.vm-gold-v2, │ on policy_vm.tf line 172, in resource "azurerm_backup_policy_vm" "vm-gold-v2": │ 172: resource "azurerm_backup_policy_vm" "vm-gold-v2" { │

Greysi87 commented 4 weeks ago

I found, issue exists only, if an azurerm_backup_policy_vm contains a retention_monthly config. If a backup policy will be created without a monthly retention, all works fine.

It also works, if you remove monthly retention, change backup time and recreate monthly retention.

Conclusion: As soon as retention policy has a monthly retention, modifying backup time is not possible and you get above error.