main.tf 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. data "aws_caller_identity" "current" {
  2. }
  3. data "aws_region" "current" {
  4. }
  5. locals {
  6. account_id = data.aws_caller_identity.current.account_id
  7. bucket_name = coalesce(
  8. var.bucket_name,
  9. "${local.account_id}-${local.region}-s3logging-${var.bucket_suffix}"
  10. )
  11. region = data.aws_region.current.name
  12. }
  13. resource "aws_s3_bucket" "this" {
  14. bucket = local.bucket_name
  15. acl = "log-delivery-write"
  16. tags = var.tags
  17. dynamic "lifecycle_rule" {
  18. iterator = rule
  19. for_each = var.lifecycle_rules
  20. content {
  21. id = rule.value.id
  22. enabled = rule.value.enabled
  23. prefix = lookup(rule.value, "prefix", null)
  24. abort_incomplete_multipart_upload_days = lookup(rule.value, "abort_incomplete_multipart_upload_days", 0)
  25. expiration {
  26. days = lookup(rule.value, "expiration", 2147483647)
  27. }
  28. noncurrent_version_expiration {
  29. days = lookup(rule.value, "noncurrent_version_expiration", 2147483647)
  30. }
  31. }
  32. }
  33. server_side_encryption_configuration {
  34. rule {
  35. apply_server_side_encryption_by_default {
  36. sse_algorithm = "aws:kms"
  37. }
  38. }
  39. }
  40. versioning {
  41. enabled = var.versioning_enabled
  42. }
  43. lifecycle {
  44. ignore_changes = [versioning[0].mfa_delete]
  45. }
  46. # Conformance Pack for CIS requires access logs on all S3 buckets and is a best
  47. # practice.
  48. #
  49. # Logging to the bucket itself is allowed, but if we ingest into splunk, make
  50. # sure we don't set up a feedback loop (splunk accesses s3 bucket to get a log
  51. # which creates a log which leads to splunk accessing the s3 bucket)
  52. logging {
  53. target_bucket = local.bucket_name
  54. target_prefix = "${data.aws_caller_identity.current.account_id}-${data.aws_region.current.name}-${local.bucket_name}"
  55. }
  56. }
  57. resource "aws_s3_bucket_public_access_block" "this" {
  58. bucket = aws_s3_bucket.this.id
  59. block_public_acls = true
  60. block_public_policy = true
  61. ignore_public_acls = true
  62. restrict_public_buckets = true
  63. }