Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Postgres DB and Add DB Variables to Middleware-Api #476

Merged
merged 14 commits into from
Dec 12, 2024
25 changes: 17 additions & 8 deletions ops/terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ module "networking" {
middlewaresubnetcidr = local.workspace["middlewaresubnetcidr"]
dbsubnetcidr = local.workspace["dbsubnetcidr"]
env = local.environment

# The DNS zone and DNS link are managed inside the networking module.
postgres_server_id = module.database.postgres_server_id


}

module "securitygroup" {
Expand Down Expand Up @@ -69,11 +74,15 @@ module "middleware_api" {
app_subnet_id = module.networking.middlewaresubnet_id

app_settings = {
WEBSITES_PORT = "8081"
WEBSITES_PORT = "8081"
POSTGRES_HOST = module.database.postgres_fqdn
POSTGRES_DB = module.database.postgres_db_name
POSTGRES_USER = module.database.postgres_user
POSTGRES_PASSWORD = module.vault.postgres_password
}

lb_subnet_id = module.networking.lbsubnet_id
health_path = "/actuator/health"
health_path = "/actuator/health"
env = local.environment
vnet = module.networking.network_name
sku_name = var.sku_name
Expand All @@ -93,12 +102,12 @@ module "ocr_api" {
WEBSITES_PORT = "8000"
}

lb_subnet_id = module.networking.middlewaresubnet_id
env = local.environment
vnet = module.networking.network_name
sku_name = var.sku_name
https_only = true
depends_on = [module.networking.ocrsubnet_id, module.networking.middlewaresubnet_id]
lb_subnet_id = module.networking.middlewaresubnet_id
env = local.environment
vnet = module.networking.network_name
sku_name = var.sku_name
https_only = true
depends_on = [module.networking.ocrsubnet_id, module.networking.middlewaresubnet_id]
}

module "ocr_autoscale" {
Expand Down
3 changes: 1 addition & 2 deletions ops/terraform/modules/app_service/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ variable "sku_name" {
}
variable "service" {}


variable "https_only" {
type = bool
default = false
Expand All @@ -22,4 +21,4 @@ variable "app_settings" {
}
variable "health_path" {
default = "/"
}
}
7 changes: 4 additions & 3 deletions ops/terraform/modules/database/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
output "postgres_server_id" {
value = azurerm_postgresql_flexible_server.postgres_flexible_server
value = azurerm_postgresql_flexible_server.postgres_flexible_server.id
}

output "postgres_fqdn" {
value = azurerm_postgresql_flexible_server.postgres_flexible_server
value = azurerm_postgresql_flexible_server.postgres_flexible_server.fqdn
description = "The fully qualified domain name (FQDN) of the PostgreSQL flexible server"
}

Expand All @@ -12,6 +12,7 @@ output "postgres_user" {
description = "User name for the Application's PostgreSQL flexible server database"
}


output "postgres_db_name" {
value = var.db_username
value = azurerm_postgresql_flexible_server.postgres_flexible_server.name
}
27 changes: 25 additions & 2 deletions ops/terraform/modules/network/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,19 @@ resource "azurerm_subnet" "middleware-subnet" {
}
}


resource "azurerm_subnet" "db-subnet" {
name = "${var.name}-db-subnet-${var.env}"
virtual_network_name = azurerm_virtual_network.vnet.name
resource_group_name = var.resource_group
address_prefixes = [var.dbsubnetcidr]

delegation {
name = "postgresql-delegation"
name = "postgresql-fs-delegation"
service_delegation {
name = "Microsoft.DBforPostgreSQL/flexibleServers"
actions = [
"Microsoft.Network/virtualNetworks/subnets/join/action",
]
}
}
}
Expand All @@ -87,4 +89,25 @@ resource "azurerm_private_dns_zone_virtual_network_link" "dns_link" {
resource_group_name = var.resource_group
private_dns_zone_name = azurerm_private_dns_zone.postgresql_dns_zone.name
virtual_network_id = azurerm_virtual_network.vnet.id
depends_on = [azurerm_subnet.db-subnet]
}

# Create private endpoint for SQL server
resource "azurerm_private_endpoint" "psql_db_pivate_endpoint" {
name = "psql-private-endpoint-${var.env}"
location = var.location
resource_group_name = var.resource_group
subnet_id = azurerm_subnet.db-subnet.id

private_service_connection {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is giving me an error on my local. not sure why
Screenshot 2024-12-11 at 3 54 54 PM

Copy link
Collaborator Author

@marycrawford marycrawford Dec 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the azurerm_private_endpoint resource, which can be an optional architectural design if we want a higher level security. To unblock the engineers and continue with e2e testing it is best to remove it. However, the communication of the network for the middleware and database respective delegations and app_settings allow it to communicate privately in the vnet.

name = "psql-private-serviceconnection-${var.env}"
private_connection_resource_id = var.postgres_server_id
subresource_names = ["psqlServer"]
is_manual_connection = false
}

private_dns_zone_group {
name = "dns-zone-group"
private_dns_zone_ids = [azurerm_private_dns_zone.postgresql_dns_zone.id]
}
}
5 changes: 5 additions & 0 deletions ops/terraform/modules/network/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ variable "dbsubnetcidr" {}
variable "location" {
default = "eastus2"
}

variable "postgres_server_id" {
marycrawford marked this conversation as resolved.
Show resolved Hide resolved
description = "The ID of the PostgreSQL server"
type = string
}
2 changes: 1 addition & 1 deletion ops/terraform/modules/vault/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ resource "random_string" "postgres_password" {
override_special = "_!@#-$%^&*()[]{}" # excluded characters
}

resource "azurerm_key_vault_secret" "postgres_db_secret" {
resource "azurerm_key_vault_secret" "postgres_db_password" {
name = "reportvision-postgres-db-password"
value = random_string.postgres_password.result
key_vault_id = azurerm_key_vault.this.id
Expand Down
5 changes: 3 additions & 2 deletions ops/terraform/modules/vault/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
output "postgres_password" {
value = random_string.postgres_password.result
sensitive = true
value = random_string.postgres_password.result
sensitive = true
description = "The randomly generated password for the PostgreSQL database"
}
4 changes: 4 additions & 0 deletions ops/terraform/variables.tf
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
variable "client_id" {}
variable "name" {}
variable "postgres_server_id" {}
marycrawford marked this conversation as resolved.
Show resolved Hide resolved
variable "object_id" {}
variable "tenant_id" {}

variable "sku_name" {
type = string
description = "The Azure Stock Keep Unit (SKU) version"
}

variable "subscription_id" {}

variable "resource_group_name" {
description = "value of the Azure resource group to deploy to"
}
Loading