account_alerts.tf 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # An SNS queue for email alerts
  2. resource "aws_sns_topic" "account-alerts" {
  3. name = "account-alerts"
  4. tags = merge(var.standard_tags, var.tags)
  5. }
  6. resource "aws_sns_topic_policy" "account-alerts" {
  7. arn = aws_sns_topic.account-alerts.arn
  8. policy = data.aws_iam_policy_document.account-alerts.json
  9. }
  10. data "aws_iam_policy_document" "account-alerts" {
  11. statement {
  12. sid = "AllowAllAccountsToPublish"
  13. actions = [ "SNS:Publish" ]
  14. effect = "Allow"
  15. resources = [ aws_sns_topic.account-alerts.arn ]
  16. principals {
  17. type = "AWS"
  18. identifiers = [ for a in var.responsible_accounts[var.environment]: "arn:${var.aws_partition}:iam::${a}:root" ]
  19. }
  20. }
  21. statement {
  22. sid = "AllowCloudWatchToPublic"
  23. actions = [ "SNS:Publish" ]
  24. effect = "Allow"
  25. resources = [ aws_sns_topic.account-alerts.arn ]
  26. principals {
  27. type = "Service"
  28. identifiers = [ "cloudwatch.amazonaws.com" ]
  29. }
  30. }
  31. }
  32. # Unfortunately, terraform does not support email destinations, so we can't manage subscriptions here.
  33. # SQS to get alerts into Splunk
  34. resource "aws_sqs_queue" "account-alerts" {
  35. name = "account-alerts"
  36. visibility_timeout_seconds = 300 # wait 5 minutes before allowing a different splunk instance to process the same message
  37. message_retention_seconds = 604800 # Keep a message in the queue for 7 days
  38. receive_wait_time_seconds = 0 # how long to wait for a message before returning
  39. redrive_policy = "{\"deadLetterTargetArn\":\"${aws_sqs_queue.account-alerts-dlq.arn}\",\"maxReceiveCount\":4}"
  40. tags = merge(var.standard_tags, var.tags)
  41. kms_master_key_id = aws_kms_key.account-alerts-key.id
  42. kms_data_key_reuse_period_seconds = 3600
  43. }
  44. data "aws_iam_policy_document" "account-alerts-sns-topic-can-publish" {
  45. statement {
  46. effect = "Allow"
  47. principals {
  48. identifiers = [ "*" ]
  49. type = "AWS"
  50. }
  51. actions = [ "SQS:SendMessage" ]
  52. resources = [ aws_sqs_queue.account-alerts.arn ]
  53. condition {
  54. test = "ArnEquals"
  55. values = [ aws_sns_topic.account-alerts.arn ]
  56. variable = "aws:SourceArn"
  57. }
  58. }
  59. }
  60. // Dead Letter queue, use same parameters as main queue
  61. resource "aws_sqs_queue" "account-alerts-dlq" {
  62. name = "account-alerts-dlq"
  63. message_retention_seconds = 300
  64. receive_wait_time_seconds = 0
  65. tags = merge(var.standard_tags, var.tags)
  66. kms_master_key_id = aws_kms_key.account-alerts-key.id
  67. kms_data_key_reuse_period_seconds = 3600
  68. }
  69. resource "aws_sqs_queue_policy" "account-alerts-can-publish" {
  70. policy = data.aws_iam_policy_document.account-alerts-sns-topic-can-publish.json
  71. queue_url = aws_sqs_queue.account-alerts.id
  72. }
  73. resource "aws_sns_topic_subscription" "account-alerts-to-queue" {
  74. topic_arn = aws_sns_topic.account-alerts.arn
  75. protocol = "sqs"
  76. endpoint = aws_sqs_queue.account-alerts.arn
  77. }
  78. resource "aws_kms_key" "account-alerts-key" {
  79. description = "Encryption of SNS and SQS queue for account alerts notifications"
  80. policy = data.aws_iam_policy_document.account-alerts-kms-policy.json
  81. enable_key_rotation = true
  82. }
  83. data "aws_iam_policy_document" "account-alerts-kms-policy" {
  84. statement {
  85. sid = "AllowServices"
  86. effect = "Allow"
  87. principals {
  88. identifiers = ["cloudwatch.amazonaws.com", "sns.amazonaws.com", "sqs.amazonaws.com"]
  89. type = "Service"
  90. }
  91. actions = [
  92. "kms:GenerateDataKey",
  93. "kms:Decrypt"
  94. ]
  95. resources = [ "*" ]
  96. }
  97. statement {
  98. sid = "AllowOtherAccounts"
  99. effect = "Allow"
  100. principals {
  101. type = "AWS"
  102. identifiers = [ for a in var.responsible_accounts[var.environment]: "arn:${var.aws_partition}:iam::${a}:root" ]
  103. }
  104. actions = [
  105. "kms:GenerateDataKey",
  106. "kms:Encrypt"
  107. ]
  108. resources = [ "*" ]
  109. }
  110. # allow account to modify/manage key
  111. statement {
  112. sid = "AllowThisAccount"
  113. effect = "Allow"
  114. principals {
  115. identifiers = ["arn:${var.aws_partition}:iam::${var.aws_account_id}:root"]
  116. type = "AWS"
  117. }
  118. actions = [
  119. "kms:*"
  120. ]
  121. resources = ["*"]
  122. }
  123. }
  124. resource "aws_kms_alias" "account-alerts-key-alias" {
  125. name = "alias/account-alerts-key"
  126. target_key_id = aws_kms_key.account-alerts-key.key_id
  127. }