Creating multiple Azure postgresql server replicas using Terraform

Using Terraform to create a postgresql server in Azure? There may be sometimes a requirement to create additional read-replicas of this server. In this blog, I am going to show how you can achieve this! I will be giving the option to create additional replicas with a variable addition (up to 5 replicas allowed currently at time of writing this blog post).

Replicas are updated asynchronously and recommended to be used to improve the performance of intensive read workloads; where you just need read-only access which you know is intensive – it is recommended to begin looking at one (or multiple) read replicas!

Terraform

Please note: This terraform will contain login username and password in plain text – this is only an example, I do recommend you storing credentials within an Azure Key Vault

Lets start off my creating an Azure resource group with a single postgresql server and database

resource "azurerm_resource_group" "resource_group" {
  name     = "tamops-postgres"
  location = "West Europe"
}

resource "azurerm_postgresql_server" "postgresserver" {
  name                = "postgresql-tamops"
  location            = azurerm_resource_group.resource_group.location
  resource_group_name = azurerm_resource_group.resource_group.name

  sku_name = "B_Gen5_2"

  storage_mb                   = 5120
  backup_retention_days        = 7
  geo_redundant_backup_enabled = false
  auto_grow_enabled            = true

  administrator_login          = "psqladmin"
  administrator_login_password = "H@Sh1CoR3!"
  version                      = "11"
  ssl_enforcement_enabled      = true
}

resource "azurerm_postgresql_database" "db" {
  name                = "dbtest"
  resource_group_name = azurerm_resource_group.resource_group.name
  server_name         = azurerm_postgresql_server.postgresserver.name
  charset             = "UTF8"
  collation           = "en-GB"
}

The above will create successfully as below:

To create additional read replicas of this database, I am going to using a for_each with a variable called replicas

Example of the variable replicas

variable "replicas" {
  description = "Names of additional replica databases to create"
  default     = [
  "replica1",
  "replica2"
  ]
}

Adding an additional postgresql server azurerm_postgresql_server resource and utilising for_each we can deploy the above two replicas

resource "azurerm_postgresql_server" "replica" {
  for_each = toset(var.replicas)

  name                = "postgresql-tamops-${each.key}"
  location            = azurerm_resource_group.resource_group.location
  resource_group_name = azurerm_resource_group.resource_group.name

  sku_name = "B_Gen5_2"
  version                      = "11"
  storage_mb                   = 5120

  backup_retention_days        = 7
  geo_redundant_backup_enabled = false 

  ssl_enforcement_enabled          = true

  administrator_login          = "psqladmin"
  administrator_login_password = "H@Sh1CoR3!"

  create_mode               = "Replica"
  creation_source_server_id = azurerm_postgresql_server.postgresserver.id
}

With successful deployment, two new postgresql resources appear in Azure

Checking the replication settings on the initial postgresql server, we can see the two new replicas:

Awesome! Hopefully this blog post assists you and gives you an initial overview on how to create an Azure postgresql server with additional replicas!

Full terraform used found in this GitHub repository

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s