0

I'm using an Azure PostgreSQL database and need to perform a point-in-time restore (PITR) as part of a disaster recovery drill. I can successfully restore the database to a new server and update my application to point to this new server.

However, I'm deploying the database server using Infrastructure as Code (Terraform). After performing a PITR, I end up with a new server that is not represented in my Terraform code.

My questions are:

  1. Is there a way to properly replace the existing server with the point-in-time data state while keeping my Terraform code in sync?

  2. Should I delete the previous server after restoring, or is there a better approach to managing this situation with Terraform? Any insights or best practices would be greatly appreciated!

2
  • please share the code you tried @kamalrathnayake Commented May 23, 2024 at 5:43
  • To keep your Terraform state in sync after a PITR, you can import the newly restored server into your Terraform state using the terraform import command. This way, you avoid inconsistencies between your infrastructure and Terraform code. After importing, you can safely delete the old server if it’s no longer needed. @kamalRathnayake Commented May 23, 2024 at 6:24

2 Answers 2

0

postgresql_flexible_server resource has enough options to allow you to have a point-in-time restore of postgres server.

You need to play with following options:

create_mode - set it to PointInTimeRestore

point_in_time_restore_time_in_utc - put time here

source_server_id - put source db id here

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

Sign up to request clarification or add additional context in comments.

1 Comment

This does not address the state management issues, but this would be the approach for performing the initial restore using Terraform. It's important to note that you can't change create mode directly on the original server resource because it would trigger a recreate which isn't typically desired behavior when dealing with backups,
0

Azure currently does not support in-place restores using the Point-in-Time Recovery feature of Azure PSQL Flexible server, so you have to always initially create a standalone flexible server resource. There is no one-click way to get Terraform to recognize this newly restored server without adding it to your Terraform code first and then using Terraform import commands. Deleting the original server will not address any state management issues.

The other alternative outside of imports would be to add some additional complexity to your Terraform code to conditionally create the restored server and manage in-line import blocks. Given that approach requires a bit of a deep dive I wrote an article here for further review.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.