|
| 1 | +# |
| 2 | +## Related to the provisioning of services |
| 3 | +# |
| 4 | + |
| 5 | +locals { |
| 6 | + # The SNS topic ARN to use for the cost anomaly detection |
| 7 | + sns_topic_arn = var.create_sns_topic ? module.sns[0].topic_arn : data.aws_sns_topic.current[0].arn |
| 8 | +} |
| 9 | + |
| 10 | +# |
| 11 | +## We need to lookup the SNS topic ARN, if it exists |
| 12 | +# |
| 13 | +data "aws_sns_topic" "current" { |
| 14 | + count = var.create_sns_topic ? 0 : 1 |
| 15 | + name = var.sns_topic_name |
| 16 | +} |
| 17 | + |
| 18 | +# |
| 19 | +## Provision the SNS topic for the cost anomaly detection, if required |
| 20 | +# |
| 21 | +module "sns" { |
| 22 | + source = "terraform-aws-modules/sns/aws" |
| 23 | + version = "v6.0.1" |
| 24 | + count = var.create_sns_topic ? 1 : 0 |
| 25 | + |
| 26 | + name = var.sns_topic_name |
| 27 | + tags = var.tags |
| 28 | + topic_policy_statements = { |
| 29 | + "AllowBudgetsToNotifySNSTopic" = { |
| 30 | + actions = ["sns:Publish"] |
| 31 | + effect = "Allow" |
| 32 | + principals = [{ |
| 33 | + type = "Service" |
| 34 | + identifiers = ["budgets.amazonaws.com"] |
| 35 | + }] |
| 36 | + } |
| 37 | + "AllowLambda" = { |
| 38 | + actions = [ |
| 39 | + "sns:Subscribe", |
| 40 | + ] |
| 41 | + effect = "Allow" |
| 42 | + principals = [{ |
| 43 | + type = "Service" |
| 44 | + identifiers = ["lambda.amazonaws.com"] |
| 45 | + }] |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +# |
| 51 | +## Provision the cost anomaly detection for services |
| 52 | +# |
| 53 | +resource "aws_ce_anomaly_monitor" "this" { |
| 54 | + for_each = { for x in var.monitors : x.name => x } |
| 55 | + |
| 56 | + name = each.value.name |
| 57 | + monitor_type = each.value.monitor_type |
| 58 | + monitor_dimension = each.value.monitor_dimension |
| 59 | + #monitor_specification = each.value.monitor_specification != "" ? jsonencode(each.value.monitor_specification) : "" |
| 60 | + tags = var.tags |
| 61 | +} |
| 62 | + |
| 63 | +# |
| 64 | +## Provision the subscriptions to the anomaly detection monitors |
| 65 | +# |
| 66 | +resource "aws_ce_anomaly_subscription" "this" { |
| 67 | + for_each = { for x in var.monitors : x.name => x } |
| 68 | + |
| 69 | + name = each.value.name |
| 70 | + frequency = each.value.notify.frequency |
| 71 | + monitor_arn_list = [aws_ce_anomaly_monitor.this[each.key].arn] |
| 72 | + tags = var.tags |
| 73 | + |
| 74 | + dynamic "threshold_expression" { |
| 75 | + for_each = [each.value.notify.threshold_expression != null ? [1] : []] |
| 76 | + content { |
| 77 | + dynamic "dimension" { |
| 78 | + for_each = [for x in each.value.notify.threshold_expression : x if lookup(x, "dimension", null) != null] |
| 79 | + content { |
| 80 | + key = dimension.value.key |
| 81 | + match_options = dimension.value.match_options |
| 82 | + values = dimension.value.values |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + dynamic "cost_category" { |
| 87 | + for_each = [for x in each.value.notify.threshold_expression : x if lookup(x, "cost_category", null) != null] |
| 88 | + content { |
| 89 | + key = cost_category.value.key |
| 90 | + match_options = cost_category.value.match_options |
| 91 | + values = cost_category.value.values |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + dynamic "tags" { |
| 96 | + for_each = [for x in each.value.notify.threshold_expression : x if lookup(x, "tags", null) != null] |
| 97 | + content { |
| 98 | + key = tags.value.key |
| 99 | + match_options = tags.value.match_options |
| 100 | + values = tags.value.values |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + dynamic "and" { |
| 105 | + for_each = [for x in each.value.notify.threshold_expression : x if lookup(x, "and", null) != null] |
| 106 | + content { |
| 107 | + dynamic "dimension" { |
| 108 | + for_each = and.value.and.dimension != null ? [and.value.and.dimension] : [] |
| 109 | + content { |
| 110 | + key = dimension.value.key |
| 111 | + match_options = dimension.value.match_options |
| 112 | + values = dimension.value.values |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + dynamic "or" { |
| 119 | + for_each = [for x in each.value.notify.threshold_expression : x if lookup(x, "or", null) != null] |
| 120 | + content { |
| 121 | + dynamic "dimension" { |
| 122 | + for_each = or.value.or.dimension != null ? [or.value.or.dimension] : [] |
| 123 | + content { |
| 124 | + key = dimension.value.key |
| 125 | + match_options = dimension.value.match_options |
| 126 | + values = dimension.value.values |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + dynamic "not" { |
| 133 | + for_each = [for x in each.value.notify.threshold_expression : x if lookup(x, "not", null) != null] |
| 134 | + content { |
| 135 | + dynamic "dimension" { |
| 136 | + for_each = not.value.not.dimension != null ? [not.value.not.dimension] : [] |
| 137 | + content { |
| 138 | + key = dimension.value.key |
| 139 | + match_options = dimension.value.match_options |
| 140 | + values = dimension.value.values |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + subscriber { |
| 149 | + address = local.sns_topic_arn |
| 150 | + type = "SNS" |
| 151 | + } |
| 152 | + |
| 153 | + depends_on = [module.sns] |
| 154 | +} |
| 155 | + |
| 156 | +# |
| 157 | +## Provision any additional notification subscriptions (email) |
| 158 | +# |
| 159 | +resource "aws_sns_topic_subscription" "main" { |
| 160 | + for_each = { for x in var.notification.email.addresses : x => x } |
| 161 | + |
| 162 | + endpoint = each.value |
| 163 | + protocol = "email" |
| 164 | + topic_arn = local.sns_topic_arn |
| 165 | +} |
| 166 | + |
| 167 | + |
| 168 | +# |
| 169 | +## Provision a slack notification if required |
| 170 | +# |
| 171 | +# tfsec:ignore:aws-lambda-enable-tracing |
| 172 | +# tfsec:ignore:aws-lambda-restrict-source-arn |
| 173 | +module "slack_notfications" { |
| 174 | + count = var.notification.slack != null ? 1 : 0 |
| 175 | + source = "terraform-aws-modules/notify-slack/aws" |
| 176 | + version = "6.1.1" |
| 177 | + |
| 178 | + create_sns_topic = false |
| 179 | + lambda_function_name = "cost-anomaly-detection" |
| 180 | + slack_channel = var.notification.slack.channel |
| 181 | + slack_username = ":aws: (Cost Anomaly Detection)" |
| 182 | + slack_webhook_url = var.notification.slack.webhook_url |
| 183 | + sns_topic_name = var.sns_topic_name |
| 184 | + tags = var.tags |
| 185 | +} |
| 186 | + |
0 commit comments