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

Create postgresql server and db with relevant resources #393

Merged
merged 30 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
67840d0
creating database module and resources for postgresql server and db
Nov 13, 2024
e51d31a
removed commented out code
Nov 13, 2024
2f5a345
making module headers consistent
Nov 13, 2024
24ad5e3
remove azurerm_postgresql_flexible_server resources and using a singl…
Nov 13, 2024
64d5ed9
update resource notes in modules/database/main.tf
Nov 13, 2024
2ebf0f7
save randomly created postgres db login password in azure key vault
Nov 14, 2024
8875095
add client_id and object_id for vault
Nov 14, 2024
79f013d
fix errors
Nov 14, 2024
095f8d9
update database with network subnet and vault with object_id and vite…
Nov 18, 2024
a884392
update subnet used
Nov 18, 2024
9f4f1c1
update how we are consuming the vite_api_url and object_id variables
Nov 18, 2024
cb224ce
remove the data.tf files from the database and vault modules, use mai…
Nov 18, 2024
537ee13
remove duplicate variables and add descriptions
Nov 18, 2024
1878571
fix syntax error and put variable descriptions
Nov 18, 2024
0916362
create subnet for db and update tf code
Nov 19, 2024
b7b24e0
modify and clean up code
Nov 20, 2024
34025ba
refactor code to fix error
Nov 20, 2024
9da59a2
update db with postgresql_flexible_server since single server will be…
Nov 20, 2024
d082319
update note regarding retiring azurerm_postgresql_server in March 2025
Nov 20, 2024
11a797a
reverse postgres_flex_server changes and comment them out
Nov 20, 2024
196eca7
update db to postgresql flexible server and add postgresql dns zone n…
Nov 21, 2024
78e199c
remove azurerm_postgresql_server code
Nov 21, 2024
d201fa5
clean up code by removing unused and commented out code
Nov 21, 2024
8f4f912
remove more commented out code
Nov 21, 2024
6b81cc2
remove variables used when working on vault module
Nov 21, 2024
2d52961
add back client_id and tenant_id variables to support the azuread pro…
Nov 21, 2024
2423ed1
update database name
Nov 21, 2024
47014cd
reduce the sku_name to Standard_B1ms
Nov 21, 2024
e51c5b5
remove unused variables
Nov 24, 2024
927d60b
update database cidr block
Nov 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion ops/terraform/locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ locals {
appsubnetcidr = "10.0.1.0/24"
websubnetcidr = "10.0.2.0/24"
lbsubnetcidr = "10.0.3.0/24"
dbsubnetcidr = "10.0.4.0/24"
}
}
demo = {
Expand All @@ -18,6 +19,7 @@ locals {
appsubnetcidr = "10.1.1.0/24"
websubnetcidr = "10.1.2.0/24"
lbsubnetcidr = "10.1.3.0/24"
dbsubnetcidr = "10.0.4.0/24"
Copy link
Collaborator

@derekadombek derekadombek Nov 25, 2024

Choose a reason for hiding this comment

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

just notice this as well. this cidr will probably need to be 10.1.4.0/24 and not 10.0.4.0/24 to eliminate the chance of crossing on top of each other when both environments are running and to match the pattern of the rest of the cidr's.

}
}
}
}
51 changes: 10 additions & 41 deletions ops/terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ locals {
}
}

##########
## 02-network
##########
module "networking" {
source = "./modules/network"
name = var.name
Expand All @@ -20,22 +17,19 @@ module "networking" {
websubnetcidr = local.workspace["websubnetcidr"]
lbsubnetcidr = local.workspace["lbsubnetcidr"]
appsubnetcidr = local.workspace["appsubnetcidr"]
dbsubnetcidr = local.workspace["dbsubnetcidr"]
env = local.environment
}

##########
## 02-security
##########

module "securitygroup" {
source = "./modules/security"
name = var.name
location = data.azurerm_resource_group.rg.location
resource_group = data.azurerm_resource_group.rg.name
web_subnet_id = module.networking.websubnet_id
# db_subnet_id = module.networking.dbsubnet_id
lb_subnet_id = module.networking.lbsubnet_id
env = local.environment
db_subnet_id = module.networking.dbsubnet_id
lb_subnet_id = module.networking.lbsubnet_id
env = local.environment
}

module "app_gateway" {
Expand All @@ -53,10 +47,6 @@ module "app_gateway" {
depends_on = [module.networking, module.ocr_api]
}

##########
## 05-Persistent
##########

module "storage" {
source = "./modules/storage"
name = var.name
Expand All @@ -68,10 +58,6 @@ module "storage" {
web_subnet_id = module.networking.websubnet_id
}

##########
## 06-App
##########

module "ocr_api" {
source = "./modules/app_service"
name = var.name
Expand All @@ -98,26 +84,9 @@ module "ocr_autoscale" {
weekend_capacity_instances = 1
}

# module "compute" {
# source = "./modules/container_instances"
# location = data.azurerm_resource_group.rg.location
# resource_group = data.azurerm_resource_group.rg.name
# environment = local.environment
# app_subnet = module.networking.appsubnet_id
# # web_subnet_id = module.networking.websubnet_id
# # app_subnet_id = module.networking.appsubnet_id
# # web_host_name = local.app.web_host_name
# # web_username = local.app.web_username
# # web_os_password = local.app.web_os_password
# # app_host_name = local.app.app_host_name
# # app_username = local.app.app_username
# # app_os_password = local.app.app_os_password
# }

##########
## 04-config
##########

##########
## 07-Monitor
##########
module "database" {
source = "./modules/database"
resource_group_name = data.azurerm_resource_group.rg.name
subnet = module.networking.dbsubnet_id
private_dns_zone_id = module.networking.private_dns_zone_id
}
37 changes: 37 additions & 0 deletions ops/terraform/modules/database/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Azure Postgres Single Service (azurerm_postgresql_server) retires in March 2025.
# As a result we are using Azure Database for PostgreSQL Flexible Server
# with granular control, flexibility and better cost optimization.
resource "azurerm_postgresql_flexible_server" "postgres_flexible_server" {
name = "reportvisionpostgresql-flexible-server"
location = var.location
resource_group_name = var.resource_group_name
sku_name = var.postgres_sku_name
version = var.engine_version
storage_mb = 32768 # 32 GB, the lowest of the valid options
backup_retention_days = 7

administrator_login = var.db_username
administrator_password = random_string.setup_rds_password.result
delegated_subnet_id = var.subnet
private_dns_zone_id = var.private_dns_zone_id

# Disable Public Network Access
public_network_access_enabled = false

lifecycle {
prevent_destroy = true
}
}

resource "azurerm_postgresql_flexible_server_database" "postgres_db" {
name = "${azurerm_postgresql_flexible_server.postgres_flexible_server.name}-db"
server_id = azurerm_postgresql_flexible_server.postgres_flexible_server.id
}

# Random string resource for the postgres password
resource "random_string" "setup_rds_password" {
length = 16 # Length of the password

# Character set that excludes problematic characters like quotes, backslashes, etc.
override_special = "_!@#-$%^&*()[]{}"
}
3 changes: 3 additions & 0 deletions ops/terraform/modules/database/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "postgres_db_password" {
value = azurerm_postgresql_flexible_server.postgres_flexible_server.administrator_login
}
37 changes: 37 additions & 0 deletions ops/terraform/modules/database/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
variable "db_username" {
type = string
description = "Username of RDS Instance."
default = "reportVisionDbUser"
}

variable "engine_version" {
description = "Postgres DB engine version."
default = "11"
}

variable "location" {
type = string
description = "Location of the resource."
default = "eastus2"
}

variable "resource_group_name" {
type = string
description = "The Azure Resource Group to deploy to"
}

variable "postgres_sku_name" {
type = string
description = "value"
default = "Standard_B1ms"
}

variable "subnet" {
type = string
description = "The subnet ID to associate with the PostgreSQL Flexible Server"
}

variable "private_dns_zone_id" {
type = string
description = "Private DNS Zone for PostgreSQL Flexible Server"
}
37 changes: 29 additions & 8 deletions ops/terraform/modules/network/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ resource "azurerm_subnet" "web-subnet" {
virtual_network_name = azurerm_virtual_network.vnet.name
resource_group_name = var.resource_group
address_prefixes = [var.websubnetcidr]
service_endpoints = [
service_endpoints = [
"Microsoft.Storage",
"Microsoft.Web"
]
depends_on = [azurerm_virtual_network.vnet]
depends_on = [azurerm_virtual_network.vnet]
}

resource "azurerm_subnet" "app-subnet" {
Expand Down Expand Up @@ -45,9 +45,30 @@ resource "azurerm_subnet" "lb-subnet" {
depends_on = [azurerm_virtual_network.vnet]
}

# 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]
# }

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"
service_delegation {
name = "Microsoft.DBforPostgreSQL/flexibleServers"
}
}
}

resource "azurerm_private_dns_zone" "postgresql_dns_zone" {
name = "privatelink.postgres.database.azure.com"
resource_group_name = var.resource_group
}

# Link Private DNS Zone to Virtual Network
resource "azurerm_private_dns_zone_virtual_network_link" "dns_link" {
name = "postgresql-vnet-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
}
15 changes: 10 additions & 5 deletions ops/terraform/modules/network/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ output "websubnet_id" {
description = "Id of websubnet in the network"
}

# output "dbsubnet_id" {
# value = azurerm_subnet.db-subnet.id
# description = "Id of dbsubnet in the network"
# }
output "dbsubnet_id" {
value = azurerm_subnet.db-subnet.id
description = "Id of dbsubnet in the network"
}

output "lbsubnet_id" {
value = azurerm_subnet.lb-subnet.id
Expand All @@ -21,4 +21,9 @@ output "lbsubnet_id" {
output "appsubnet_id" {
value = azurerm_subnet.app-subnet.id
description = "Id of lbsubnet in the network"
}
}

output "private_dns_zone_id" {
value = azurerm_private_dns_zone.postgresql_dns_zone.id
description = "Private DNS Zone for PostgreSQL Flexible Server"
}
9 changes: 6 additions & 3 deletions ops/terraform/modules/network/variables.tf
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
variable "resource_group" {}
variable "name" {}
variable "location" {}
variable "vnetcidr" {}
variable "websubnetcidr" {}
variable "lbsubnetcidr" {}
# variable "dbsubnetcidr" {}
variable "dbsubnetcidr" {}
variable "appsubnetcidr" {}
variable "env" {}
variable "env" {}

variable "location" {
default = "eastus2"
}
4 changes: 2 additions & 2 deletions ops/terraform/modules/security/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ variable "name" {}
variable "env" {}
variable "resource_group" {}
variable "web_subnet_id" {}
# variable "db_subnet_id" {}
variable "lb_subnet_id" {}
variable "db_subnet_id" {}
variable "lb_subnet_id" {}
14 changes: 13 additions & 1 deletion ops/terraform/providers.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,26 @@ terraform {
azurerm = {
source = "hashicorp/azurerm"
version = "~>3.0"

}
random = {
source = "hashicorp/random"
version = "~>3.0"
}
# azuread = {
# source = "hashicorp/azuread"
# }
}
}

provider "azurerm" {
features {}
}
}

# # Provider for Azure Active Directory resources (e.g., service principals)
# provider "azuread" {

# client_id = var.client_id
# tenant_id = var.tenant_id

# }
11 changes: 7 additions & 4 deletions ops/terraform/variables.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
variable "name" {}

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

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

variable "name" {}

variable "sku_name" {}
Loading