Ikteaja hsan
Ikteaja hsan

Reputation: 61

is it possible to create Azure Database for PostgreSQL Flexible server - Restore server-> (Back up and restore) using terraform?

The azure Database for PostgreSQL Flexible server automatically back up the databases. In case of any accidental deletion of any databases we can restore the database by creating a new flexible server for the recovery process from the back up database .I know how do it from azure portal.Does the terraform code can also configure "backup and restore" for PostgreSQL Flexible server - Restore server.

The exact summary of the manual task documented in the azure doc:https://learn.microsoft.com/en-us/azure/postgresql/flexible-server/how-to-restore-server-portal. Just i want do the task using terraform . In addition to that ensure appropriate login and database level permission

I really appreciate any support and help

enter image description here

Upvotes: 0

Views: 2113

Answers (1)

Komali Annem
Komali Annem

Reputation: 753

It is possible to create the azure database for PostgreSQL flexible server backup using terraform

Please use the below terraform code to restore the server

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "example" {
  name     = "RG_NAME"
  location = "EASTUS"
}

resource "azurerm_virtual_network" "example" {
  name                = "example-vn"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  address_space       = ["10.0.0.0/16"]
}

resource "azurerm_subnet" "example" {
  name                 = "example-sn"
  resource_group_name  = azurerm_resource_group.example.name
  virtual_network_name = azurerm_virtual_network.example.name
  address_prefixes     = ["10.0.2.0/24"]
  service_endpoints    = ["Microsoft.Storage"]
  delegation {
    name = "fs"
    service_delegation {
      name = "Microsoft.DBforPostgreSQL/flexibleServers"
      actions = [
        "Microsoft.Network/virtualNetworks/subnets/join/action",
      ]
    }
  }
}
resource "azurerm_private_dns_zone" "example" {
  name                = "example.postgres.database.azure.com"
  resource_group_name = azurerm_resource_group.example.name
}

resource "azurerm_private_dns_zone_virtual_network_link" "example" {
  name                  = "exampleVnetZone.com"
  private_dns_zone_name = azurerm_private_dns_zone.example.name
  virtual_network_id    = azurerm_virtual_network.example.id
  resource_group_name   = azurerm_resource_group.example.name
}

resource "azurerm_postgresql_flexible_server" "example" {
  name                   = "example-psqlflexibleserver"
  resource_group_name    = azurerm_resource_group.example.name
  location               = azurerm_resource_group.example.location
  version                = "12"
  delegated_subnet_id    = azurerm_subnet.example.id
  private_dns_zone_id    = azurerm_private_dns_zone.example.id
  administrator_login    = "psqladmin"
  administrator_password = "H@Sh1CoR3!"
  zone                   = "1"

  storage_mb = 32768
  backup_retention_days = 30
  geo_redundant_backup_enabled = true

  sku_name   = "GP_Standard_D4s_v3"
  depends_on = [azurerm_private_dns_zone_virtual_network_link.example]
 
}

Here I have mentioned the RG_name, subnet, VM, Vnet, db name, password and backup policy days

I have given the backup policy retention days are 30 the policy retention days should be in between 1 to 35 and the defaults value is 7 days

Before running the script we have to check the appropriate login server details

After the follow the below steps to execute the file

terraform init

It will initialize the file

enter image description here

Terraform plan

This will creates an execution plan and it will preview the changes that terraform plans to make the infrastructure

enter image description here

Terraform apply

This will creates or updates the infrastructure depending on the configuration

enter image description here

Previously it was default and the geo_redundant_backup_enabled is false I have set it to true and backup policy will be 30 days

For reference you can use this documentation

Upvotes: 0

Related Questions