|
| 1 | +data "azurerm_private_dns_zone" "postgres" { |
| 2 | + count = var.features.private_networking ? 1 : 0 |
| 3 | + |
| 4 | + provider = azurerm.hub |
| 5 | + |
| 6 | + name = "privatelink.postgres.database.azure.com" |
| 7 | + resource_group_name = "rg-hub-${var.hub}-uks-private-dns-zones" |
| 8 | +} |
| 9 | + |
| 10 | +# Don't deploy if deploy_database_as_container is true |
| 11 | +module "postgres" { |
| 12 | + count = var.deploy_database_as_container ? 0 : 1 |
| 13 | + |
| 14 | + source = "../dtos-devops-templates/infrastructure/modules/postgresql-flexible" |
| 15 | + |
| 16 | + # postgresql Server |
| 17 | + name = "postgres-${var.app_short_name}-${var.environment}-uks" |
| 18 | + resource_group_name = azurerm_resource_group.main.name |
| 19 | + location = var.region |
| 20 | + |
| 21 | + backup_retention_days = var.postgres_backup_retention_days |
| 22 | + geo_redundant_backup_enabled = var.postgres_geo_redundant_backup_enabled |
| 23 | + postgresql_admin_object_id = data.azuread_group.postgres_sql_admin_group.object_id |
| 24 | + postgresql_admin_principal_name = var.postgres_sql_admin_group |
| 25 | + postgresql_admin_principal_type = "Group" |
| 26 | + administrator_login = local.database_user |
| 27 | + admin_identities = [module.db_connect_identity[0]] |
| 28 | + |
| 29 | + # Diagnostic Settings |
| 30 | + log_analytics_workspace_id = var.log_analytics_workspace_audit_id |
| 31 | + monitor_diagnostic_setting_postgresql_server_enabled_logs = ["PostgreSQLLogs", "PostgreSQLFlexSessions", "PostgreSQLFlexQueryStoreRuntime", "PostgreSQLFlexQueryStoreWaitStats", "PostgreSQLFlexTableStats", "PostgreSQLFlexDatabaseXacts"] |
| 32 | + monitor_diagnostic_setting_postgresql_server_metrics = ["AllMetrics"] |
| 33 | + |
| 34 | + sku_name = var.postgres_sku_name |
| 35 | + storage_mb = var.postgres_storage_mb |
| 36 | + storage_tier = var.postgres_storage_tier |
| 37 | + |
| 38 | + server_version = "16" |
| 39 | + tenant_id = data.azurerm_client_config.current.tenant_id |
| 40 | + |
| 41 | + private_endpoint_properties = var.features.private_networking ? { |
| 42 | + private_dns_zone_ids_postgresql = [data.azurerm_private_dns_zone.postgres[0].id] |
| 43 | + private_endpoint_enabled = true |
| 44 | + private_endpoint_subnet_id = var.postgres_subnet_id |
| 45 | + private_endpoint_resource_group_name = azurerm_resource_group.main.name |
| 46 | + private_service_connection_is_manual = false |
| 47 | + } : null |
| 48 | + |
| 49 | + databases = { |
| 50 | + db1 = { |
| 51 | + collation = "en_US.utf8" |
| 52 | + charset = "UTF8" |
| 53 | + max_size_gb = 10 |
| 54 | + name = local.database_name |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + tags = {} |
| 59 | +} |
| 60 | + |
| 61 | +module "db_connect_identity" { |
| 62 | + count = var.deploy_database_as_container ? 0 : 1 |
| 63 | + |
| 64 | + source = "../dtos-devops-templates/infrastructure/modules/managed-identity" |
| 65 | + resource_group_name = azurerm_resource_group.main.name |
| 66 | + location = var.region |
| 67 | + uai_name = "mi-${var.app_short_name}-${var.environment}-db-connect" |
| 68 | +} |
| 69 | + |
| 70 | +resource "random_password" "admin_password" { |
| 71 | + count = var.deploy_database_as_container ? 1 : 0 |
| 72 | + |
| 73 | + length = 30 |
| 74 | + special = true |
| 75 | + override_special = "!@#$%^&*()-_=+" |
| 76 | +} |
| 77 | + |
| 78 | +module "database_container" { |
| 79 | + count = var.deploy_database_as_container ? 1 : 0 |
| 80 | + |
| 81 | + providers = { |
| 82 | + azurerm = azurerm |
| 83 | + azurerm.hub = azurerm.hub |
| 84 | + } |
| 85 | + |
| 86 | + source = "../dtos-devops-templates/infrastructure/modules/container-app" |
| 87 | + name = "${var.app_short_name}-db-${var.environment}" |
| 88 | + container_app_environment_id = var.container_app_environment_id |
| 89 | + docker_image = "postgres:16" |
| 90 | + secret_variables = var.deploy_database_as_container ? { POSTGRES_PASSWORD = resource.random_password.admin_password[0].result } : {} |
| 91 | + environment_variables = { |
| 92 | + POSTGRES_USER = local.database_user |
| 93 | + POSTGRES_DB = local.database_name |
| 94 | + } |
| 95 | + resource_group_name = azurerm_resource_group.main.name |
| 96 | + is_tcp_app = true |
| 97 | + # postgres has a port of 5432 |
| 98 | + port = 5432 |
| 99 | + exposed_port = local.database_port |
| 100 | +} |
0 commit comments