Skip to content

Commit 1c18d78

Browse files
ps-occrpimrannayer
andauthored
feat: Add support to use existing notification channels for alerts (#593)
Co-authored-by: Imran Nayer <[email protected]>
1 parent 0f18fd7 commit 1c18d78

File tree

6 files changed

+50
-28
lines changed

6 files changed

+50
-28
lines changed

examples/postgresql-backup-provided-service-account/main.tf

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,31 @@ resource "google_storage_bucket" "backup" {
4545
project = var.project_id
4646
}
4747

48+
resource "google_monitoring_notification_channel" "email" {
49+
display_name = "Test email notification channel"
50+
type = "email"
51+
project = var.project_id
52+
labels = {
53+
email_address = "[email protected]"
54+
}
55+
}
56+
4857
module "backup" {
4958
source = "terraform-google-modules/sql-db/google//modules/backup"
5059
version = "~> 20.0"
5160

52-
region = "us-central1"
53-
project_id = var.project_id
54-
sql_instance = module.postgresql.instance_name
55-
export_databases = []
56-
export_uri = google_storage_bucket.backup.url
57-
backup_retention_time = 1
58-
backup_schedule = "5 * * * *"
59-
export_schedule = "10 * * * *"
60-
use_serverless_export = true
61-
service_account = "${data.google_project.test_project.number}[email protected]"
61+
region = "us-central1"
62+
project_id = var.project_id
63+
sql_instance = module.postgresql.instance_name
64+
export_databases = []
65+
export_uri = google_storage_bucket.backup.url
66+
backup_retention_time = 1
67+
backup_schedule = "5 * * * *"
68+
export_schedule = "10 * * * *"
69+
use_serverless_export = true
70+
service_account = "${data.google_project.test_project.number}[email protected]"
71+
create_notification_channel = false
72+
notification_channels = [google_monitoring_notification_channel.email.id]
6273
}
6374

6475
data "google_project" "test_project" {

modules/backup/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ fetch workflows.googleapis.com/Workflow
5959
| backup\_schedule | The cron schedule to execute the internal backup | `string` | `"45 2 * * *"` | no |
6060
| compress\_export | Whether or not to compress the export when storing in the bucket; Only valid for MySQL and PostgreSQL | `bool` | `true` | no |
6161
| connector\_params\_timeout | The end-to-end duration the connector call is allowed to run for before throwing a timeout exception. The default value is 1800 and this should be the maximum for connector methods that are not long-running operations. Otherwise, for long-running operations, the maximum timeout for a connector call is 31536000 seconds (one year). | `number` | `1800` | no |
62-
| create\_email\_notification\_channel | Create email notification channel to send alerts | `bool` | `false` | no |
63-
| email\_notification\_channel\_name | Name of email notification channel | `string` | `"Email Notification"` | no |
62+
| create\_notification\_channel | If set to true it will create email notification channel | `bool` | `false` | no |
6463
| enable\_backup\_monitoring | Whether to monitor backup workflows or not | `bool` | `false` | no |
6564
| enable\_connector\_params | Whether to enable connector-specific parameters for Google Workflow SQL Export. | `bool` | `false` | no |
6665
| enable\_export\_backup | Weather to create exports to GCS Buckets with this module | `bool` | `true` | no |
@@ -72,6 +71,8 @@ fetch workflows.googleapis.com/Workflow
7271
| export\_uri | The bucket and path uri for exporting to GCS | `string` | n/a | yes |
7372
| log\_db\_name\_to\_export | Whether or not to log database name in the export workflow | `bool` | `false` | no |
7473
| monitoring\_email | Email address to send alerts | `string` | `null` | no |
74+
| notification\_channel\_name | Name of the email notification channel to be created. Only needed when create\_notification\_channel is set to true. | `string` | `"Email Notification"` | no |
75+
| notification\_channels | List of existing notification channels to send alerts to | `list(string)` | `[]` | no |
7576
| project\_id | The project ID | `string` | n/a | yes |
7677
| region | The region where to run the workflow | `string` | `"us-central1"` | no |
7778
| scheduler\_timezone | The Timezone in which the Scheduler Jobs are triggered | `string` | `"Etc/GMT"` | no |

modules/backup/main.tf

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ locals {
2121
backup_name = "sql-backup-${var.sql_instance}${var.unique_suffix}"
2222
role_name = var.enable_export_backup ? "roles/cloudsql.editor" : "roles/cloudsql.viewer"
2323
export_name = var.use_sql_instance_replica_in_exporter ? "sql-export-${var.sql_instance_replica}${var.unique_suffix}" : "sql-export-${var.sql_instance}${var.unique_suffix}"
24+
notification_channels = var.create_notification_channel ? concat(var.notification_channels, [google_monitoring_notification_channel.email[0].id]) : var.notification_channels
2425
}
2526

2627

@@ -63,8 +64,9 @@ data "google_sql_database_instance" "backup_instance" {
6364
}
6465

6566
resource "google_monitoring_notification_channel" "email" {
66-
count = var.create_email_notification_channel ? 1 : 0
67-
display_name = var.email_notification_channel_name
67+
count = var.create_notification_channel ? 1 : 0
68+
display_name = var.notification_channel_name
69+
project = var.project_id
6870
type = "email"
6971
labels = {
7072
email_address = var.monitoring_email
@@ -133,7 +135,7 @@ resource "google_monitoring_alert_policy" "sql_backup_workflow_success_alert" {
133135
evaluation_missing_data = "EVALUATION_MISSING_DATA_ACTIVE"
134136
}
135137
}
136-
notification_channels = [google_monitoring_notification_channel.email[0].id]
138+
notification_channels = local.notification_channels
137139
}
138140

139141
################################
@@ -212,5 +214,5 @@ resource "google_monitoring_alert_policy" "sql_export_workflow_success_alert" {
212214
evaluation_missing_data = "EVALUATION_MISSING_DATA_ACTIVE"
213215
}
214216
}
215-
notification_channels = [google_monitoring_notification_channel.email[0].id]
217+
notification_channels = local.notification_channels
216218
}

modules/backup/variables.tf

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,20 @@ variable "export_monitoring_frequency" {
175175
default = "1d"
176176
}
177177

178-
variable "create_email_notification_channel" {
179-
description = "Create email notification channel to send alerts"
178+
variable "create_notification_channel" {
179+
description = "If set to true it will create email notification channel"
180180
type = bool
181181
default = false
182182
}
183183

184-
variable "email_notification_channel_name" {
185-
description = "Name of email notification channel"
184+
variable "notification_channel_name" {
185+
description = "Name of the email notification channel to be created. Only needed when create_notification_channel is set to true."
186186
type = string
187187
default = "Email Notification"
188188
}
189+
190+
variable "notification_channels" {
191+
description = "List of existing notification channels to send alerts to"
192+
type = list(string)
193+
default = []
194+
}

test/setup/iam.tf

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,18 @@
1616

1717
locals {
1818
int_required_roles = [
19+
"roles/cloudkms.admin",
20+
"roles/cloudkms.cryptoKeyEncrypterDecrypter",
21+
"roles/cloudscheduler.admin",
1922
"roles/cloudsql.admin",
2023
"roles/compute.admin",
2124
"roles/compute.networkAdmin",
2225
"roles/iam.serviceAccountAdmin",
26+
"roles/iam.serviceAccountUser",
27+
"roles/monitoring.editor",
2328
"roles/resourcemanager.projectIamAdmin",
2429
"roles/storage.admin",
2530
"roles/workflows.admin",
26-
"roles/cloudscheduler.admin",
27-
"roles/iam.serviceAccountUser",
28-
"roles/cloudkms.admin",
29-
"roles/cloudkms.cryptoKeyEncrypterDecrypter",
3031
]
3132
}
3233

test/setup/main.tf

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,16 @@ module "project" {
2525
billing_account = var.billing_account
2626

2727
activate_apis = [
28+
"cloudkms.googleapis.com",
2829
"cloudresourcemanager.googleapis.com",
30+
"cloudscheduler.googleapis.com",
2931
"compute.googleapis.com",
32+
"iam.googleapis.com",
33+
"monitoring.googleapis.com",
3034
"servicenetworking.googleapis.com",
35+
"serviceusage.googleapis.com",
3136
"sqladmin.googleapis.com",
32-
"iam.googleapis.com",
3337
"workflows.googleapis.com",
34-
"cloudscheduler.googleapis.com",
35-
"cloudkms.googleapis.com",
36-
"serviceusage.googleapis.com",
3738
]
3839
}
3940

0 commit comments

Comments
 (0)