Enabling PostgreSQL flexible server logs and configuring a retention period using Terraform

In this blog post, we will look at enabling PostgreSQL flexible server logs and setting a retention period using Terraform. (Somewhat a new feature (was in Preview), the ability to enable server logs.)

Deploying a sample server below, we can see within “Server Logs” that it currently isn’t enabled by default.

Reviewing the linked learn.microsoft.com docs:

You can configure your server logs in the same way as above using the Server Parameters, setting the appropriate values for these parameters: logfiles.download_enable to ON to enable this feature, and logfiles.retention_days to define retention in days. Initially, server logs occupy data disk space for about an hour before moving to backup storage for the set retention period.

https://learn.microsoft.com/en-us/azure/postgresql/flexible-server/how-to-server-logs-portal

Terraform caters nicely for server parameters by using the resource: azurerm_postgresql_flexible_server_configuration ( https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/postgresql_flexible_server_configuration). By tweaking the Server Parameters, specifically setting logfiles.download_enable to ON and logfiles.retention_days to define the retention period, you’re all set.

The Terraform

I initially created the flexible server: tamops-psqlflexibleserver using the below Terraform

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "tamopsrg" {
  name     = "tamops-psql-logs"
  location = "UK South"
}

resource "azurerm_postgresql_flexible_server" "tamopspsql" {
  name                   = "tamops-psqlflexibleserver"
  resource_group_name    = azurerm_resource_group.tamopsrg.name
  location               = azurerm_resource_group.tamopsrg.location
  version                = "15"
  administrator_login    = "psqladmin"
  administrator_password = "H@Sh1CoR3!"
  zone                  = "2"

  storage_mb = 32768

  sku_name = "GP_Standard_D4s_v3"
}

Now lets enable and configure retention period for server logging using: azurerm_postgresql_flexible_server_configuration

In the below, I am enabling server logs to be downloaded and retention period of 5 days:

resource "azurerm_postgresql_flexible_server_configuration" "logfiles_download_enable" {
  name      = "logfiles.download_enable"
  server_id = azurerm_postgresql_flexible_server.tamopspsql.id
  value     = "ON"
}

resource "azurerm_postgresql_flexible_server_configuration" "logfiles_retention_days" {
  name      = "logfiles.retention_days"
  server_id = azurerm_postgresql_flexible_server.tamopspsql.id
  value     = "5"
}

Once ran, reviewing the Azure Portal – we can now see Download server logs = Enabled and retention set to 5 days

Awesome! Hopefully this blog will assist you in enabling download server logs and setting retention period for PostgreSQL Flexible server

GitHub repository containing example Terraform

Leave a Reply

Discover more from Thomas Thornton Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading

Discover more from Thomas Thornton Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading