From e8ab9356f0721ba8703d418bb7d0df2c52de5d2c Mon Sep 17 00:00:00 2001 From: Bogdan Barna Date: Mon, 12 Jul 2021 18:33:47 +0300 Subject: [PATCH] Allow passing of a new variable `aws_sns_topic_arn` and not create an SNS topic --- README.md | 1 + alarms.tf | 28 ++++++++++++++-------------- main.tf | 15 ++++++++++----- outputs.tf | 2 +- variables.tf | 6 ++++++ 5 files changed, 32 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index e0df545..cdcc341 100644 --- a/README.md +++ b/README.md @@ -182,6 +182,7 @@ module "rds_alarms" { |------|-------------|------|---------|:--------:| | [additional\_tag\_map](#input\_additional\_tag\_map) | Additional tags for appending to tags\_as\_list\_of\_maps. Not added to `tags`. | `map(string)` | `{}` | no | | [attributes](#input\_attributes) | Additional attributes (e.g. `1`) | `list(string)` | `[]` | no | +| [aws\_sns\_topic\_arn](#input\_aws\_sns\_topic\_arn) | ARN of an already existing SNS topic. | `string` | `""` | no | | [burst\_balance\_threshold](#input\_burst\_balance\_threshold) | The minimum percent of General Purpose SSD (gp2) burst-bucket I/O credits available. | `string` | `20` | no | | [context](#input\_context) | Single object for setting entire context at once.
See description of individual variables for details.
Leave string and numeric variables as `null` to use default value.
Individual variable settings (non-null) override settings in context object,
except for attributes, tags, and additional\_tag\_map, which are merged. | `any` |
{
"additional_tag_map": {},
"attributes": [],
"delimiter": null,
"enabled": true,
"environment": null,
"id_length_limit": null,
"label_key_case": null,
"label_order": [],
"label_value_case": null,
"name": null,
"namespace": null,
"regex_replace_chars": null,
"stage": null,
"tags": {}
}
| no | | [cpu\_credit\_balance\_threshold](#input\_cpu\_credit\_balance\_threshold) | The minimum number of CPU credits (t2 instances only) available. | `string` | `20` | no | diff --git a/alarms.tf b/alarms.tf index d0f1947..bf3892c 100644 --- a/alarms.tf +++ b/alarms.tf @@ -20,8 +20,8 @@ resource "aws_cloudwatch_metric_alarm" "burst_balance_too_low" { statistic = "Average" threshold = local.thresholds["BurstBalanceThreshold"] alarm_description = "Average database storage burst balance over last 10 minutes too low, expect a significant performance drop soon" - alarm_actions = aws_sns_topic.default.*.arn - ok_actions = aws_sns_topic.default.*.arn + alarm_actions = local.aws_sns_topic_arn + ok_actions = local.aws_sns_topic_arn dimensions = { DBInstanceIdentifier = var.db_instance_id @@ -38,8 +38,8 @@ resource "aws_cloudwatch_metric_alarm" "cpu_utilization_too_high" { statistic = "Average" threshold = local.thresholds["CPUUtilizationThreshold"] alarm_description = "Average database CPU utilization over last 10 minutes too high" - alarm_actions = aws_sns_topic.default.*.arn - ok_actions = aws_sns_topic.default.*.arn + alarm_actions = local.aws_sns_topic_arn + ok_actions = local.aws_sns_topic_arn dimensions = { DBInstanceIdentifier = var.db_instance_id @@ -56,8 +56,8 @@ resource "aws_cloudwatch_metric_alarm" "cpu_credit_balance_too_low" { statistic = "Average" threshold = local.thresholds["CPUCreditBalanceThreshold"] alarm_description = "Average database CPU credit balance over last 10 minutes too low, expect a significant performance drop soon" - alarm_actions = aws_sns_topic.default.*.arn - ok_actions = aws_sns_topic.default.*.arn + alarm_actions = local.aws_sns_topic_arn + ok_actions = local.aws_sns_topic_arn dimensions = { DBInstanceIdentifier = var.db_instance_id @@ -74,8 +74,8 @@ resource "aws_cloudwatch_metric_alarm" "disk_queue_depth_too_high" { statistic = "Average" threshold = local.thresholds["DiskQueueDepthThreshold"] alarm_description = "Average database disk queue depth over last 10 minutes too high, performance may suffer" - alarm_actions = aws_sns_topic.default.*.arn - ok_actions = aws_sns_topic.default.*.arn + alarm_actions = local.aws_sns_topic_arn + ok_actions = local.aws_sns_topic_arn dimensions = { DBInstanceIdentifier = var.db_instance_id @@ -92,8 +92,8 @@ resource "aws_cloudwatch_metric_alarm" "freeable_memory_too_low" { statistic = "Average" threshold = local.thresholds["FreeableMemoryThreshold"] alarm_description = "Average database freeable memory over last 10 minutes too low, performance may suffer" - alarm_actions = aws_sns_topic.default.*.arn - ok_actions = aws_sns_topic.default.*.arn + alarm_actions = local.aws_sns_topic_arn + ok_actions = local.aws_sns_topic_arn dimensions = { DBInstanceIdentifier = var.db_instance_id @@ -110,8 +110,8 @@ resource "aws_cloudwatch_metric_alarm" "free_storage_space_too_low" { statistic = "Average" threshold = local.thresholds["FreeStorageSpaceThreshold"] alarm_description = "Average database free storage space over last 10 minutes too low" - alarm_actions = aws_sns_topic.default.*.arn - ok_actions = aws_sns_topic.default.*.arn + alarm_actions = local.aws_sns_topic_arn + ok_actions = local.aws_sns_topic_arn dimensions = { DBInstanceIdentifier = var.db_instance_id @@ -128,8 +128,8 @@ resource "aws_cloudwatch_metric_alarm" "swap_usage_too_high" { statistic = "Average" threshold = local.thresholds["SwapUsageThreshold"] alarm_description = "Average database swap usage over last 10 minutes too high, performance may suffer" - alarm_actions = aws_sns_topic.default.*.arn - ok_actions = aws_sns_topic.default.*.arn + alarm_actions = local.aws_sns_topic_arn + ok_actions = local.aws_sns_topic_arn dimensions = { DBInstanceIdentifier = var.db_instance_id diff --git a/main.tf b/main.tf index 52ee17c..5c07b40 100644 --- a/main.tf +++ b/main.tf @@ -11,8 +11,13 @@ module "topic_label" { context = module.this.context } +locals { + create_sns_topic = var.aws_sns_topic_arn == "" + aws_sns_topic_arn = local.create_sns_topic ? aws_sns_topic.default.*.arn : [var.aws_sns_topic_arn] +} + resource "aws_sns_topic" "default" { - count = module.this.enabled ? 1 : 0 + count = module.this.enabled && local.create_sns_topic ? 1 : 0 name = module.topic_label.id } @@ -28,7 +33,7 @@ module "subscription_label" { resource "aws_db_event_subscription" "default" { count = module.this.enabled ? 1 : 0 name = module.subscription_label.id - sns_topic = join("", aws_sns_topic.default.*.arn) + sns_topic = join("", local.aws_sns_topic_arn) source_type = "db-instance" source_ids = [var.db_instance_id] @@ -43,18 +48,18 @@ resource "aws_db_event_subscription" "default" { ] depends_on = [ - aws_sns_topic_policy.default + local.aws_sns_topic_arn ] } resource "aws_sns_topic_policy" "default" { - count = module.this.enabled ? 1 : 0 + count = module.this.enabled && local.create_sns_topic ? 1 : 0 arn = join("", aws_sns_topic.default.*.arn) policy = join("", data.aws_iam_policy_document.sns_topic_policy.*.json) } data "aws_iam_policy_document" "sns_topic_policy" { - count = module.this.enabled ? 1 : 0 + count = module.this.enabled && local.create_sns_topic ? 1 : 0 statement { sid = "AllowManageSNS" diff --git a/outputs.tf b/outputs.tf index 811495e..21f29a0 100644 --- a/outputs.tf +++ b/outputs.tf @@ -1,4 +1,4 @@ output "sns_topic_arn" { description = "The ARN of the SNS topic" - value = join("", aws_sns_topic.default.*.arn) + value = join("", local.aws_sns_topic_arn) } diff --git a/variables.tf b/variables.tf index 1d31986..0bc85ae 100644 --- a/variables.tf +++ b/variables.tf @@ -50,3 +50,9 @@ variable "swap_usage_threshold" { # 256 Megabyte in Byte } + +variable "aws_sns_topic_arn" { + description = "ARN of an already existing SNS topic." + type = string + default = "" +}