config_aggregator.tf 4.4 KB

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