main.tf 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. data "aws_caller_identity" "current" {
  2. }
  3. data "aws_partition" "current" {
  4. }
  5. locals {
  6. account_id = data.aws_caller_identity.current.account_id
  7. partition = data.aws_partition.current.partition
  8. bucket_name = var.bucket_name == "" ? "${local.account_id}-${var.region}-cloudtrail" : var.bucket_name
  9. # Account IDs that will have access to stream CloudTrail logs
  10. account_ids = concat([local.account_id], var.allowed_account_ids)
  11. # Format account IDs into necessary resource lists.
  12. bucket_policy_put_resources = formatlist("${aws_s3_bucket.this.arn}/AWSLogs/%s/*", local.account_ids)
  13. kms_key_encrypt_resources = formatlist("arn:${local.partition}:cloudtrail:*:%s:trail/*", local.account_ids)
  14. }
  15. resource "aws_s3_bucket" "this" {
  16. bucket = local.bucket_name
  17. acl = "private"
  18. tags = var.tags
  19. # If we want to PR this upstream, we have to find a way to leave this enabled, but we don't need/want
  20. # it since we import into Splunk.
  21. #lifecycle_rule {
  22. # enabled = true
  23. #
  24. # transition {
  25. # days = 30
  26. # storage_class = "STANDARD_IA"
  27. # }
  28. #
  29. #}
  30. dynamic "lifecycle_rule" {
  31. iterator = rule
  32. for_each = var.lifecycle_rules
  33. content {
  34. id = rule.value.id
  35. enabled = rule.value.enabled
  36. prefix = lookup(rule.value, "prefix", null)
  37. abort_incomplete_multipart_upload_days = lookup(rule.value, "abort_incomplete_multipart_upload_days", 0)
  38. expiration {
  39. days = lookup(rule.value, "expiration", 2147483647)
  40. }
  41. noncurrent_version_expiration {
  42. days = lookup(rule.value, "noncurrent_version_expiration", 2147483647)
  43. }
  44. }
  45. }
  46. logging {
  47. target_bucket = var.logging_bucket
  48. target_prefix = "${local.account_id}-${var.region}-cloudtrail/"
  49. }
  50. server_side_encryption_configuration {
  51. rule {
  52. apply_server_side_encryption_by_default {
  53. sse_algorithm = "aws:kms"
  54. kms_master_key_id = aws_kms_key.this.arn
  55. }
  56. }
  57. }
  58. versioning {
  59. enabled = true
  60. }
  61. lifecycle {
  62. prevent_destroy = true
  63. }
  64. }
  65. resource "aws_s3_bucket_public_access_block" "this" {
  66. bucket = aws_s3_bucket.this.id
  67. block_public_acls = true
  68. block_public_policy = true
  69. ignore_public_acls = true
  70. restrict_public_buckets = true
  71. }
  72. data "aws_iam_policy_document" "this" {
  73. statement {
  74. actions = ["s3:GetBucketAcl"]
  75. effect = "Allow"
  76. resources = [aws_s3_bucket.this.arn]
  77. principals {
  78. type = "Service"
  79. identifiers = ["cloudtrail.amazonaws.com"]
  80. }
  81. }
  82. statement {
  83. actions = ["s3:PutObject"]
  84. effect = "Allow"
  85. resources = local.bucket_policy_put_resources
  86. condition {
  87. test = "StringEquals"
  88. variable = "s3:x-amz-acl"
  89. values = ["bucket-owner-full-control"]
  90. }
  91. principals {
  92. type = "Service"
  93. identifiers = ["cloudtrail.amazonaws.com"]
  94. }
  95. }
  96. }
  97. resource "aws_s3_bucket_policy" "this" {
  98. bucket = aws_s3_bucket.this.id
  99. policy = data.aws_iam_policy_document.this.json
  100. }