config_aggregator.tf 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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.config-notifications-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. enable_key_rotation = true
  76. }
  77. data "aws_iam_policy_document" "config-notifications-kms-policy" {
  78. statement {
  79. sid = "AllowServices"
  80. effect = "Allow"
  81. principals {
  82. identifiers = ["config.amazonaws.com", "sns.amazonaws.com", "sqs.amazonaws.com"]
  83. type = "Service"
  84. }
  85. actions = [
  86. "kms:GenerateDataKey",
  87. "kms:Decrypt"
  88. ]
  89. resources = [ "*" ]
  90. }
  91. statement {
  92. sid = "AllowOtherAccounts"
  93. effect = "Allow"
  94. principals {
  95. type = "AWS"
  96. identifiers = [ for a in var.responsible_accounts[var.environment]: "arn:${var.aws_partition}:iam::${a}:root" ]
  97. }
  98. actions = [
  99. "kms:GenerateDataKey",
  100. "kms:Encrypt"
  101. ]
  102. resources = [ "*" ]
  103. }
  104. # allow account to modify/manage key
  105. statement {
  106. sid = "AllowThisAccount"
  107. effect = "Allow"
  108. principals {
  109. identifiers = ["arn:${var.aws_partition}:iam::${var.aws_account_id}:root"]
  110. type = "AWS"
  111. }
  112. actions = [
  113. "kms:*"
  114. ]
  115. resources = ["*"]
  116. }
  117. }
  118. resource "aws_kms_alias" "config-notifications-key-alias" {
  119. name = "alias/config-notifications-key"
  120. target_key_id = aws_kms_key.config-notifications-key.key_id
  121. }