config_aggregator.tf 4.2 KB

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