Skip to content

Commit

Permalink
Adding email subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
adenot committed May 3, 2021
1 parent e7b7212 commit 7697599
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 5 deletions.
3 changes: 2 additions & 1 deletion _data.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@
#}
#

data "aws_region" "current" {}
data "aws_region" "current" {}
data "aws_caller_identity" "current" {}
13 changes: 12 additions & 1 deletion _variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@

variable "slack_endpoint" {
default = ""
description = "endpoint to Slack notifications chanel"
description = "endpoint to Slack notifications channel (optional)"
}

variable "email" {
default = ""
description = "Email address to subscribe notification to (optional)"
}

variable "sns_topic_name" {
Expand All @@ -22,3 +27,9 @@ variable "account_ids" {
default = []
description = "List of accounts to allow publishing to SNS (optional - only when SNS topic is created)"
}

variable "sns_kms_encryption" {
type = bool
default = false
description = "Enabled KMS CMK encryption at rest for SNS Topic"
}
6 changes: 6 additions & 0 deletions email.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
resource "aws_sns_topic_subscription" "alarm_email" {
count = var.email != "" ? 1 : 0
topic_arn = aws_sns_topic.default[0].arn
protocol = "email"
endpoint = var.email
}
30 changes: 30 additions & 0 deletions kms.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
data "aws_iam_policy_document" "kms_policy_sns" {
count = var.sns_kms_encryption ? 1 : 0
statement {
sid = "Enable IAM User Permissions"
effect = "Allow"
principals {
type = "AWS"
identifiers = ["arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"]
}
actions = ["kms:*"]
resources = ["*"]
}
statement {
actions = [ "kms:Decrypt","kms:GenerateDataKey*"]
principals {
type = "Service"
identifiers = ["cloudwatch.amazonaws.com","lambda.amazonaws.com"]
}
resources = ["*"]
sid = "allow-services-kms"
}
}

resource "aws_kms_key" "sns" {
count = var.sns_kms_encryption ? 1 : 0
deletion_window_in_days = 7
description = "SNS CMK Encryption Key"
enable_key_rotation = true
policy = data.aws_iam_policy_document.kms_policy_sns[0].json
}
6 changes: 3 additions & 3 deletions sns-topic.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
resource "aws_sns_topic" "default" {
count = var.sns_topic_name != "" ? 1 : 0
name = var.sns_topic_name

count = var.sns_topic_name != "" ? 1 : 0
name = var.sns_topic_name
kms_master_key_id = var.sns_kms_encryption ? aws_kms_key.sns[0].id : null # default key does not allow cloudwatch alarms to publish
# provisioner "local-exec" {
# command = "aws sns subscribe --topic-arn ${self.arn} --region ${data.aws_region.current.name} --protocol email --notification-endpoint ${var.sns_subscribe_list}"
# }
Expand Down

0 comments on commit 7697599

Please sign in to comment.