# SNS Topic for Alerts resource "aws_sns_topic" "alerts" { name = "${var.project_name}-${var.environment}-alerts" tags = { Name = "${var.project_name}-${var.environment}-alerts" Environment = var.environment Project = var.project_name Purpose = "System monitoring alerts" } } # SNS Topic Policy to allow CloudWatch to publish resource "aws_sns_topic_policy" "alerts_policy" { arn = aws_sns_topic.alerts.arn policy = jsonencode({ Version = "2012-10-17" Statement = [ { Sid = "AllowCloudWatchAlarmsToPublish" Effect = "Allow" Principal = { Service = "cloudwatch.amazonaws.com" } Action = [ "SNS:Publish" ] Resource = aws_sns_topic.alerts.arn Condition = { StringEquals = { "aws:SourceAccount" = data.aws_caller_identity.current.account_id } } } ] }) } # Email Subscription (requires manual confirmation) resource "aws_sns_topic_subscription" "email_alerts" { count = var.alert_email != "" ? 1 : 0 topic_arn = aws_sns_topic.alerts.arn protocol = "email" endpoint = var.alert_email depends_on = [aws_sns_topic.alerts] } # Data source to get current AWS account ID data "aws_caller_identity" "current" {}