Deploy a PostgreSQL Flexible Server Database using Terraform
Article tested with the following Terraform and Terraform provider versions:
Terraform enables the definition, preview, and deployment of cloud infrastructure. Using Terraform, you create configuration files using HCL syntax. The HCL syntax allows you to specify the cloud provider - such as Azure - and the elements that make up your cloud infrastructure. After you create your configuration files, you create an execution plan that allows you to preview your infrastructure changes before they're deployed. Once you verify the changes, you apply the execution plan to deploy the infrastructure.
This article shows how to deploy a PostgreSQL Flexible Server Database using Terraform.
In this article, you learn how to:
- Create an Azure resource group using azurerm_resource_group
- Create an Azure virtual network (VNet) using azurerm_virtual_network
- Create an Azure Network Security Group (NSG) using azurerm_network_security_group
- Create an Azure subnet azurerm_subnet
- Create an Azure subnet Network Security Group (NSG) using azurerm_subnet_network_security_group_association
- Define a private DNS zone within an Azure DNS using azurerm_private_dns_zone
- Define a private DNS zone VNet link using using azurerm_private_dns_zone_virtual_network_link
- Deploy an Azure PostgreSQL Flexible Server on which the database runs using azurerm_postgresql_flexible_server
- Instantiate an Azure PostgreSQL database using azurerm_postgresql_flexible_server_database
Note
The example code in this article is located in the Azure Terraform GitHub repo.
1. Configure your environment
- Azure subscription: If you don't have an Azure subscription, create a free account before you begin.
Configure Terraform: If you haven't already done so, configure Terraform using one of the following options:
2. Implement the Terraform code
Create a directory in which to test and run the sample Terraform code and make it the current directory.
Create a file named
providers.tf
and insert the following code:terraform { required_version = ">=1.0" required_providers { azurerm = { source = "hashicorp/azurerm" version = "~>3.0" } random = { source = "hashicorp/random" version = ">= 3.4.0" } } } provider "azurerm" { features {} }
Create a file named
main.tf
and insert the following code to deploy the PostgreSQL Flexible Server on which the database runs.resource "random_pet" "name_prefix" { prefix = var.name_prefix length = 1 } resource "azurerm_resource_group" "default" { name = random_pet.name_prefix.id location = var.location } resource "azurerm_virtual_network" "default" { name = "${random_pet.name_prefix.id}-vnet" location = azurerm_resource_group.default.location resource_group_name = azurerm_resource_group.default.name address_space = ["10.0.0.0/16"] } resource "azurerm_network_security_group" "default" { name = "${random_pet.name_prefix.id}-nsg" location = azurerm_resource_group.default.location resource_group_name = azurerm_resource_group.default.name security_rule { name = "test123" priority = 100 direction = "Inbound" access = "Allow" protocol = "Tcp" source_port_range = "*" destination_port_range = "*" source_address_prefix = "*" destination_address_prefix = "*" } } resource "azurerm_subnet" "default" { name = "${random_pet.name_prefix.id}-subnet" virtual_network_name = azurerm_virtual_network.default.name resource_group_name = azurerm_resource_group.default.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_subnet_network_security_group_association" "default" { subnet_id = azurerm_subnet.default.id network_security_group_id = azurerm_network_security_group.default.id } resource "azurerm_private_dns_zone" "default" { name = "${random_pet.name_prefix.id}-pdz.postgres.database.azure.com" resource_group_name = azurerm_resource_group.default.name depends_on = [azurerm_subnet_network_security_group_association.default] } resource "azurerm_private_dns_zone_virtual_network_link" "default" { name = "${random_pet.name_prefix.id}-pdzvnetlink.com" private_dns_zone_name = azurerm_private_dns_zone.default.name virtual_network_id = azurerm_virtual_network.default.id resource_group_name = azurerm_resource_group.default.name } resource "random_password" "pass" { length = 20 } resource "azurerm_postgresql_flexible_server" "default" { name = "${random_pet.name_prefix.id}-server" resource_group_name = azurerm_resource_group.default.name location = azurerm_resource_group.default.location version = "13" delegated_subnet_id = azurerm_subnet.default.id private_dns_zone_id = azurerm_private_dns_zone.default.id administrator_login = "adminTerraform" administrator_password = random_password.pass.result zone = "1" storage_mb = 32768 sku_name = "GP_Standard_D2s_v3" backup_retention_days = 7 depends_on = [azurerm_private_dns_zone_virtual_network_link.default] }
Create a file named
postgresql-fs-db.tf
and insert the following code to instantiate the database:resource "azurerm_postgresql_flexible_server_database" "default" { name = "${random_pet.name_prefix.id}-db" server_id = azurerm_postgresql_flexible_server.default.id collation = "en_US.utf8" charset = "UTF8" }
Create a file named
variables.tf
and insert the following code:variable "name_prefix" { default = "postgresqlfs" description = "Prefix of the resource name." } variable "location" { default = "eastus" description = "Location of the resource." }
Create a file named
outputs.tf
and insert the following code to output the resource group name, Azure PostgreSQL server name, and Azure PostgreSQL database name:output "resource_group_name" { value = azurerm_resource_group.default.name } output "azurerm_postgresql_flexible_server" { value = azurerm_postgresql_flexible_server.default.name } output "postgresql_flexible_server_database_name" { value = azurerm_postgresql_flexible_server_database.default.name } output "postgresql_flexible_server_admin_password" { sensitive = true value = azurerm_postgresql_flexible_server.default.administrator_password }
3. Initialize Terraform
Run terraform init to initialize the Terraform deployment. This command downloads the Azure provider required to manage your Azure resources.
terraform init -upgrade
Key points:
- The
-upgrade
parameter upgrades the necessary provider plugins to the newest version that complies with the configuration's version constraints.
4. Create a Terraform execution plan
Run terraform plan to create an execution plan.
terraform plan -out main.tfplan
Key points:
- The
terraform plan
command creates an execution plan, but doesn't execute it. Instead, it determines what actions are necessary to create the configuration specified in your configuration files. This pattern allows you to verify whether the execution plan matches your expectations before making any changes to actual resources. - The optional
-out
parameter allows you to specify an output file for the plan. Using the-out
parameter ensures that the plan you reviewed is exactly what is applied.
5. Apply a Terraform execution plan
Run terraform apply to apply the execution plan to your cloud infrastructure.
terraform apply main.tfplan
Key points:
- The example
terraform apply
command assumes you previously ranterraform plan -out main.tfplan
. - If you specified a different filename for the
-out
parameter, use that same filename in the call toterraform apply
. - If you didn't use the
-out
parameter, callterraform apply
without any parameters.
6. Verify the results
Run az postgres flexible-server db show to display the Azure PostgreSQL database.
az postgres flexible-server db show --resource-group <resource_group_name> --server-name <server_name> --database-name <database_name>
Key points:
- The values for the
<resource_group_name>
,<server_name>
, and<database_name>
are displayed in theterraform apply
output.
7. Clean up resources
When you no longer need the resources created via Terraform, do the following steps:
Run terraform plan and specify the
destroy
flag.terraform plan -destroy -out main.destroy.tfplan
Key points:
- The
terraform plan
command creates an execution plan, but doesn't execute it. Instead, it determines what actions are necessary to create the configuration specified in your configuration files. This pattern allows you to verify whether the execution plan matches your expectations before making any changes to actual resources. - The optional
-out
parameter allows you to specify an output file for the plan. Using the-out
parameter ensures that the plan you reviewed is exactly what is applied.
- The
Run terraform apply to apply the execution plan.
terraform apply main.destroy.tfplan
Troubleshoot Terraform on Azure
Troubleshoot common problems when using Terraform on Azure