Search code examples
terraformterraform-provider-azureazure-sql-server

Azure SQL back up setting in Terraform


Trying to set a parameter in Terraform file to back-up an Azure SQL server.

In AWS i can add a key like:

      allocated_storage = 1000
      engine_version = “13.00.4451.0.v1”
      backup_window = “18:00-18:30”
      backup_retention_period = 14

I can't find a similar key to back-up an Azure SQL. Below is the back up key that i have found in Terraform Docs and on the web:

  resource "azurerm_sql_server" "some_erver" {
    administrator_login          = "some_login"
    administrator_login_password = "some_password"
    location                     ="some_location"
    name                         = "some_server"
    resource_group_name          = "some_resource"
    version                      = "some_version"

  tags = {
    environment  = "t"
    stack-name   = "name"
    stack-number = "number"
    app-number   = "app-number"
    logicmonitor = "bool"
  }
  backup {
    frequency = "Daily"
    time      = "04:00"
  }

  retention_daily {
    count = 30
  }
}

But the Terraform Apply pipeline fails with a message:

     Error: azurerm_sql_server.enxtz256-db01: : invalid or unknown key: backup

     Error: azurerm_sql_server.enxtz256-db01: : invalid or unknown key: retention_daily

Any help would be much appreciated


Solution

  • I would like to point out that according to the development team (source) azurerm_sql_database is not getting updated anymore. Instead, all new features are added to azurerm_mssql_database, which actually deploys the same database types. According to the Terraform documentation, azurerm_mssql_database supports both short-term backups (point-in-time recovery) as well as long-term backups. This can be managed by the following in-line blocks:

    A long_term_retention_policy block supports the following:

    weekly_retention - (Optional) The weekly retention policy for an LTR backup in an ISO 8601 format. Valid value is between 1 to 520 weeks. e.g. P1Y, P1M, P1W or P7D.

    monthly_retention - (Optional) The monthly retention policy for an LTR backup in an ISO 8601 format. Valid value is between 1 to 120 months. e.g. P1Y, P1M, P4W or P30D.

    yearly_retention - (Optional) The yearly retention policy for an LTR backup in an ISO 8601 format. Valid value is between 1 to 10 years. e.g. P1Y, P12M, P52W or P365D.

    week_of_year - (Optional) The week of year to take the yearly backup in an ISO 8601 format. Value has to be between 1 and 52.

    A short_term_retention_policy block supports the following:

    retention_days - (Required) Point In Time Restore configuration. Value has to be between 7 and 35.

    If you somehow came from azurerm_sql_database and want to migrate to azurerm_mssql_database you could follow the steps below. I have done this myself, and it is quite easy.

    • Run: terraform state rm "azurerm_sql_database.database"
    • Change the resource to azurerm_mssql_database, including the appropriate fields
    • Run: terraform import azurerm_mssql_database.database "/very/long/database/id"
    • Run: terraform apply

    https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/mssql_database

    .