Setting up an Azure PostgreSQL Flexible Server with Terraform is a straightforward process. You'll need to create a resource group and a server, specifying the location, administrator credentials, and storage settings.
To create a resource group, you can use the `az group create` command, specifying the name, location, and tags. For example, `az group create --name my-resource-group --location westus2 --tags environment=dev`.
The Azure PostgreSQL Flexible Server can be created using the `az postgres flexible-server create` command. This command requires the resource group name, server name, administrator username, and password. For example, `az postgres flexible-server create --resource-group my-resource-group --name my-server --admin-username my-admin-username --admin-password my-admin-password`.
Once created, you can manage your Azure PostgreSQL Flexible Server using Terraform. This includes creating and managing databases, users, and roles, as well as configuring storage and performance settings.
Getting Started
Azure PostgreSQL Flexible Server is a managed database service that allows you to create a PostgreSQL server in minutes.
To get started, you'll need to create a PostgreSQL Flexible Server instance in the Azure portal. You can do this by selecting the "Create a resource" button and searching for "PostgreSQL Flexible Server".
You can choose from several pre-configured server sizes, each with its own set of resources and pricing. The smallest server size is 1 vCore with 1 GB of RAM, while the largest size is 32 vCores with 256 GB of RAM.
Before creating your server, you'll need to choose a location for it. This will determine the region where your server will be hosted and the latency for your users. You can choose from over 60 locations worldwide.
Once you've created your server, you'll need to configure it with a database user and password. This will allow you to connect to your database using a tool like psql. You can also configure your server to use a firewall rule to restrict access to your database.
Configuration
You can update a flexible server's sku using local context for server and resource group. This change also applies to data encryption, where you can update the key/identity, but keep in mind that data encryption cannot be enabled post server creation.
To update a flexible server's storage, you have a few options: you can enable or disable storage auto-grow, set custom storage performance tier, set IOPS (operations/sec) if you're using Premium SSD v2 Disks, or set Throughput (MB/sec) if you're using Premium SSD v2 Disks as well.
You can also schedule maintenance by specifying a period of time in UTC, such as "Sun:23:30" to schedule on Sunday, 11:30pm UTC, or you can set it back to default by passing in "Disabled".
What's New
With the new flexible Postgres server in Azure, you'll notice a significant change in managing AAD user roles. You'll use a different psql query, which is `CREATE ROLE "your_role_name" WITH NOSUPERUSER NOCREATEDB NOCREATEROLE INHERIT LOGIN;` instead of the classic single server query.
Managing a flexible Postgres server is surprisingly similar to managing a single PostgreSQL server. You can create the required role using Entra object identifier, with the command being `CREATE ROLE "your_role_name" FROM OBJECT ID 'your_oid';`.
The underlying server has been switched from Windows to Linux, which is a big improvement. You'll also notice improved IOPs and the introduction of burstable SKUs.
Terraform Configuration
Terraform is a popular tool for infrastructure as code, and its configuration is a crucial part of any infrastructure project.
Terraform uses HCL (HashiCorp Configuration Language) for its configuration files, which are written in a human-readable format.
A Terraform configuration file is typically named with a `.tf` extension and contains a list of resources to be created or managed.
Terraform configurations can include variables, which allow you to decouple configuration values from the code itself.
Variables can be defined in a separate file called `variables.tf` or directly in the configuration file.
A Terraform configuration file can also include modules, which are reusable blocks of code that encapsulate a set of resources and their dependencies.
Modules are useful for organizing and reusing complex configurations.
Terraform configurations can be organized into workspaces, which allow you to manage multiple environments, such as development, staging, and production.
Workspaces are useful for isolating changes and testing new configurations without affecting the production environment.
Parameters
The parameters for configuring a PostgreSQL Flexible Server are quite specific. You'll need to provide the name of the server, which must be unique across the entire Azure service.
The server ID is also required, and changing this will force a new server to be created. You'll also need to specify the charset and collation for the server, which defaults to UTF8 and en_US.utf8 respectively.
Here are the details of the parameters:
- name - (Required) The name which should be used for this PostgreSQL Flexible Server Database. Changing this forces a new Azure PostgreSQL Flexible Server Database to be created.
- server_id - (Required) The ID of the Azure PostgreSQL Flexible Server from which to create this PostgreSQL Flexible Server Database. Changing this forces a new Azure PostgreSQL Flexible Server Database to be created.
- charset - (Optional) Specifies the Charset for the Azure PostgreSQL Flexible Server Database, which needs to be a valid PostgreSQL Charset. Defaults to UTF8. Changing this forces a new Azure PostgreSQL Flexible Server Database to be created.
- collation - (Optional) Specifies the Collation for the Azure PostgreSQL Flexible Server Database, which needs to be a valid PostgreSQL Collation. Defaults to en_US.utf8. Changing this forces a new Azure PostgreSQL Flexible Server Database to be created.
These parameters are essential for setting up and configuring your PostgreSQL Flexible Server.
Resource Properties
In configuration, you'll often need to look up existing resources to get their current state.
You can look up an existing FlexibleServer resource by name, ID, or a combination of both.
To qualify the lookup, you can use extra properties, which are optional.
Getting an existing FlexibleServer resource's state is as simple as calling a function with the given name, ID, and optional extra properties.
The name and ID are used to uniquely identify the resource, making it easy to retrieve its state.
By using the right combination of name, ID, and extra properties, you can get the exact resource you need.
This approach ensures that you're working with the most up-to-date information, which is crucial for efficient configuration management.
Virtual Network Integration
A subnet is created and delegated to your flexible server for exclusive use.
This allows your server to be isolated and have its own dedicated space within the virtual network.
Several networking resources are provisioned in order for your flexible server to be available to other resources in your Azure virtual network.
This enables seamless communication and interaction between your server and other resources.
A private DNS zone is created and associated with your virtual network so your workloads can use the internal DNS reference.
This makes it easier for your workloads to communicate with each other and access resources within the virtual network.
Automated Updates
Automated updates are a game-changer for system administrators, and our service delivers on this front. The service performs automated patching of the underlying hardware, OS, and database engine.
This means you don't have to worry about manual updates, which can be time-consuming and prone to human error. Security and software updates are included as part of the automated patching process.
Minor version upgrades for the PostgreSQL engine are also included as part of the planned maintenance release. This ensures your database stays up-to-date and secure.
Management
Management is a breeze with Azure PostgreSQL Flexible Server. You can delete a flexible server using the command "az postgres flexible-server delete" along with one or more resource IDs.
To show connection strings for PgBouncer, you'll need to use the "az postgres flexible-server show-connection-strings" command. This is a straightforward way to access the connection strings you need.
Updating a flexible server to update private DNS zone for a VNET enabled server is also possible, and can be done by using private DNS zone in the same resource group and subscription.
Problem Statement
I've been in a similar situation where I'm trying to create a flexible PostgreSQL server and the required server roles for both AAD (Entra ID) and username-password authentication.
The problem is that there aren't many resources available that address this specific use case. I had to start from scratch and create a list of resources that I would need to create for this project.
Here are the resources I needed to create:
- a resource group to host all the resources
- a flexible Postgres server in UK East region
- a flexible Postgres server replica in a geo-paired region (UK West in our case)
- an AAD administrator for the server
- a couple of key vault secrets containing server admin username and password as well as AAD group's object id
- a host of firewall rules for the server and the replica
- a server configuration to enable certain azure extensions
- a terraform data block to run a script for user roles creation
- naming modules for the 2 servers and the resource group to conform to Microsoft Cloud Adoption Framework and provide consistency in terms of resource naming
It was a long list, but I knew I had to get it right in order to create a flexible and secure PostgreSQL server.
Execute
Execute is a crucial part of managing your PostgreSQL flexible server. You can execute a SQL file on your server using the `az postgres flexible-server execute` command.
To do this, you simply need to provide the path to the SQL file you want to execute.
Want to upgrade your server's major version? Simply use the `az postgres flexible-server upgrade` command with the name of your server and the desired major version.
For example, `az postgres flexible-server upgrade --name testsvr --version 16` will upgrade your server 'testsvr' to PostgreSQL major version 16.
You can also use the `az postgres flexible-server execute` command to run a SQL file, or the `az postgres flexible-server upgrade` command to upgrade your server's major version.
Here is a list of the commands you can use to execute, upgrade, and manage your PostgreSQL flexible server:
These commands are essential for managing your PostgreSQL flexible server and ensuring it runs smoothly.
Restart
Restarting a server can be a straightforward process, and in the case of an Az Postgres flexible server, it's as simple as running the command "az postgres flexible-server restart".
You have two options for restarting a server: planned failover or forced failover. This is determined by the type of failover you want to perform, which is specified with the "Forced" or "Planned" value.
Planned failover is a good option when you want to restart a server without disrupting ongoing operations. Forced failover, on the other hand, is necessary when you need to restart a server immediately, even if it means causing a temporary interruption.
The failover type is specified with the "Forced" or "Planned" value, which is a crucial consideration when restarting a server.
Wait
Wait can be a powerful tool in management, allowing you to pause and reflect before making decisions.
You can use the 'wait' command to pause the execution of a script until a certain condition is met, such as a server being created or updated.
For example, you can use 'az postgres flexible-server wait' to wait until a PostgreSQL flexible server is created with a 'provisioningState' of 'Succeeded'.
The polling interval in seconds can be customized to suit your needs, allowing you to adjust how frequently the script checks for the desired condition.
This can be particularly useful when working with complex systems that require time to process and update, such as when waiting for a server to be updated with a 'provisioningState' of 'Succeeded'.
High Availability
High availability is crucial for ensuring your database remains accessible and functional. This is achieved through flexible server deployment models that support high availability within a single availability zone and across multiple availability zones.
Data durability is also a key aspect of high availability. Storage maintains three locally redundant synchronous copies of the database files, ensuring that data is always available.
In production environments, high availability is set to true to ensure that the database remains accessible and functional. This is recommended for both small/medium-size and large-size production environments.
Here are the recommended settings for high availability in production environments:
Backup retention is also an important aspect of high availability, and is set to 30 days in production environments. This ensures that data is always available in case of a failure or loss.
Development
In a development environment, it's essential to set up PostgreSQL to the recommended version, which is 13.
This version is recommended for its stability and performance. I've found that using the latest recommended version helps catch any potential issues early on.
To keep costs low, we're using the Burstable SKU, which is the cheapest option available.
The B2s size is the smallest and cheapest available size for the Burstable tier, providing 2 vCores, 4GiB memory, and 1280 max iops.
Here's a summary of the development environment settings:
Frequently Asked Questions
What is Azure PostgreSQL flexible server?
Azure PostgreSQL flexible server is a fully managed database service that offers granular control and customization options for database management. It provides flexibility to tailor database settings to meet specific user requirements.
What is the difference between flexible server and single server in Azure Database?
Flexible Server in Azure Database offers granular control and customization, while Single Server provides a fully managed experience with minimal configuration options. This difference in management and customization capabilities sets them apart for different use cases and database needs.
What are the SKU names for Azure PostgreSQL flexible server?
The SKU names for Azure PostgreSQL flexible server are B4ms, B8ms, B12ms, B16ms, and B20ms. These burstable SKUs offer a low-cost solution for flexible CPU usage.
Sources
- https://medium.com/@fedosov.evg_70413/managing-azure-postgresql-flexible-servers-with-terraform-5bd549a0ef34
- https://learn.microsoft.com/en-us/cli/azure/postgres/flexible-server
- https://www.pulumi.com/registry/packages/azure/api-docs/postgresql/flexibleserver/
- https://shisho.dev/dojo/providers/azurerm/Database/azurerm-postgresql-flexible-server-database/
- https://www.massdriver.cloud/templates/azure-postgresql-flexible-server
Featured Images: pexels.com