Skip to content

Commit 4a53d19

Browse files
rfk-ncmrlockstarpatrickmoore-nc
authored
feat: DTOSS-5834 Create new PostgreSQL module (#54)
* PostgreSql Module initial commit * updating postgresdb module * wip * wip * Default APIM Entra ID auth to MSAL-2 --------- Co-authored-by: Alastair Lock <[email protected]> Co-authored-by: patrickmoore-nc <[email protected]>
1 parent d7e0176 commit 4a53d19

File tree

5 files changed

+238
-1
lines changed

5 files changed

+238
-1
lines changed

infrastructure/modules/api-management/variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ variable "client_id" {
240240
variable "client_library" {
241241
description = "The client library for the API Management AAD Identity Provider."
242242
type = string
243-
default = "MSAL"
243+
default = "MSAL-2"
244244
}
245245

246246
variable "client_secret" {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
resource "azurerm_postgresql_flexible_server_database" "postgresql_flexible_db" {
3+
for_each = var.databases
4+
5+
name = each.value.name
6+
server_id = azurerm_postgresql_flexible_server.postgresql_flexible_server.id
7+
charset = each.value.charset
8+
collation = each.value.collation
9+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
resource "azurerm_postgresql_flexible_server" "postgresql_flexible_server" {
2+
name = var.name
3+
resource_group_name = var.resource_group_name
4+
location = var.location
5+
6+
7+
public_network_access_enabled = var.public_network_access_enabled
8+
sku_name = var.sku_name
9+
storage_mb = var.storage_mb
10+
storage_tier = var.storage_tier
11+
version = var.server_version
12+
zone = var.zone
13+
14+
backup_retention_days = var.backup_retention_days
15+
geo_redundant_backup_enabled = var.geo_redundant_backup_enabled
16+
17+
authentication {
18+
active_directory_auth_enabled = true
19+
password_auth_enabled = false
20+
tenant_id = var.tenant_id
21+
}
22+
23+
# Postgres Flexible Server does not support User Assigned Identity
24+
# so do not enable for now. If required, create the identity in an
25+
# associated identity module and reference it here.
26+
#
27+
# identity {
28+
# type = "SystemAssigned"
29+
# }
30+
31+
tags = var.tags
32+
}
33+
34+
# Create the Active Directory Administrator for the Postgres Flexible Server
35+
resource "azurerm_postgresql_flexible_server_active_directory_administrator" "postgresql_admin" {
36+
server_name = azurerm_postgresql_flexible_server.postgresql_flexible_server.name
37+
resource_group_name = var.resource_group_name
38+
tenant_id = var.tenant_id
39+
object_id = var.postgresql_admin_object_id
40+
principal_name = var.postgresql_admin_principal_name
41+
principal_type = var.postgresql_admin_principal_type
42+
}
43+
44+
# Create the server configurations
45+
resource "azurerm_postgresql_flexible_server_configuration" "postgresql_flexible_config" {
46+
for_each = var.postgresql_configurations
47+
48+
server_id = azurerm_postgresql_flexible_server.postgresql_flexible_server.id
49+
50+
name = each.key
51+
value = each.value
52+
}
53+
54+
/* --------------------------------------------------------------------------------------------------
55+
Private Endpoint Configuration for Postgres Flexible Server
56+
-------------------------------------------------------------------------------------------------- */
57+
58+
module "private_endpoint_postgresql_flexible_server" {
59+
count = var.private_endpoint_properties.private_endpoint_enabled ? 1 : 0
60+
61+
source = "../private-endpoint"
62+
63+
name = "${var.name}-postgresql-private-endpoint"
64+
resource_group_name = var.private_endpoint_properties.private_endpoint_resource_group_name
65+
location = var.location
66+
subnet_id = var.private_endpoint_properties.private_endpoint_subnet_id
67+
68+
private_dns_zone_group = {
69+
name = "${var.name}-postgresql-private-endpoint-zone-group"
70+
private_dns_zone_ids = var.private_endpoint_properties.private_dns_zone_ids_postgresql
71+
}
72+
73+
private_service_connection = {
74+
name = "${var.name}-postgresql-private-endpoint-connection"
75+
private_connection_resource_id = azurerm_postgresql_flexible_server.postgresql_flexible_server.id
76+
subresource_names = ["postgresqlServer"]
77+
is_manual_connection = var.private_endpoint_properties.private_service_connection_is_manual
78+
}
79+
80+
tags = var.tags
81+
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
variable "name" {
2+
description = "The name of the PostgreSQL Flexible Server."
3+
type = string
4+
}
5+
6+
variable "resource_group_name" {
7+
description = "The name of the resource group in which to create the PostgreSQL Flexible Server."
8+
type = string
9+
}
10+
11+
variable "location" {
12+
description = "The location/region where the PostgreSQL Flexible Server is created."
13+
type = string
14+
}
15+
16+
variable "backup_retention_days" {
17+
description = "The number of days to retain backups for the PostgreSQL Flexible Server."
18+
type = number
19+
}
20+
21+
variable "geo_redundant_backup_enabled" {
22+
description = "Whether geo-redundant backup is enabled for the PostgreSQL Flexible Server."
23+
type = bool
24+
}
25+
26+
variable "postgresql_admin_object_id" {
27+
description = "The object ID of the PostgreSQL Active Directory administrator."
28+
type = string
29+
}
30+
31+
variable "postgresql_admin_principal_name" {
32+
description = "The principal name of the PostgreSQL Active Directory administrator."
33+
type = string
34+
}
35+
36+
variable "postgresql_admin_principal_type" {
37+
description = "The principal type of the PostgreSQL Active Directory administrator."
38+
type = string
39+
}
40+
41+
variable "public_network_access_enabled" {
42+
description = "Whether public network access is enabled for the PostgreSQL Flexible Server."
43+
type = bool
44+
default = false
45+
}
46+
47+
variable "sku_name" {
48+
# See: https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/postgresql_flexible_server#sku_name-2
49+
description = "The SKU name for the PostgreSQL Flexible Server."
50+
type = string
51+
}
52+
53+
variable "storage_mb" {
54+
# See: https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/postgresql_flexible_server#storage_mb-2
55+
description = "The storage size in MB for the PostgreSQL Flexible Server."
56+
type = number
57+
default = 32768
58+
59+
validation {
60+
condition = contains([32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4193280, 4194304, 8388608, 16777216, 33553408], var.storage_mb)
61+
error_message = "The storage size must be one of the following: 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4193280, 4194304, 8388608, 16777216, 33553408."
62+
}
63+
}
64+
65+
variable "storage_tier" {
66+
# See defaults: https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/postgresql_flexible_server#storage_tier-defaults-based-on-storage_mb
67+
description = "The storage tier for the PostgreSQL Flexible Server."
68+
type = string
69+
default = "P4"
70+
71+
validation {
72+
condition = contains(["P4", "P6", "P10", "P15", "P20", "P30", "P40", "P50", "P60", "P70", "P80"], var.storage_tier)
73+
error_message = "The storage tier must be one of the following: P4, P6, P10, P15, P20, P30, P40, P50, P60, P70, P80."
74+
}
75+
}
76+
77+
variable "server_version" {
78+
description = "The version of the PostgreSQL server."
79+
type = string
80+
default = "16"
81+
82+
validation {
83+
condition = contains(["11", "12", "13", "14", "15", "16"], var.server_version)
84+
error_message = "The server version must be one of the following: 11, 12, 13, 14, 15, 16."
85+
}
86+
}
87+
88+
variable "tenant_id" {
89+
description = "The tenant ID for the Azure Active Directory."
90+
type = string
91+
}
92+
93+
variable "zone" {
94+
# See: https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/postgresql_flexible_server#zone-2
95+
description = "The availability zone for the PostgreSQL Flexible Server. Azure will automatically assign an Availability Zone if one is not specified."
96+
type = string
97+
default = null
98+
}
99+
100+
variable "tags" {
101+
description = "A map of tags to assign to the PostgreSQL Flexible Server."
102+
type = map(string)
103+
}
104+
105+
# Databases
106+
variable "databases" {
107+
description = "A map of databases to create on the PostgreSQL Flexible Server."
108+
type = map(object({
109+
collation = string
110+
charset = string
111+
max_size_gb = number
112+
name = string
113+
}))
114+
}
115+
116+
# Server configurations
117+
variable "postgresql_configurations" {
118+
description = "A map of PostgreSQL configurations to apply to the PostgreSQL Flexible Server."
119+
type = map(string)
120+
default = {}
121+
}
122+
123+
# Private Endpoint Properties
124+
variable "private_endpoint_properties" {
125+
description = "Consolidated properties for the PostgreSql Private Endpoint."
126+
type = object({
127+
private_dns_zone_ids_postgresql = optional(list(string), [])
128+
private_endpoint_enabled = optional(bool, false)
129+
private_endpoint_subnet_id = optional(string, "")
130+
private_endpoint_resource_group_name = optional(string, "")
131+
private_service_connection_is_manual = optional(bool, false)
132+
})
133+
}
134+
135+
# /* --------------------------------------------------------------------------------------------------
136+
# Auditing and Diagnostics Variables
137+
# -------------------------------------------------------------------------------------------------- */
138+
# variable "monitor_diagnostic_setting_database_enabled_logs" {
139+
# type = list(string)
140+
# description = "Controls what logs will be enabled for the database"
141+
# }
142+
143+
# variable "monitor_diagnostic_setting_database_metrics" {
144+
# type = list(string)
145+
# description = "Controls what metrics will be enabled for the database"
146+
# }

infrastructure/modules/shared-config/output.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ locals {
7878
managed-devops-pool = lower("private-pool-${var.env}-${var.location_map[var.location]}")
7979
network-interface = upper("${var.env}-${var.location_map[var.location]}-${var.application}")
8080
network-security-group = upper("NSG-${var.env}-${var.location_map[var.location]}-${var.application}")
81+
postgres-sql-server = lower("postgres-${var.application}-${var.env}-${var.location_map[var.location]}")
8182
private-ssh-key = lower("ssh-pri-${var.env}${var.location_map[var.location]}${var.application}")
8283
public-ip-address = lower("PIP-${var.env}-${var.location_map[var.location]}-${var.application}")
8384
public-ip-dns = lower("${var.env}${var.location_map[var.location]}${var.application}")

0 commit comments

Comments
 (0)