Skip to content

Commit bdf62d7

Browse files
authored
Merge pull request #338 from NHSDigital/feat/DTOSS-10700-Use-a-docker-image-for-the-postgres-database-for-the-review-apps-testing
[DTOSS-10700] use a docker image for the postgres database for the review apps testing
2 parents c95f2da + 35a004a commit bdf62d7

File tree

7 files changed

+114
-35
lines changed

7 files changed

+114
-35
lines changed

infrastructure/environments/dev/variables.tfvars

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ postgres_geo_redundant_backup_enabled = false
77
protect_keyvault = false
88
vnet_address_space = "10.128.0.0/16"
99
personas_enabled = true
10+
seed_demo_data = true

infrastructure/environments/review/variables.tfvars

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ postgres_geo_redundant_backup_enabled = false
77
protect_keyvault = false
88
vnet_address_space = "10.142.0.0/16"
99
personas_enabled = true
10+
deploy_database_as_container = true
11+
seed_demo_data = true

infrastructure/modules/container-apps/main.tf

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@ module "shared_config" {
1111
application = var.app_short_name
1212
}
1313

14-
# create the database
15-
# prod : make migrate seed [default]
16-
# dev : make migrate seed [default]
17-
# review: make migrate seed example_data
18-
# put "example_data" once the PR has been merged in.
14+
# populate the database
1915
module "db_setup" {
2016
source = "../dtos-devops-templates/infrastructure/modules/container-app-job"
2117

@@ -26,29 +22,23 @@ module "db_setup" {
2622
# Run everything through /bin/sh
2723
container_command = ["/bin/sh", "-c"]
2824

29-
# Build the full command string, conditionally including example_data
30-
# && python manage.py example_data"
3125
container_args = [
32-
var.env_config == "prod"
33-
? "python manage.py migrate"
34-
: "python manage.py migrate && python manage.py seed_demo_data --noinput"
26+
var.seed_demo_data
27+
? "python manage.py migrate && python manage.py seed_demo_data --noinput"
28+
: "python manage.py migrate"
3529
]
36-
30+
secret_variables = var.deploy_database_as_container ? { DATABASE_PASSWORD = resource.random_password.admin_password[0].result } : {}
3731
docker_image = var.docker_image
38-
user_assigned_identity_ids = [module.db_connect_identity.id]
32+
user_assigned_identity_ids = var.deploy_database_as_container ? [] : [module.db_connect_identity[0].id]
33+
environment_variables = merge(
34+
local.common_env,
35+
var.deploy_database_as_container ? local.container_db_env : local.azure_db_env
36+
)
3937

40-
environment_variables = {
41-
DATABASE_HOST = module.postgres.host
42-
DATABASE_NAME = module.postgres.database_names[0]
43-
DATABASE_USER = module.db_connect_identity.name
44-
SSL_MODE = "require"
45-
AZURE_CLIENT_ID = module.db_connect_identity.client_id
46-
PERSONAS_ENABLED = var.personas_enabled ? "1" : "0"
47-
DJANGO_ENV = var.env_config
48-
}
4938
}
5039

5140
module "webapp" {
41+
5242
providers = {
5343
azurerm = azurerm
5444
azurerm.hub = azurerm.hub
@@ -63,15 +53,15 @@ module "webapp" {
6353
enable_auth = var.enable_auth
6454
app_key_vault_id = var.app_key_vault_id
6555
docker_image = var.docker_image
66-
user_assigned_identity_ids = [module.db_connect_identity.id]
67-
environment_variables = {
68-
ALLOWED_HOSTS = "${var.app_short_name}-web-${var.environment}.${var.default_domain}"
69-
DATABASE_HOST = module.postgres.host
70-
DATABASE_NAME = module.postgres.database_names[0]
71-
DATABASE_USER = module.db_connect_identity.name
72-
SSL_MODE = "require"
73-
AZURE_CLIENT_ID = module.db_connect_identity.client_id
74-
}
75-
is_web_app = true
76-
http_port = 8000
56+
user_assigned_identity_ids = var.deploy_database_as_container ? [] : [module.db_connect_identity[0].id]
57+
environment_variables = merge(
58+
local.common_env,
59+
{
60+
ALLOWED_HOSTS = "${var.app_short_name}-web-${var.environment}.${var.default_domain}"
61+
},
62+
var.deploy_database_as_container ? local.container_db_env : local.azure_db_env
63+
)
64+
secret_variables = var.deploy_database_as_container ? { DATABASE_PASSWORD = resource.random_password.admin_password[0].result } : {}
65+
is_web_app = true
66+
port = 8000
7767
}

infrastructure/modules/container-apps/postgres.tf

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ data "azurerm_private_dns_zone" "postgres" {
55
resource_group_name = "rg-hub-${var.hub}-uks-private-dns-zones"
66
}
77

8+
# Don't deploy if deploy_database_as_container is true
89
module "postgres" {
10+
count = var.deploy_database_as_container ? 0 : 1
11+
912
source = "../dtos-devops-templates/infrastructure/modules/postgresql-flexible"
1013

1114
# postgresql Server
@@ -18,8 +21,8 @@ module "postgres" {
1821
postgresql_admin_object_id = data.azuread_group.postgres_sql_admin_group.object_id
1922
postgresql_admin_principal_name = var.postgres_sql_admin_group
2023
postgresql_admin_principal_type = "Group"
21-
administrator_login = "admin"
22-
admin_identities = [module.db_connect_identity]
24+
administrator_login = local.database_user
25+
admin_identities = [module.db_connect_identity[0]]
2326

2427
# Diagnostic Settings
2528
log_analytics_workspace_id = var.log_analytics_workspace_audit_id
@@ -47,16 +50,52 @@ module "postgres" {
4750
collation = "en_US.utf8"
4851
charset = "UTF8"
4952
max_size_gb = 10
50-
name = "manage_breast_screening"
53+
name = local.database_name
5154
}
5255
}
5356

5457
tags = {}
5558
}
5659

5760
module "db_connect_identity" {
61+
count = var.deploy_database_as_container ? 0 : 1
62+
5863
source = "../dtos-devops-templates/infrastructure/modules/managed-identity"
5964
resource_group_name = azurerm_resource_group.main.name
6065
location = var.region
6166
uai_name = "mi-${var.app_short_name}-${var.environment}-db-connect"
6267
}
68+
69+
resource "random_password" "admin_password" {
70+
count = var.deploy_database_as_container ? 1 : 0
71+
72+
length = 30
73+
special = true
74+
override_special = "!@#$%^&*()-_=+"
75+
}
76+
77+
module "database_container" {
78+
count = var.deploy_database_as_container ? 1 : 0
79+
80+
providers = {
81+
azurerm = azurerm
82+
azurerm.hub = azurerm.hub
83+
}
84+
app_key_vault_id = var.app_key_vault_id
85+
source = "../dtos-devops-templates/infrastructure/modules/container-app"
86+
name = "${var.app_short_name}-db-${var.environment}"
87+
container_app_environment_id = var.container_app_environment_id
88+
docker_image = "postgres:16"
89+
enable_auth = false
90+
secret_variables = var.deploy_database_as_container ? { POSTGRES_PASSWORD = resource.random_password.admin_password[0].result } : {}
91+
environment_variables = {
92+
POSTGRES_USER = local.database_user
93+
POSTGRES_DB = local.database_name
94+
}
95+
resource_group_name = azurerm_resource_group.main.name
96+
fetch_secrets_from_app_key_vault = var.fetch_secrets_from_app_key_vault
97+
infra_key_vault_name = "kv-${var.app_short_name}-${var.env_config}-inf"
98+
infra_key_vault_rg = "rg-${var.app_short_name}-${var.env_config}-infra"
99+
is_tcp_app = true
100+
port = 5432
101+
}

infrastructure/modules/container-apps/variables.tf

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ variable "log_analytics_workspace_audit_id" {
6767
type = string
6868
}
6969

70+
variable "deploy_database_as_container" {
71+
description = "Whether to deploy the database as a container or as an Azure postgres flexible server."
72+
type = bool
73+
}
74+
7075
variable "postgres_backup_retention_days" {
7176
description = "The number of days to retain backups for the PostgreSQL Flexible Server."
7277
type = number
@@ -113,6 +118,12 @@ variable "personas_enabled" {
113118
default = false
114119
}
115120

121+
variable "seed_demo_data" {
122+
description = "Whether or not to seed the demo data in the database."
123+
type = bool
124+
default = false
125+
}
126+
116127
variable "use_apex_domain" {
117128
description = "Use apex domain for the Front Door endpoint. Set to true for production."
118129
type = bool
@@ -122,4 +133,26 @@ locals {
122133
resource_group_name = "rg-${var.app_short_name}-${var.environment}-container-app-uks"
123134

124135
hostname = var.use_apex_domain ? var.dns_zone_name : "${var.environment}.${var.dns_zone_name}"
136+
137+
database_user = "admin"
138+
database_name = "manage_breast_screening"
139+
140+
common_env = {
141+
SSL_MODE = "require"
142+
PERSONAS_ENABLED = var.personas_enabled ? "1" : "0"
143+
DJANGO_ENV = var.env_config
144+
}
145+
146+
container_db_env = {
147+
DATABASE_HOST = var.deploy_database_as_container ? module.database_container[0].container_app_fqdn : null
148+
DATABASE_NAME = local.database_name
149+
DATABASE_USER = local.database_user
150+
}
151+
152+
azure_db_env = {
153+
AZURE_CLIENT_ID = var.deploy_database_as_container ? null : module.db_connect_identity[0].client_id
154+
DATABASE_HOST = var.deploy_database_as_container ? null : module.postgres[0].host
155+
DATABASE_NAME = var.deploy_database_as_container ? null : module.postgres[0].database_names[0]
156+
DATABASE_USER = var.deploy_database_as_container ? null : module.db_connect_identity[0].name
157+
}
125158
}

infrastructure/terraform/main.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ module "container-apps" {
4242
default_domain = var.deploy_infra ? module.infra[0].default_domain : data.azurerm_container_app_environment.this[0].default_domain
4343
dns_zone_name = var.dns_zone_name
4444
docker_image = var.docker_image
45+
deploy_database_as_container = var.deploy_database_as_container
4546
enable_auth = var.enable_auth
4647
environment = var.environment
4748
env_config = var.env_config
@@ -56,5 +57,6 @@ module "container-apps" {
5657
postgres_storage_mb = var.postgres_storage_mb
5758
postgres_storage_tier = var.postgres_storage_tier
5859
postgres_subnet_id = var.deploy_infra ? module.infra[0].postgres_subnet_id : data.azurerm_subnet.postgres[0].id
60+
seed_demo_data = var.seed_demo_data
5961
use_apex_domain = var.use_apex_domain
6062
}

infrastructure/terraform/variables.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ variable "postgres_geo_redundant_backup_enabled" {
7272
default = true
7373
}
7474

75+
variable "deploy_database_as_container" {
76+
description = "Whether to deploy the database as a container or as an Azure postgres flexible server."
77+
type = bool
78+
default = false
79+
}
80+
7581
variable "postgres_sku_name" {
7682
description = "Value of the PostgreSQL Flexible Server SKU name"
7783
default = "B_Standard_B1ms"
@@ -116,6 +122,12 @@ variable "personas_enabled" {
116122
default = false
117123
}
118124

125+
variable "seed_demo_data" {
126+
description = "Whether or not to seed the demo data in the database."
127+
type = bool
128+
default = false
129+
}
130+
119131
locals {
120132
region = "uksouth"
121133

0 commit comments

Comments
 (0)