PITR in Terraform

I want to use azurerm_postgresql_server module for recovery from backup.
This module has restore_point_in_time and
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/postgresql_server#creation_source_server_id
It also has several required fields.
The code below is to restore a postgres instance. You can see, it is very similar to creating a brand new instance except using above two fields.

Is there a simpler way to do that, instead of duplicating most of the code?

resource "azurerm_postgresql_server" "pg_server_abc_restore_2023_05_08" {
  administrator_login              = lookup(local.pg_instances["abc"], "administrator_login", "dba")
  administrator_login_password     = module.pg_passwords["abc"].secret
  auto_grow_enabled                = lookup(local.pg_instances["abc"], "auto_grow_enabled", true)
  backup_retention_days            = lookup(local.pg_instances["abc"], "backup_retention_days", "7")
 
 create_mode                      = "PointInTimeRestore"
 creation_source_server_id        = azurerm_postgresql_server.this["abc"].id
  
geo_redundant_backup_enabled     = lookup(local.pg_instances["abc"], "geo_redundant_backup_enabled", substr(local.pg_instances["abc"].sku_name, 0, 2) != "B_") # Basic tier does not support geo redundant backups 
  location                         = lookup(local.pg_instances["abc"], "region", var.region)
  name                             = join("-", [lookup(local.pg_instances["abc"], "name", "${local.longname}-pg-abc"), "recovery"])
  public_network_access_enabled    = true
  resource_group_name              = lookup(local.pg_instances["abc"], "resource_group_name", azurerm_resource_group.this.name)
  restore_point_in_time            = "2023-05-01T06:20:00Z" 
  sku_name                         = local.pg_instances["abc"].sku_name
  ssl_enforcement_enabled          = true
  ssl_minimal_tls_version_enforced = lookup(local.pg_instances["abc"], "ssl_minimal_tls_version_enforced", "TLS1_2")
  storage_mb                       = local.pg_instances["abc"].storage_mb
  version                          = lookup(local.pg_instances["abc"], "version", "11")
  tags = local.tags,
   
  lifecycle {
    ignore_changes = [
      storage_mb,
    ]
  }
}
1 Like

Hi Wshao! Unfortunately there isn’t a simpler approach for using PITR for TF with Azure PSQL Flexi Server. Your code snippet is an accurate example of how you’d perform the restore. It requires a separate server to be instantiated. I believe this is mostly driven by the fact that Azure doesn’t currently support in-place PITR recovery for the overall PSQL Flexi Server service. If you try to perform a PITR via the portal or CLI you’ll see that it behaves very similarly to how TF is doing it minus the fact that Azure is auto-populating most of the fields for you.

I did develop a solution to address state management while using PITR for Azure PSQL and published an article titled, “Managing Terraform State while using Azure PostgreSQL Flexible Server Point-in-Time Recovery”. Let me know if it’s helpful!