main.tf 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. tags = var.tags
  18. lifecycle {
  19. prevent_destroy = true
  20. }
  21. }
  22. resource "aws_s3_bucket_lifecycle_configuration" "this" {
  23. bucket = aws_s3_bucket.this.id
  24. count = length(var.lifecycle_rules) > 0 ? 1 : 0 # handle the case of no lifecycle rules
  25. dynamic "rule" {
  26. for_each = var.lifecycle_rules
  27. content {
  28. id = rule.value.id
  29. status = rule.value.enabled == true ? "Enabled" : "Disabled"
  30. filter {
  31. prefix = lookup(rule.value, "prefix", null)
  32. }
  33. abort_incomplete_multipart_upload {
  34. days_after_initiation = lookup(rule.value, "abort_incomplete_multipart_upload_days", 0)
  35. }
  36. expiration {
  37. days = lookup(rule.value, "expiration", 2147483647)
  38. }
  39. noncurrent_version_expiration {
  40. noncurrent_days = lookup(rule.value, "noncurrent_version_expiration", 2147483647)
  41. }
  42. }
  43. }
  44. }
  45. resource "aws_s3_bucket_logging" "this" {
  46. bucket = aws_s3_bucket.this.id
  47. target_bucket = var.logging_bucket
  48. target_prefix = "${local.account_id}-${var.region}-cloudtrail/"
  49. }
  50. resource "aws_s3_bucket_versioning" "this" {
  51. bucket = aws_s3_bucket.this.id
  52. versioning_configuration {
  53. status = "Enabled"
  54. }
  55. }
  56. resource "aws_s3_bucket_acl" "this" {
  57. bucket = aws_s3_bucket.this.id
  58. acl = "private"
  59. }
  60. resource "aws_s3_bucket_server_side_encryption_configuration" "kinesis_firehose_s3_bucket" {
  61. bucket = aws_s3_bucket.this.id
  62. rule {
  63. apply_server_side_encryption_by_default {
  64. sse_algorithm = "aws:kms"
  65. kms_master_key_id = aws_kms_key.this.arn
  66. }
  67. }
  68. }
  69. resource "aws_s3_bucket_public_access_block" "this" {
  70. bucket = aws_s3_bucket.this.id
  71. block_public_acls = true
  72. block_public_policy = true
  73. ignore_public_acls = true
  74. restrict_public_buckets = true
  75. }
  76. data "aws_iam_policy_document" "this" {
  77. statement {
  78. actions = ["s3:GetBucketAcl"]
  79. effect = "Allow"
  80. resources = [aws_s3_bucket.this.arn]
  81. principals {
  82. type = "Service"
  83. identifiers = ["cloudtrail.amazonaws.com"]
  84. }
  85. }
  86. statement {
  87. actions = ["s3:PutObject"]
  88. effect = "Allow"
  89. resources = local.bucket_policy_put_resources
  90. condition {
  91. test = "StringEquals"
  92. variable = "s3:x-amz-acl"
  93. values = ["bucket-owner-full-control"]
  94. }
  95. principals {
  96. type = "Service"
  97. identifiers = ["cloudtrail.amazonaws.com"]
  98. }
  99. }
  100. }
  101. resource "aws_s3_bucket_policy" "this" {
  102. bucket = aws_s3_bucket.this.id
  103. policy = data.aws_iam_policy_document.this.json
  104. }