-
Notifications
You must be signed in to change notification settings - Fork 1
/
database-private.tf
75 lines (57 loc) · 2.84 KB
/
database-private.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
############################################
## PostgresSQL Private Access - Resources ##
############################################
# Create Private DNS Zone Virtual Network Link for PostgreSQL
resource "azurerm_private_dns_zone_virtual_network_link" "private" {
name = "${lower(replace(var.company," ","-"))}-${var.app_name}-${var.environment}-vnet-link"
resource_group_name = data.azurerm_private_dns_zone.postgres_dns_zone.resource_group_name
private_dns_zone_name = data.azurerm_private_dns_zone.postgres_dns_zone.name
virtual_network_id = azurerm_virtual_network.this.id
depends_on = [ azurerm_subnet.postgres, azurerm_virtual_network.this ]
}
# Create Private PostgreSQL Flexible Server
resource "azurerm_postgresql_flexible_server" "private" {
name = "${lower(replace(var.company," ","-"))}-${var.app_name}-${var.environment}-postgresql-private"
location = azurerm_resource_group.this.location
resource_group_name = azurerm_resource_group.this.name
version = var.postgres_server_version
sku_name = var.postgres_server_sku
public_network_access_enabled = false
private_dns_zone_id = data.azurerm_private_dns_zone.postgres_dns_zone.id
delegated_subnet_id = azurerm_subnet.private.id
administrator_login = var.postgres_user
administrator_password = var.postgres_password
storage_mb = var.postgres_server_storage_mb
storage_tier = var.postgres_server_storage_tier
tags = var.tags
depends_on = [ azurerm_subnet.private, azurerm_private_dns_zone_virtual_network_link.private ]
}
# Create PostgreSQL Database
resource "azurerm_postgresql_flexible_server_database" "private" {
name = "${var.app_name}-${var.environment}-db-private"
server_id = azurerm_postgresql_flexible_server.private.id
charset = var.postgres_database_charset
collation = var.postgres_database_collation
# To prevent the possibility of accidental data loss change to true in production
lifecycle {
prevent_destroy = false
}
depends_on = [ azurerm_postgresql_flexible_server.private ]
}
# Enable PostgreSQL Extensions
resource "azurerm_postgresql_flexible_server_configuration" "extensions" {
name = "azure.extensions"
server_id = azurerm_postgresql_flexible_server.private.id
value = "VECTOR"
}
###########################################
## PostgresSQL Private Database - Output ##
###########################################
output "private_postgresql_server_name" {
value = azurerm_postgresql_flexible_server.private.name
description = "The name of the PostgreSQL Server"
}
output "private_postgresql_database_name" {
value = azurerm_postgresql_flexible_server_database.private.name
description = "The name of the PostgreSQL Database"
}