-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
199 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<!-- BEGIN_TF_DOCS --> | ||
## Requirements | ||
|
||
| Name | Version | | ||
|------|---------| | ||
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >=1.7 | | ||
| <a name="requirement_google"></a> [google](#requirement\_google) | >= 5.22.0 | | ||
| <a name="requirement_google-beta"></a> [google-beta](#requirement\_google-beta) | >= 5.22.0 | | ||
|
||
## Providers | ||
|
||
| Name | Version | | ||
|------|---------| | ||
| <a name="provider_google"></a> [google](#provider\_google) | >= 5.22.0 | | ||
|
||
## Modules | ||
|
||
No modules. | ||
|
||
## Resources | ||
|
||
| Name | Type | | ||
|------|------| | ||
| [google_monitoring_alert_policy.main](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/monitoring_alert_policy) | resource | | ||
| [google_monitoring_notification_channel.main](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/monitoring_notification_channel) | resource | | ||
| [google_project_service.main](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/project_service) | resource | | ||
| [google_project.main](https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/project) | data source | | ||
|
||
## Inputs | ||
|
||
| Name | Description | Type | Default | Required | | ||
|------|-------------|------|---------|:--------:| | ||
| <a name="input_channels"></a> [channels](#input\_channels) | Channel variable that contains `error` and `warn` as keys | `map(string)` | n/a | yes | | ||
| <a name="input_location"></a> [location](#input\_location) | The location of the resource. | `string` | n/a | yes | | ||
| <a name="input_project_id"></a> [project\_id](#input\_project\_id) | The ID of Google Cloud Platform. | `string` | n/a | yes | | ||
| <a name="input_secrets"></a> [secrets](#input\_secrets) | The token required for notifications to Slack.<br> The variable is required for monitoring. | `string` | n/a | yes | | ||
| <a name="input_target"></a> [target](#input\_target) | The target information for monitoring.<br> `base_value` is used to calculate a numeric value based on the target resource.<br> Specifically, it is used to obtain a percentage, as in `floor(base_value * threshold.value)`. | <pre>object({<br> title = string<br> metric = string<br> resource_type = string<br> label = string<br> name = string<br> filter = string<br> reducer = string<br> aligner = string<br> base_value = optional(number, 1)<br> threshold = map(<br> object({<br> value = number<br> window = string<br> })<br> )<br> })</pre> | n/a | yes | | ||
|
||
## Outputs | ||
|
||
| Name | Description | | ||
|------|-------------| | ||
| <a name="output_enabled_apis"></a> [enabled\_apis](#output\_enabled\_apis) | Already enabled APIs list. | | ||
| <a name="output_policies"></a> [policies](#output\_policies) | Alert policies name object. | | ||
<!-- END_TF_DOCS --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
resource "google_monitoring_alert_policy" "main" { | ||
for_each = local.levels | ||
|
||
display_name = "[${upper(each.value)}] ${var.target.name} ${var.target.title}" | ||
|
||
conditions { | ||
display_name = "[${upper(each.value)}] ${var.target.name} - ${var.target.title}" | ||
|
||
condition_threshold { | ||
filter = <<EOF | ||
metric.type = "${var.target.metric}" | ||
AND resource.type = "${var.target.resource_type}" | ||
AND ${var.target.label} = ${var.target.name} | ||
AND ${var.target.filter} | ||
EOF | ||
|
||
aggregations { | ||
alignment_period = var.target.threshold[each.value].window | ||
cross_series_reducer = var.target.reducer | ||
per_series_aligner = var.target.aligner | ||
} | ||
|
||
trigger { | ||
count = 1 | ||
} | ||
|
||
threshold_value = ( | ||
var.target.base_value == 1 ? | ||
var.target.threshold[each.value].value : | ||
floor(var.target.base_value * var.target.threshold[each.value].value) | ||
) | ||
|
||
comparison = "COMPARISON_GT" | ||
duration = "0s" | ||
} | ||
} | ||
|
||
alert_strategy { | ||
auto_close = "604800s" | ||
} | ||
|
||
combiner = "OR" | ||
enabled = "true" | ||
notification_channels = [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
resource "google_monitoring_notification_channel" "main" { | ||
for_each = local.levels | ||
|
||
display_name = upper(each.key) | ||
type = "slack" | ||
|
||
labels = { | ||
channel_name = each.value | ||
} | ||
|
||
sensitive_labels { | ||
auth_token = yamldecode(var.secrets)["auth_token"] | ||
} | ||
|
||
lifecycle { | ||
ignore_changes = [ | ||
labels["team"] | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
locals { | ||
apis = toset([ | ||
"monitoring.googleapis.com", | ||
]) | ||
levels = toset(keys(var.target.threshold)) | ||
} | ||
|
||
data "google_project" "main" {} | ||
|
||
resource "google_project_service" "main" { | ||
for_each = local.apis | ||
|
||
project = data.google_project.main.project_id | ||
service = each.value | ||
disable_on_destroy = false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
output "enabled_apis" { | ||
description = "Already enabled APIs list." | ||
value = [for api in google_project_service.main : api.service] | ||
} | ||
|
||
output "policies" { | ||
description = "Alert policies name object." | ||
value = { | ||
error = google_monitoring_alert_policy.main["error"].name | ||
warn = google_monitoring_alert_policy.main["warn"].name | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
terraform { | ||
required_version = ">=1.7" | ||
|
||
required_providers { | ||
google = { | ||
source = "hashicorp/google" | ||
version = ">= 5.22.0" | ||
} | ||
google-beta = { | ||
source = "hashicorp/google" | ||
version = ">= 5.22.0" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
variable "project_id" { | ||
description = "The ID of Google Cloud Platform." | ||
type = string | ||
} | ||
|
||
variable "location" { | ||
description = "The location of the resource." | ||
type = string | ||
} | ||
|
||
variable "target" { | ||
description = <<EOF | ||
The target information for monitoring. | ||
`base_value` is used to calculate a numeric value based on the target resource. | ||
Specifically, it is used to obtain a percentage, as in `floor(base_value * threshold.value)`. | ||
EOF | ||
type = object({ | ||
title = string | ||
metric = string | ||
resource_type = string | ||
label = string | ||
name = string | ||
filter = string | ||
reducer = string | ||
aligner = string | ||
base_value = optional(number, 1) | ||
threshold = map( | ||
object({ | ||
value = number | ||
window = string | ||
}) | ||
) | ||
}) | ||
} | ||
|
||
variable "channels" { | ||
description = "Channel variable that contains `error` and `warn` as keys" | ||
type = map(string) | ||
} | ||
|
||
variable "secrets" { | ||
description = <<EOF | ||
The token required for notifications to Slack. | ||
The variable is required for monitoring. | ||
EOF | ||
type = string | ||
} |