User Tools

Site Tools


terraformpostgresql

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
terraformpostgresql [2024/11/28 22:22] z0hpvkterraformpostgresql [2025/03/08 22:24] (current) – external edit 127.0.0.1
Line 1: Line 1:
 ===== Terraform with PostgreSQL ===== ===== Terraform with PostgreSQL =====
  
-==== Microsoft Azure ====+==== Microsoft Azure and PostgreSQL Flexible Server ====
  
 === Useful Links === === Useful Links ===
Line 10: Line 10:
 [https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/postgresql_flexible_server] [https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/postgresql_flexible_server]
  
-=== providers.tf === +The below commands will initialise Terraform, create an execution plan and apply it.\\ 
-<code yaml>+Afterwards you can clean up the resources created via the terraform destroy command.\\ 
 +<code> 
 +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 
 +</code> 
 + 
 +=== Terraform Code === 
 +== providers.tf == 
 +<file yaml providers.tf>
 terraform { terraform {
   required_version = ">=1.0"   required_version = ">=1.0"
Line 30: Line 43:
   features {}   features {}
 } }
 +</file>
  
-=== main.tf ===+== variables.tf =
 +<file yaml 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." 
 +
 +</file> 
 + 
 +== main.tf ==
 Uses existing resource group, virtual network and network security group Uses existing resource group, virtual network and network security group
  
-<code yaml>+<file yaml main.tf>
 resource "random_password" "pass" { resource "random_password" "pass" {
   length = 20   length = 20
Line 53: Line 90:
   backup_retention_days        = 7   backup_retention_days        = 7
 } }
-</code>+</file>
  
 +== postgresql-fs-db.tf ==
 +<file yaml 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
 +}
 +</file>
 +
 +== outputs.tf ==
 +<file yaml 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
 +}
 +</file>
terraformpostgresql.1732832550.txt.gz · Last modified: 2025/03/08 22:23 (external edit)