Skip to content

Commit 38e321f

Browse files
committed
DTOSS-11103: Create alerts for the postgres db
A PR to setup technical tools and alerting to allow us to monitor our live infrastructure. This PR is to alert on the Postgres database on Azure (not the container version)
1 parent 4ac7ea7 commit 38e321f

File tree

9 files changed

+87
-60
lines changed

9 files changed

+87
-60
lines changed

infrastructure/environments/preprod/variables.tfvars

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
alert_window_size = "PT15M"
12
api_oauth_token_url = "https://int.api.service.nhs.uk/oauth2/token"
23
dns_zone_name = "manage-breast-screening.screening.nhs.uk"
34
enable_auth = false
@@ -11,3 +12,5 @@ vnet_address_space = "10.10.0.0/16"
1112
nhs_notify_api_message_batch_url = "https://int.api.service.nhs.uk/comms/v1/message-batches"
1213
seed_demo_data = false
1314
allowed_paths = ["/notifications/message-status/create"]
15+
enable_monitoring = true
16+
monitoring_email_address = "[email protected]"

infrastructure/modules/container-apps/postgres.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ module "postgres" {
4545
private_service_connection_is_manual = false
4646
}
4747

48+
# alerts
49+
action_group_id = var.action_group_id
50+
enable_monitoring = var.enable_monitoring
51+
52+
4853
databases = {
4954
db1 = {
5055
collation = "en_US.utf8"

infrastructure/modules/container-apps/variables.tf

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,27 @@ variable "use_apex_domain" {
145145
type = bool
146146
}
147147

148+
variable "enable_monitoring" {
149+
description = "Whether monitoring and alerting is enabled for the PostgreSQL Flexible Server."
150+
type = bool
151+
}
152+
153+
154+
variable "alert_window_size" {
155+
type = string
156+
nullable = false
157+
validation {
158+
condition = contains(["PT1M", "PT5M", "PT15M", "PT30M", "PT1H", "PT6H", "PT12H"], var.alert_window_size)
159+
error_message = "The alert_window_size must be one of: PT1M, PT5M, PT15M, PT30M, PT1H, PT6H, PT12H"
160+
}
161+
description = "The period of time that is used to monitor alert activity e.g. PT1M, PT5M, PT15M, PT30M, PT1H, PT6H, PT12H. The interval between checks is adjusted accordingly."
162+
}
163+
164+
variable "action_group_id" {
165+
type = string
166+
description = "ID of the action group to notify."
167+
}
168+
148169

149170
locals {
150171
resource_group_name = "rg-${var.app_short_name}-${var.environment}-container-app-uks"
@@ -162,8 +183,8 @@ locals {
162183
common_env = merge(
163184
local.env_vars_from_yaml,
164185
{
165-
SSL_MODE = "require"
166-
DJANGO_ENV = var.env_config
186+
SSL_MODE = "require"
187+
DJANGO_ENV = var.env_config
167188
}
168189
)
169190

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,14 @@
11
module "monitor_action_group" {
2-
for_each = local.monitor_action_group_map
3-
42
source = "../dtos-devops-templates/infrastructure/modules/monitor-action-group"
53

64
name = module.shared_config.names.monitor-action-group
75
resource_group_name = azurerm_resource_group.main.name
86
location = var.region
9-
short_name = each.value.short_name
10-
email_receiver = each.value.email_receiver
11-
event_hub_receiver = each.value.event_hub_receiver
12-
sms_receiver = each.value.sms_receiver
13-
voice_receiver = each.value.voice_receiver
14-
webhook_receiver = each.value.webhook_receiver
15-
}
16-
17-
locals {
18-
monitor_action_group_map = {
19-
for action_group_key, action_group_details in var.monitor_action_group :
20-
action_group_key => merge(
21-
{
22-
action_group_key = action_group_key
23-
},
24-
action_group_details
25-
)
7+
short_name = "ag-${var.environment}"
8+
email_receiver = {
9+
email = {
10+
name = "email"
11+
email_address = var.monitoring_email_address
12+
}
2613
}
2714
}

infrastructure/modules/infra/output.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@ output "postgres_subnet_id" {
2525
output "main_subnet_id" {
2626
value = module.main_subnet.id
2727
}
28+
29+
output "monitor_action_group_id" {
30+
value = module.monitor_action_group.monitor_action_group.id
31+
}

infrastructure/modules/infra/variables.tf

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -34,38 +34,9 @@ variable "protect_keyvault" {
3434
default = true
3535
}
3636

37-
variable "monitor_action_group" {
38-
description = "Default configuration for the monitor action groups."
39-
type = map(object({
40-
short_name = string
41-
email_receiver = optional(map(object({
42-
name = string
43-
email_address = string
44-
use_common_alert_schema = optional(bool, false)
45-
})))
46-
event_hub_receiver = optional(map(object({
47-
name = string
48-
event_hub_namespace = string
49-
event_hub_name = string
50-
subscription_id = string
51-
use_common_alert_schema = optional(bool, false)
52-
})))
53-
sms_receiver = optional(map(object({
54-
name = string
55-
country_code = string
56-
phone_number = string
57-
})))
58-
voice_receiver = optional(map(object({
59-
name = string
60-
country_code = string
61-
phone_number = string
62-
})))
63-
webhook_receiver = optional(map(object({
64-
name = string
65-
service_uri = string
66-
use_common_alert_schema = optional(bool, false)
67-
})))
68-
}))
37+
variable "monitoring_email_address" {
38+
description = "monitoring email address"
39+
type = string
6940
}
7041

7142
locals {

infrastructure/terraform/data.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,10 @@ data "azurerm_subnet" "main" {
3535
virtual_network_name = module.shared_config.names.virtual-network-lowercase
3636
resource_group_name = local.resource_group_name
3737
}
38+
39+
data "azurerm_monitor_action_group" "main" {
40+
count = var.deploy_infra ? 0 : 1
41+
42+
name = module.shared_config.names.monitor-action-group
43+
resource_group_name = local.resource_group_name
44+
}

infrastructure/terraform/main.tf

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ module "infra" {
88
azurerm.hub = azurerm.hub
99
}
1010

11-
region = local.region
12-
resource_group_name = local.resource_group_name
13-
app_short_name = var.app_short_name
14-
environment = var.env_config
15-
hub = var.hub
16-
protect_keyvault = var.protect_keyvault
17-
vnet_address_space = var.vnet_address_space
11+
monitoring_email_address = var.monitoring_email_address
12+
region = local.region
13+
resource_group_name = local.resource_group_name
14+
app_short_name = var.app_short_name
15+
environment = var.env_config
16+
hub = var.hub
17+
protect_keyvault = var.protect_keyvault
18+
vnet_address_space = var.vnet_address_space
1819
}
1920

2021
module "shared_config" {
@@ -36,6 +37,9 @@ module "container-apps" {
3637
}
3738

3839
region = local.region
40+
action_group_id = var.deploy_infra ? module.infra[0].monitor_action_group_id : data.azurerm_monitor_action_group.main[0].id
41+
alert_window_size = var.alert_window_size
42+
enable_monitoring = var.enable_monitoring
3943
app_key_vault_id = var.deploy_infra ? module.infra[0].app_key_vault_id : data.azurerm_key_vault.app_key_vault[0].id
4044
app_short_name = var.app_short_name
4145
allowed_paths = var.allowed_paths

infrastructure/terraform/variables.tf

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,13 @@ variable "postgres_sku_name" {
8989
default = "B_Standard_B1ms"
9090
type = string
9191
}
92+
9293
variable "postgres_storage_mb" {
9394
description = "Value of the PostgreSQL Flexible Server storage in MB"
9495
default = 32768
9596
type = number
9697
}
98+
9799
variable "postgres_storage_tier" {
98100
description = "Value of the PostgreSQL Flexible Server storage tier"
99101
default = "P4"
@@ -128,6 +130,29 @@ variable "seed_demo_data" {
128130
default = false
129131
}
130132

133+
variable "alert_window_size" {
134+
type = string
135+
nullable = false
136+
default = "PT5M"
137+
validation {
138+
condition = contains(["PT1M", "PT5M", "PT15M", "PT30M", "PT1H", "PT6H", "PT12H"], var.alert_window_size)
139+
error_message = "The alert_window_size must be one of: PT1M, PT5M, PT15M, PT30M, PT1H, PT6H, PT12H"
140+
}
141+
description = "The period of time that is used to monitor alert activity e.g. PT1M, PT5M, PT15M, PT30M, PT1H, PT6H, PT12H. The interval between checks is adjusted accordingly."
142+
}
143+
144+
variable "enable_monitoring" {
145+
description = "Whether monitoring and alerting is enabled for the PostgreSQL Flexible Server."
146+
type = bool
147+
default = false
148+
}
149+
150+
variable "monitoring_email_address" {
151+
description = "monitoring email address"
152+
type = string
153+
default = null
154+
}
155+
131156
locals {
132157
region = "uksouth"
133158

0 commit comments

Comments
 (0)