elb_bucket.tf 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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(local.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. tags = merge(local.standard_tags, var.tags)
  22. }
  23. resource "aws_s3_bucket_acl" "s3_acl_elb_logging_bucket" {
  24. bucket = aws_s3_bucket.elb_logging_bucket.id
  25. acl = "private"
  26. }
  27. resource "aws_s3_bucket_versioning" "s3_version_elb_logging_bucket" {
  28. bucket = aws_s3_bucket.elb_logging_bucket.id
  29. versioning_configuration {
  30. status = "Enabled"
  31. }
  32. }
  33. resource "aws_s3_bucket_logging" "elb_logging_bucket" {
  34. bucket = aws_s3_bucket.elb_logging_bucket.id
  35. target_bucket = module.elb_logging_logging_bucket.s3_bucket_name
  36. target_prefix = "${var.aws_account_id}-${var.aws_region}-elblogs/"
  37. }
  38. # tfsec:ignore:aws-s3-encryption-customer-key Risk is low for AES-256 encryption
  39. resource "aws_s3_bucket_server_side_encryption_configuration" "s3_sse_elb_logging_bucket" {
  40. bucket = aws_s3_bucket.elb_logging_bucket.id
  41. rule {
  42. apply_server_side_encryption_by_default {
  43. sse_algorithm = "AES256" # ELB logging only supports SSE-S3
  44. }
  45. }
  46. }
  47. resource "aws_s3_bucket_public_access_block" "aws_elb_bucket_block_public_access" {
  48. block_public_acls = true
  49. block_public_policy = true
  50. bucket = aws_s3_bucket.elb_logging_bucket.id
  51. ignore_public_acls = true
  52. restrict_public_buckets = true
  53. }
  54. data "aws_iam_policy_document" "aws_elb_bucket_policy" {
  55. statement {
  56. effect = "Allow"
  57. actions = ["s3:PutObject"]
  58. # principals {
  59. # type = "AWS"
  60. # identifiers = [ for a in local.responsible_accounts[var.environment]: "arn:${var.aws_partition}:iam::${a}:root" ]
  61. #}
  62. principals {
  63. type = "AWS"
  64. identifiers = [data.aws_elb_service_account.main.arn]
  65. }
  66. resources = ["arn:${var.aws_partition}:s3:::${aws_s3_bucket.elb_logging_bucket.bucket}/*"]
  67. }
  68. statement {
  69. effect = "Allow"
  70. actions = ["s3:PutObject"]
  71. principals {
  72. type = "Service"
  73. identifiers = ["delivery.logs.amazonaws.com"]
  74. }
  75. resources = ["arn:${var.aws_partition}:s3:::${aws_s3_bucket.elb_logging_bucket.bucket}/*"]
  76. condition {
  77. test = "StringEquals"
  78. variable = "s3:x-amz-acl"
  79. values = ["bucket-owner-full-control"]
  80. }
  81. }
  82. statement {
  83. effect = "Allow"
  84. actions = ["s3:GetBucketAcl"]
  85. principals {
  86. type = "Service"
  87. identifiers = ["delivery.logs.amazonaws.com"]
  88. }
  89. resources = ["arn:${var.aws_partition}:s3:::${aws_s3_bucket.elb_logging_bucket.bucket}"]
  90. }
  91. }
  92. resource "aws_s3_bucket_policy" "aws_elb_bucket_policy" {
  93. bucket = aws_s3_bucket.elb_logging_bucket.id
  94. policy = data.aws_iam_policy_document.aws_elb_bucket_policy.json
  95. # Ordering bug, see https://github.com/terraform-providers/terraform-provider-aws/issues/7628
  96. depends_on = [aws_s3_bucket_public_access_block.aws_elb_bucket_block_public_access]
  97. }
  98. #### SQS Queue for Splunk
  99. resource "aws_s3_bucket_notification" "on_new_elb_log" {
  100. bucket = aws_s3_bucket.elb_logging_bucket.bucket
  101. topic {
  102. topic_arn = aws_sns_topic.new_elb_log_event.arn
  103. events = [
  104. "s3:ObjectCreated:*",
  105. ]
  106. filter_suffix = ""
  107. }
  108. }
  109. resource "aws_sns_topic" "new_elb_log_event" {
  110. name = "s3-notification-topic-${aws_s3_bucket.elb_logging_bucket.bucket}"
  111. kms_master_key_id = aws_kms_key.new_object_key.id
  112. }
  113. resource "aws_sns_topic_policy" "elb_log" {
  114. arn = aws_sns_topic.new_elb_log_event.arn
  115. policy = data.aws_iam_policy_document.elblog_bucket_can_publish.json
  116. }
  117. data "aws_iam_policy_document" "elblog_bucket_can_publish" {
  118. statement {
  119. actions = [
  120. "SNS:Publish",
  121. ]
  122. effect = "Allow"
  123. condition {
  124. test = "ArnLike"
  125. variable = "aws:SourceArn"
  126. values = [
  127. aws_s3_bucket.elb_logging_bucket.arn
  128. ]
  129. }
  130. principals {
  131. type = "AWS"
  132. identifiers = ["*"]
  133. }
  134. resources = [
  135. aws_sns_topic.new_elb_log_event.arn
  136. ]
  137. sid = "allowpublish"
  138. }
  139. statement {
  140. actions = [
  141. "SNS:Subscribe",
  142. "SNS:Receive",
  143. ]
  144. effect = "Allow"
  145. principals {
  146. type = "AWS"
  147. identifiers = ["*"]
  148. }
  149. condition {
  150. test = "ArnEquals"
  151. values = [aws_sqs_queue.new_elblog.arn]
  152. variable = "aws:SourceArn"
  153. }
  154. resources = [
  155. aws_sns_topic.new_elb_log_event.arn
  156. ]
  157. sid = "sid_allow_subscribe"
  158. }
  159. }
  160. resource "aws_sqs_queue" "new_elblog" {
  161. name = "new-objects-for-${aws_s3_bucket.elb_logging_bucket.bucket}"
  162. visibility_timeout_seconds = 300 # wait 5 minutes before allowing a different splunk instance to process the same message
  163. message_retention_seconds = 604800 # Keep a message in the queue for 7 days
  164. receive_wait_time_seconds = 0 # how long to wait for a message before returning
  165. redrive_policy = "{\"deadLetterTargetArn\":\"${aws_sqs_queue.elblog-dlg.arn}\",\"maxReceiveCount\":4}"
  166. tags = merge(local.standard_tags, var.tags)
  167. kms_master_key_id = aws_kms_key.new_object_key.id
  168. kms_data_key_reuse_period_seconds = 3600
  169. }
  170. data "aws_iam_policy_document" "sns_topic_elblog_can_publish" {
  171. statement {
  172. effect = "Allow"
  173. principals {
  174. identifiers = [
  175. "*",
  176. ]
  177. type = "AWS"
  178. }
  179. actions = [
  180. "SQS:SendMessage",
  181. ]
  182. resources = [
  183. aws_sqs_queue.new_elblog.arn
  184. ]
  185. condition {
  186. test = "ArnEquals"
  187. values = [
  188. aws_sns_topic.new_elb_log_event.arn
  189. ]
  190. variable = "aws:SourceArn"
  191. }
  192. }
  193. }
  194. // Dead Letter queue, use same parameters as main queue
  195. resource "aws_sqs_queue" "elblog-dlg" {
  196. name = "new-objects-for-${aws_s3_bucket.elb_logging_bucket.bucket}-dlq"
  197. message_retention_seconds = 300
  198. receive_wait_time_seconds = 0
  199. tags = merge(local.standard_tags, var.tags)
  200. kms_master_key_id = aws_kms_key.new_object_key.id
  201. kms_data_key_reuse_period_seconds = 3600
  202. }
  203. resource "aws_sqs_queue_policy" "elblog_bucket_can_publish" {
  204. policy = data.aws_iam_policy_document.sns_topic_elblog_can_publish.json
  205. queue_url = aws_sqs_queue.new_elblog.id
  206. }
  207. resource "aws_sns_topic_subscription" "elblog_bucket_change_notification_to_queue" {
  208. topic_arn = aws_sns_topic.new_elb_log_event.arn
  209. protocol = "sqs"
  210. endpoint = aws_sqs_queue.new_elblog.arn
  211. }
  212. //AWS Provider outdated arguments <4.4.0
  213. /*resource "aws_s3_bucket" "elb_logging_bucket" {
  214. bucket = "xdr-elb-${var.environment}"
  215. acl = "private"
  216. tags = merge(local.standard_tags, var.tags)
  217. versioning {
  218. enabled = true
  219. }
  220. logging {
  221. target_bucket = module.elb_logging_logging_bucket.s3_bucket_name
  222. target_prefix = "${var.aws_account_id}-${var.aws_region}-elblogs/"
  223. }
  224. server_side_encryption_configuration {
  225. rule {
  226. apply_server_side_encryption_by_default {
  227. sse_algorithm = "AES256" # ELB logging only supports SSE-S3
  228. }
  229. }
  230. }
  231. }
  232. */