Home > Net >  Configure High Availability conditionally based on variable for `azurerm_postgresql_flexible_server`
Configure High Availability conditionally based on variable for `azurerm_postgresql_flexible_server`

Time:12-09

I'm configuring my servers with terraform. For non-prod environments, our sku doesn't allow for high availability, but in prod our sku does.

For some reason high_availability.mode only accepts the value of "ZoneRedundant" for high availability, but doesn't accept any other value (according to the documentation). Depending on whether or not var.isProd is true, I want to turn high availability on and off, but how would I do that?

resource "azurerm_postgresql_flexible_server" "default" {
  name                   = "example-${var.env}-postgresql-server"
  location               = azurerm_resource_group.default.location
  resource_group_name    = azurerm_resource_group.default.name
  version                = "14"
  administrator_login    = "sqladmin"
  administrator_password = random_password.postgresql_server.result

  geo_redundant_backup_enabled = var.isProd
  backup_retention_days        = var.isProd ? 60 : 7

  storage_mb = 32768

  high_availability {
    mode = "ZoneRedundant"
  }

  sku_name = var.isProd ? "B_Standard_B2s" : "B_Standard_B1ms"
}

CodePudding user response:

If you deploy flexible server using azurerm provider in terraform it will accept only ZoneRedundant value for highavailability.mode and also using this provider you can deploy sql server versions [11 12 13] only.

Using Azapi provider in terraform you can use highavailability values with any one of these "Disabled, ZoneRedundant or SameZone"

Based on your requirement I have created the below sample terraform script which has a environment variable accepts only prod or non-prod values using this value further flexible server will get deployed with respective properties.

  • If the environment value is prod script will deploy flexible server with high availability as zone redundant and also backup retention with 35 days with geo redundant backup enabled.
  • If the environment value is non-prod script will deploy flexible server with high availability as disabled and also backup retention with 7 days with geo redundant backup disabled

Here is the Terraform Script:

terraform {
  required_providers {
    azapi = {
      source = "azure/azapi"
    }
  }
}

provider "azapi" {
}

variable "environment" {
    type = string

    validation {
      condition = anytrue([var.environment == "prod",var.environment=="non-prod"])
      error_message = "you havent defined any of allowed values"
    }
}

resource "azapi_resource" "rg" {
  type = "Microsoft.Resources/resourceGroups@2021-04-01"
  name     = "teststackhub"
  location = "eastus"
  parent_id = "/subscriptions/<subscriptionId>"
}

resource "azapi_resource" "test" {
  type = "Microsoft.DBforPostgreSQL/flexibleServers@2022-01-20-preview"
  name                   = "example-${var.environment}-postgresql-server"
  location               = azapi_resource.rg.location
  parent_id = azapi_resource.rg.id
  body = jsonencode({
    properties= {
      administratorLogin="azureuser"
      administratorLoginPassword="<password>"
    backup = {
        backupRetentionDays = var.environment=="prod"?35:7
        geoRedundantBackup = var.environment=="prod"?"Enabled":"Disabled"
      }
      storage={
        storageSizeGB=32
      }
      highAvailability={
        mode= var.environment=="prod"?"ZoneRedundant":"Disabled"
      }
      version = "14"
    }
    sku={
        name = var.environment=="prod" ? "Standard_B2s" : "Standard_B1ms"
        tier = "GeneralPurpose"
      }
  })
}

NOTE: The above terraform sample script is for your reference please do make the changes based on your business requirement.

CodePudding user response:

I believe the default assignment for this resource would be disabled HA, and therefore it is not the argument mode which manages the HA, but rather the existence of the high_availability block. Therefore, you could manage the HA by excluding the block to accept the default "disabled", or including the block to manage the HA as enabled with a value of ZoneRedundant:

dynamic "high_availability" {
  for_each = var.isProd ? ["this"] : []
  
  content {
    mode = "ZoneRedundant"
  }
}

I am hypothesizing somewhat on the API endpoint parameter defaults, so this would need to be acceptance tested with an Azure account. However, the documentation for the Azure Postgres Flexible Server in general claims HA is in fact disabled by default, so this should function as desired.

  • Related