# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: MIT-0 # # Permission is hereby granted, free of charge, to any person obtaining a copy of this # software and associated documentation files (the "Software"), to deal in the Software # without restriction, including without limitation the rights to use, copy, modify, # merge, publish, distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. # Data Source (https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) data "aws_region" "current" {} # Data Source (https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) data "aws_caller_identity" "current" {} resource "aws_sns_topic" "alerting_sns_topic" { # checkov:skip=CKV_AWS_26:KMS encryption is not required for a simple notifications name = "${var.stack_prefix}-alerts" } resource "aws_sns_topic" "notification_sns_topic" { # checkov:skip=CKV_AWS_26:KMS encryption is not required for a simple notifications name = "${var.stack_prefix}-notifications" } resource "aws_sns_topic_policy" "alerting_sns_topic_policy" { arn = aws_sns_topic.alerting_sns_topic.arn policy = data.aws_iam_policy_document.alerting_sns_topic_policy_document.json } data "aws_iam_policy_document" "alerting_sns_topic_policy_document" { policy_id = "__default_policy_ID" statement { actions = [ "sns:Subscribe", "sns:SetTopicAttributes", "sns:RemovePermission", "sns:Receive", "sns:Publish", "sns:ListSubscriptionsByTopic", "sns:GetTopicAttributes", "sns:DeleteTopic", "sns:AddPermission", ] condition { test = "StringEquals" variable = "AWS:SourceOwner" values = [ "${data.aws_caller_identity.current.account_id}", ] } effect = "Allow" principals { type = "AWS" identifiers = ["*"] } resources = [ aws_sns_topic.alerting_sns_topic.arn, ] sid = "__default_statement_ID" } statement { actions = [ "sns:Publish" ] effect = "Allow" principals { type = "Service" identifiers = ["events.amazonaws.com"] } resources = [ aws_sns_topic.alerting_sns_topic.arn, ] sid = "AllowEventsToPostToSNSTopic" } } data "aws_iam_policy_document" "notification_sns_topic_policy_document" { policy_id = "__default_policy_ID" statement { actions = [ "sns:Subscribe", "sns:SetTopicAttributes", "sns:RemovePermission", "sns:Receive", "sns:Publish", "sns:ListSubscriptionsByTopic", "sns:GetTopicAttributes", "sns:DeleteTopic", "sns:AddPermission", ] condition { test = "StringEquals" variable = "AWS:SourceOwner" values = [ "${data.aws_caller_identity.current.account_id}", ] } effect = "Allow" principals { type = "AWS" identifiers = ["*"] } resources = [ aws_sns_topic.notification_sns_topic.arn, ] sid = "__default_statement_ID" } statement { actions = [ "sns:Publish" ] effect = "Allow" principals { type = "Service" identifiers = ["events.amazonaws.com"] } resources = [ aws_sns_topic.notification_sns_topic.arn, ] sid = "AllowEventsToPostToSNSTopic" } } resource "aws_sns_topic_policy" "notification_sns_topic_policy" { arn = aws_sns_topic.notification_sns_topic.arn policy = data.aws_iam_policy_document.notification_sns_topic_policy_document.json } ######################################################### ## Alert on CMK being scheduled for deletion ######################################################### resource "aws_cloudwatch_event_rule" "cmk_deletion_rule" { name = "${var.stack_prefix}-detect-kms-cmk-deletion" description = "A CloudWatch Event Rule that triggers on AWS KMS Customer Master Key (CMK) deletion events." is_enabled = true event_pattern = <