Using Terraform for Geo-Restore in Azure PostgreSQL Flexible Server

It was recently announced that you can now use Terraform to Geo-Restore within PostgreSQL Flexible server. I will show how you can do this within this blog post. Terraform support for Geo-Restore allows you to incorporate the functionality into your CI/CD tooling!

What is Geo-Restore?

Geo-Restore is a powerful disaster recovery feature for Azure Database for PostgreSQL – Flexible Server that serves two primary purposes:

  1. Disaster Recovery: It allows you to restore your database from a geo-redundant backup to a server in another Azure region. This capability is crucial in scenarios where a regional outage occurs, ensuring business continuity by enabling you to recover your database in a different geographical location.
  2. Cross-Region Migration: Geo-Restore also provides a method for migrating your Flexible Server to another Azure region. This can be useful for various reasons, such as optimising latency for users in different geographical areas or complying with data residency requirements.

Terraform

Lets firstly look at creating a PostgreSQL Flexible server in region: uksouth

  • Note: geo_redundant_backup_enabled = true is required for Geo-Restore to work correctly
resource "azurerm_resource_group" "tamopsrg" {
  name     = "tamops-postgres"
  location = "Uk South"
}

resource "azurerm_postgresql_flexible_server" "tamopspsql" {
  name                   = "tamops-psqlflexibleserver"
  resource_group_name    = azurerm_resource_group.tamopsrg.name
  location               = azurerm_resource_group.tamopsrg.location
  version                = "16"
  administrator_login    = "thomas"
  administrator_password = "thomasthomas123!"
  zone                   = "2"

  storage_mb = 32768

  sku_name                     = "GP_Standard_D4s_v3"
  geo_redundant_backup_enabled = true
}

resource "azurerm_postgresql_flexible_server_database" "tamopspsqldb" {
  name      = "tamopsdb"
  server_id = azurerm_postgresql_flexible_server.tamopspsql.id
  collation = "en_US.utf8"
  charset   = "utf8"
}

The above will create a Resource Group and PostgreSQL Flexible server in uksouth region:

Screenshot in Azure - highlight PostgreSQL Flexible server created in Uk South

Geo-Restore to UK West

Now lets look at the Geo-Restore Terraform to allow me to create a restore copy of the above PostgreSQL Flexible sever in ukwest region:

resource "azurerm_postgresql_flexible_server" "tamopspsqlgeorestore" {
  name                              = "tamops-psqlgeorestore"
  resource_group_name               = azurerm_resource_group.tamopsrg.name
  location                          = "Uk West"
  version                           = "16"
  create_mode                       = "GeoRestore"
  source_server_id                  = azurerm_postgresql_flexible_server.tamopspsql.id
  point_in_time_restore_time_in_utc = timeadd(timestamp(), "5m") 
}

Key points of interest with the above:

  • create_mode is set to GeoRestore
  • source_server_id is referencing the PostgreSQL Flexible Server created initially in uksouth
  • point_in_time_restore_time_in_utc references a timestamp from 5 minutes ago (required or you will have error: point_in_time_restore_time_in_utc is required when create_mode is GeoRestore)

We can now see the restored PostgreSQL Flexible server has been created in ukwest below:

Screenshot in Azure - highlight PostgreSQL Flexible server created in Uk West using Geo-Restore

Terraform support for Geo-Restore in Azure Database for PostgreSQL – Flexible Server provides a powerful tool for automating disaster recovery processes

By incorporating this into your infrastructure-as-code practices, you can ensure that your data remains resilient and recoverable, even in the face of regional outage

Ps, remember to test your geo-restore configurations regularly as part of your disaster recovery exercises to ensure they work as expected 🙂

GitHub repository containing example above

Leave a Reply

Discover more from Thomas Thornton Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading

Discover more from Thomas Thornton Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading