===== Terraform with PostgreSQL =====
==== Microsoft Azure and PostgreSQL Flexible Server ====
=== Useful Links ===
[https://learn.microsoft.com/en-us/azure/developer/terraform/get-started-windows-powershell]\\
[https://learn.microsoft.com/en-us/azure/developer/terraform/authenticate-to-azure-with-microsoft-account]\\
[https://learn.microsoft.com/en-us/azure/developer/terraform/deploy-postgresql-flexible-server-database?tabs=azure-cli]\\
[https://github.com/Azure/terraform/tree/master/quickstart/201-postgresql-fs-db]\\
[https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/postgresql_flexible_server]
The below commands will initialise Terraform, create an execution plan and apply it.\\
Afterwards you can clean up the resources created via the terraform destroy command.\\
terraform init -upgrade
terraform validate
terraform plan -out main.tfplan
terraform apply main.tfplan
terraform plan -destroy -out main.destroy.tfplan
terraform apply main.destroy.tfplan
=== Terraform Code ===
== providers.tf ==
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 {}
}
== variables.tf ==
variable "resource_group" {
default = "DoobDude-WebServer_group"
description = "Default Resource Group."
}
variable "virtual_network" {
default = "DoobDude-WebServer-vnet"
description = "Default Virtual Network."
}
variable "nsg" {
default = "DoobDude-WebServer-nsg"
description = "Default Network Security Group."
}
variable "location" {
default = "UK South"
description = "Location of the resource."
}
== main.tf ==
Uses existing resource group, virtual network and network security group
resource "random_password" "pass" {
length = 20
}
resource "azurerm_postgresql_flexible_server" "default" {
name = "doob-tf-pg"
resource_group_name = var.resource_group
location = var.location
version = "16"
public_network_access_enabled = true
administrator_login = "adminTerraform"
administrator_password = random_password.pass.result
zone = "1"
storage_mb = 32768
storage_tier = "P4"
sku_name = "B_Standard_B1ms"
backup_retention_days = 7
}
== postgresql-fs-db.tf ==
locals {
pg_config = {
"log_lock_waits" : "on",
"log_min_duration_statement" : "500"
"log_statement" : "ddl",
"default_statistics_target" : "250",
"effective_io_concurrency" : "50"
}
}
resource "azurerm_postgresql_flexible_server_database" "default" {
name = "doobtf"
collation = "en_US.utf8"
charset = "UTF8"
server_id = azurerm_postgresql_flexible_server.default.id
}
resource "azurerm_postgresql_flexible_server_configuration" "default" {
for_each = local.pg_config
name = each.key
value = each.value
server_id = azurerm_postgresql_flexible_server.default.id
}
== outputs.tf ==
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
}