elb_bucket.tf 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. # The centralized bucket for ELB Logging
  2. data "aws_elb_service_account" "main" {} # ELB logs use a single aws account to place logs
  3. module "elb_logging_logging_bucket" {
  4. source = "../../thirdparty/terraform-aws-s3logging-bucket"
  5. bucket_name = "xdr-elb-${var.environment}-access-logs"
  6. lifecycle_rules = [
  7. {
  8. id = "expire-old-logs"
  9. enabled = true
  10. prefix = ""
  11. expiration = 30
  12. noncurrent_version_expiration = 30
  13. abort_incomplete_multipart_upload_days = 7
  14. }
  15. ]
  16. tags = merge(var.standard_tags, var.tags, { "Note" = "ELB Logging Does Not Support SSE-KMS. Only SSE-S3 is supported." } )
  17. versioning_enabled = true
  18. }
  19. resource "aws_s3_bucket" "elb_logging_bucket" {
  20. bucket = "xdr-elb-${var.environment}"
  21. acl = "private"
  22. tags = merge(var.standard_tags, var.tags)
  23. versioning {
  24. enabled = true
  25. }
  26. logging {
  27. target_bucket = module.elb_logging_logging_bucket.s3_bucket_name
  28. target_prefix = "${var.aws_account_id}-${var.aws_region}-elblogs/"
  29. }
  30. server_side_encryption_configuration {
  31. rule {
  32. apply_server_side_encryption_by_default {
  33. sse_algorithm = "AES256" # ELB logging only supports SSE-S3
  34. }
  35. }
  36. }
  37. }
  38. resource "aws_s3_bucket_public_access_block" "aws_elb_bucket_block_public_access" {
  39. block_public_acls = true
  40. block_public_policy = true
  41. bucket = aws_s3_bucket.elb_logging_bucket.id
  42. ignore_public_acls = true
  43. restrict_public_buckets = true
  44. }
  45. data "aws_iam_policy_document" "aws_elb_bucket_policy" {
  46. statement {
  47. effect = "Allow"
  48. actions = ["s3:PutObject"]
  49. # principals {
  50. # type = "AWS"
  51. # identifiers = [ for a in var.responsible_accounts[var.environment]: "arn:${var.aws_partition}:iam::${a}:root" ]
  52. #}
  53. principals {
  54. type = "AWS"
  55. identifiers = [ data.aws_elb_service_account.main.arn ]
  56. }
  57. resources = ["arn:${var.aws_partition}:s3:::${aws_s3_bucket.elb_logging_bucket.bucket}/*"]
  58. }
  59. statement {
  60. effect = "Allow"
  61. actions = [ "s3:PutObject" ]
  62. principals {
  63. type = "Service"
  64. identifiers = [ "delivery.logs.amazonaws.com" ]
  65. }
  66. resources = [ "arn:${var.aws_partition}:s3:::${aws_s3_bucket.elb_logging_bucket.bucket}/*" ]
  67. condition {
  68. test = "StringEquals"
  69. variable = "s3:x-amz-acl"
  70. values = [ "bucket-owner-full-control" ]
  71. }
  72. }
  73. statement {
  74. effect = "Allow"
  75. actions = [ "s3:GetBucketAcl" ]
  76. principals {
  77. type = "Service"
  78. identifiers = [ "delivery.logs.amazonaws.com" ]
  79. }
  80. resources = [ "arn:${var.aws_partition}:s3:::${aws_s3_bucket.elb_logging_bucket.bucket}" ]
  81. }
  82. }
  83. resource "aws_s3_bucket_policy" "aws_elb_bucket_policy" {
  84. bucket = aws_s3_bucket.elb_logging_bucket.id
  85. policy = data.aws_iam_policy_document.aws_elb_bucket_policy.json
  86. # Ordering bug, see https://github.com/terraform-providers/terraform-provider-aws/issues/7628
  87. depends_on = [ aws_s3_bucket_public_access_block.aws_elb_bucket_block_public_access ]
  88. }
  89. #### SQS Queue for Splunk
  90. resource "aws_s3_bucket_notification" "on_new_elb_log" {
  91. bucket = aws_s3_bucket.elb_logging_bucket.bucket
  92. topic {
  93. topic_arn = aws_sns_topic.new_elb_log_event.arn
  94. events = [
  95. "s3:ObjectCreated:*",
  96. ]
  97. filter_suffix = ""
  98. }
  99. }
  100. resource "aws_sns_topic" "new_elb_log_event" {
  101. name = "s3-notification-topic-${aws_s3_bucket.elb_logging_bucket.bucket}"
  102. kms_master_key_id = aws_kms_key.new_object_key.id
  103. }
  104. resource "aws_sns_topic_policy" "elb_log" {
  105. arn = aws_sns_topic.new_elb_log_event.arn
  106. policy = data.aws_iam_policy_document.elblog_bucket_can_publish.json
  107. }
  108. data "aws_iam_policy_document" "elblog_bucket_can_publish" {
  109. statement {
  110. actions = [
  111. "SNS:Publish",
  112. ]
  113. effect = "Allow"
  114. condition {
  115. test = "ArnLike"
  116. variable = "aws:SourceArn"
  117. values = [
  118. aws_s3_bucket.elb_logging_bucket.arn
  119. ]
  120. }
  121. principals {
  122. type = "AWS"
  123. identifiers = ["*"]
  124. }
  125. resources = [
  126. aws_sns_topic.new_elb_log_event.arn
  127. ]
  128. sid = "allowpublish"
  129. }
  130. statement {
  131. actions = [
  132. "SNS:Subscribe",
  133. "SNS:Receive",
  134. ]
  135. effect = "Allow"
  136. principals {
  137. type = "AWS"
  138. identifiers = ["*"]
  139. }
  140. condition {
  141. test = "ArnEquals"
  142. values = [ aws_sqs_queue.new_elblog.arn ]
  143. variable = "aws:SourceArn"
  144. }
  145. resources = [
  146. aws_sns_topic.new_elb_log_event.arn
  147. ]
  148. sid = "sid_allow_subscribe"
  149. }
  150. }
  151. resource "aws_sqs_queue" "new_elblog" {
  152. name = "new-objects-for-${aws_s3_bucket.elb_logging_bucket.bucket}"
  153. visibility_timeout_seconds = 300 # wait 5 minutes before allowing a different splunk instance to process the same message
  154. message_retention_seconds = 604800 # Keep a message in the queue for 7 days
  155. receive_wait_time_seconds = 0 # how long to wait for a message before returning
  156. redrive_policy = "{\"deadLetterTargetArn\":\"${aws_sqs_queue.elblog-dlg.arn}\",\"maxReceiveCount\":4}"
  157. tags = merge(var.standard_tags, var.tags)
  158. kms_master_key_id = aws_kms_key.new_object_key.id
  159. kms_data_key_reuse_period_seconds = 3600
  160. }
  161. data "aws_iam_policy_document" "sns_topic_elblog_can_publish" {
  162. statement {
  163. effect = "Allow"
  164. principals {
  165. identifiers = [
  166. "*",
  167. ]
  168. type = "AWS"
  169. }
  170. actions = [
  171. "SQS:SendMessage",
  172. ]
  173. resources = [
  174. aws_sqs_queue.new_elblog.arn
  175. ]
  176. condition {
  177. test = "ArnEquals"
  178. values = [
  179. aws_sns_topic.new_elb_log_event.arn
  180. ]
  181. variable = "aws:SourceArn"
  182. }
  183. }
  184. }
  185. // Dead Letter queue, use same parameters as main queue
  186. resource "aws_sqs_queue" "elblog-dlg" {
  187. name = "new-objects-for-${aws_s3_bucket.elb_logging_bucket.bucket}-dlq"
  188. message_retention_seconds = 300
  189. receive_wait_time_seconds = 0
  190. tags = merge(var.standard_tags, var.tags)
  191. kms_master_key_id = aws_kms_key.new_object_key.id
  192. kms_data_key_reuse_period_seconds = 3600
  193. }
  194. resource "aws_sqs_queue_policy" "elblog_bucket_can_publish" {
  195. policy = data.aws_iam_policy_document.sns_topic_elblog_can_publish.json
  196. queue_url = aws_sqs_queue.new_elblog.id
  197. }
  198. resource "aws_sns_topic_subscription" "elblog_bucket_change_notification_to_queue" {
  199. topic_arn = aws_sns_topic.new_elb_log_event.arn
  200. protocol = "sqs"
  201. endpoint = aws_sqs_queue.new_elblog.arn
  202. }