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 10 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: 4 additions & 0 deletions .github/actions/tf-setup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ inputs:
azure-subscription-id:
description: The Azure subscription_id for this environment.
required: true
azure-object-id:
description: The Azure object_id for this environment.
required: true
app-name:
description: The name of the application being deployed in Terraform.
required: true
marycrawford marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -50,6 +53,7 @@ runs:
ARM_CLIENT_ID: ${{ inputs.azure-client-id }}
ARM_TENANT_ID: ${{ inputs.azure-tenant-id }}
ARM_SUBSCRIPTION_ID: ${{ inputs.azure-subscription-id }}
ARM_OBJECT_ID: ${{ inputs.azure-object-id }}
Copy link
Collaborator

@derekadombek derekadombek Nov 21, 2024

Choose a reason for hiding this comment

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

we will still need to add this input as a .tfvar from the "Load Input Variables" step, under the run. This is if we are going that route and not using getenv()

Copy link
Collaborator

Choose a reason for hiding this comment

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

oh wait.. does the the azurerm_postgresql_flexible_server resource not need the object_id?

Copy link
Collaborator

Choose a reason for hiding this comment

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

it looks like the tenant_id and the client_id are needed though right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

These have been removed. May be added with the next PR with masking the database credentials.

shell: bash
run: |
terraform init -backend-config=config/${{ inputs.deploy-env }}.config
Expand Down
7 changes: 6 additions & 1 deletion ops/terraform/locals.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
locals {
environment = terraform.workspace

# Explicitly get object_id from the environment variable (e.g., in GitHub Actions)
azure_object_id = getenv("ARM_OBJECT_ID", var.object_id)
marycrawford marked this conversation as resolved.
Show resolved Hide resolved
vite_api_url = getenv("VITE_API_URL", "") # Default to an empty string if not set

init = {
environment = local.environment
location = "eastus2"
Expand All @@ -20,4 +25,4 @@ locals {
lbsubnetcidr = "10.1.3.0/24"
}
}
}
}
49 changes: 12 additions & 37 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 @@ -23,10 +20,6 @@ module "networking" {
env = local.environment
}

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

module "securitygroup" {
source = "./modules/security"
name = var.name
Expand All @@ -53,10 +46,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 +57,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 +83,16 @@ 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
##########
module "database" {
source = "./modules/database"
resource_group_name = data.azurerm_resource_group.rg.name
subnet = module.network.azurerm_subnet.app-subnet.id
}

##########
## 07-Monitor
##########
module "vault" {
source = "./modules/vault.tf"
resource_group_name = data.azurerm_resource_group.rg.name
azure_tenant_id = var.azure_tenant_id
object_id = local.azure_object_id
vite_api_url = local.vite_api_url
}
3 changes: 3 additions & 0 deletions ops/terraform/modules/database/data.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "azurerm_resource_group" "rg" {
name = var.resource_group_name
}
44 changes: 44 additions & 0 deletions ops/terraform/modules/database/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# PostgreSQL Server
resource "azurerm_postgresql_server" "postgres_server" {
name = "reportvisionpgserver"
location = data.azurerm_resource_group.rg.location
resource_group_name = data.azurerm_resource_group.rg.name
sku_name = var.sku_name
version = var.engine_version
administrator_login = var.db_username
administrator_login_password = random_string.setup_rds_password.result
storage_mb = 5120 # 5 GB storage
backup_retention_days = 7
ssl_enforcement_enabled = true

# Enable Virtual Network service endpoint
virtual_network_subnet_id = var.subnet

}

# PostgreSQL Database
resource "azurerm_postgresql_database" "postgres_db" {
name = "postgresdb"
resource_group_name = data.azurerm_resource_group.rg.name
server_name = azurerm_postgresql_server.postgres_server.name
charset = "UTF8"
collation = "English_United Kingdom.1252"
}

# Firewall rule for the PostgreSQL server, allowing
# db access to Azure services in same resource group
resource "azurerm_postgresql_firewall_rule" "allow_azure" {
name = "AllowAllAzureIps"
server_name = azurerm_postgresql_server.postgres_server.name
resource_group_name = data.azurerm_resource_group.rg.name
start_ip_address = "0.0.0.0"
end_ip_address = "0.0.0.0"
}

resource "random_string" "setup_rds_password" {
length = 13

# Character set that excludes problematic characters like quotes, backslashes, etc.
override_special = "_!@#-$%^&*()[]{}"
}

30 changes: 30 additions & 0 deletions ops/terraform/modules/database/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
variable "resource_group_name" {}
variable "subnet" {}

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 = "eastus"
}

variable "sku_name" {
type = string
description = "value"
default = "B_Gen5_1" # Basic SKU, Gen5, 1 vCore
}

variable "subnet" {
description = "The subnet ID to associate with the PostgreSQL server"
type = string
}
3 changes: 3 additions & 0 deletions ops/terraform/modules/vault.tf/data.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "azurerm_resource_group" "rg" {
name = var.resource_group_name
marycrawford marked this conversation as resolved.
Show resolved Hide resolved
}
39 changes: 39 additions & 0 deletions ops/terraform/modules/vault.tf/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
resource "azurerm_key_vault" "key_vault" {
name = "reportvision_keyvault"
location = "eastus"
resource_group_name = data.azurerm_resource_group.rg.name
sku_name = "standard"
tenant_id = var.azure_tenant_id

access_policy {

object_id = var.object_id

key_permissions = [
"get"
]

secret_permissions = [
"get"
]
}
}

# Saves the random password into Azure Key Vault
resource "azurerm_key_vault_secret" "postgres_password" {
name = "postgres-password"
value = azurerm_postgresql_server.postgres_db.administrator_login_password.result
key_vault_id = azurerm_key_vault.key_vault.id
}

# Define the Service Principal for which we are granting access
resource "azurerm_azuread_application" "frontendapp" {
name = "frontend-application"
# TODO: Ask if the VITE_API_URL is the correct endpoint we are using
homepage = var.vite_api_url
identifier_uris = [var.vite_api_url]
}

resource "azurerm_azuread_service_principal" "this" {
application_id = azurerm_azuread_application.frontendapp.application_id
}
32 changes: 32 additions & 0 deletions ops/terraform/modules/vault.tf/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
variable "resource_group_name" {}
variable "azure_tenant_id" {}
variable "object_id" {}
variable "vite_api_url" {}

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 = "eastus"
}

variable "object_id" {
description = "The Azure Object ID"
type = string
}

variable "sku_name" {
type = string
description = "value"
default = "B_Gen5_1" # Basic SKU, Gen5, 1 vCore
}
6 changes: 5 additions & 1 deletion ops/terraform/providers.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,9 @@ terraform {
}

provider "azurerm" {
features {}
features {
key_vault {
purge_soft_delete_on_destroy = true
recover_soft_deleted_key_vaults = true
}
}
4 changes: 3 additions & 1 deletion ops/terraform/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ variable "resource_group_name" {

variable "name" {}

variable "sku_name" {}
variable "sku_name" {}

marycrawford marked this conversation as resolved.
Show resolved Hide resolved
variable "azure_tenant_id" {}
Loading