azureterraformterraform-provider-azureazure-postgresql

Not able to do geo restore in same region of azure postgres flex server using terraform


I am trying to do restore of azure flexible postgres server on which geo redundant backup is enabled. From azure portal I am able to restore in same region. But from terraform I am getting error while running terraform.

Below is terraform template for restoring server:

provider "azurerm" {
  features {}
  subscription_id = "my-sub-id"
}



# Flexible Server creation using point-in-time restore
resource "azurerm_postgresql_flexible_server" "restored_server" {
  name                   = "sameregionrestoreserver"
  location               = "westeurope"
  storage_mb             = "131072"
  sku_name               = "GP_Standard_D2ds_v4"
  resource_group_name    = "same-resource-group"
  backup_retention_days        = 7
  geo_redundant_backup_enabled = true
  create_mode            = "GeoRestore"
  source_server_id       = "/subscriptions/my-sub-id/resourceGroups/source-resource-group/providers/Microsoft.DBforPostgreSQL/flexibleServers/source-server-name"
  point_in_time_restore_time_in_utc  = "2024-11-06T13:15:21.000Z"
}

Getting below error while running terraform

ERROR running: terraform apply -auto-approve -no-color  : 
Error: creating/updating "Resource: (ResourceId \"/subscriptions/****/resourceGroups/new-resource-group/providers/Microsoft.DBforPostgreSQL/flexibleServers/new-server-name\" / Api Version \"2024-11-01-preview\")": PUT https://management.azure.com/subscriptions/****/resourceGroups/new-resource-group/providers/Microsoft.DBforPostgreSQL/flexibleServers/server-name
--------------------------------------------------------------------------------
RESPONSE 404: 404 Not Found
ERROR CODE: ResourceNotFound
--------------------------------------------------------------------------------
 {
    "error": {
      "code": "ResourceNotFound",
      "message": "The requested resource of type 'Microsoft.DBforPostgreSQL/flexibleServers' with name 'source-server-name' was not found."
    }
  }
 --------------------------------------------------------------------------------

Failing to restore in same region. Restore operation should complete successful in same region.


Solution

  • Geo restore in same region of azure postgres flex server using terraform

    Issue seems to be with the way you are using create_mode as "GeoRestore" but in case if you want to do backup in the same region we need to use "point in time restore" which helps you in achiveing this requriement.

    GeoRestore should be used in the case only if you want to take the back up in different location.

    Configuration:

    resource "azurerm_postgresql_flexible_server" "restored_server" {
      depends_on             = [time_sleep.wait_before_restore]
      name                   = "sameregionrestoreserver"
      location               = azurerm_resource_group.example.location
      resource_group_name    = azurerm_resource_group.example.name
      storage_mb             = 131072
      sku_name               = "GP_Standard_D2ds_v4"
      backup_retention_days  = 7
      geo_redundant_backup_enabled = true
      create_mode            = "PointInTimeRestore"
      source_server_id       = azurerm_postgresql_flexible_server.new_server.id
      point_in_time_restore_time_in_utc = "2024-11-06T13:15:21.000Z"
    }
    

    Deployment:

    enter image description here

    Refer:

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

    https://learn.microsoft.com/en-us/rest/api/postgresql/flexibleserver/servers/create?view=rest-postgresql-flexibleserver-2022-12-01&tabs=HTTP