s3.tf 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # tfsec:ignore:aws-s3-enable-bucket-logging TODO: enable everywhere at a later date if required
  2. resource "aws_s3_bucket" "bucket" {
  3. # checkov:skip=CKV_AWS_18: see tfsec S3 logging above
  4. # checkov:skip=CKV_AWS_144: TODO: cross replication
  5. bucket = local.fullname
  6. tags = merge(local.standard_tags, var.tags)
  7. }
  8. resource "aws_s3_bucket_versioning" "s3_version_bucket" {
  9. bucket = aws_s3_bucket.bucket.id
  10. versioning_configuration {
  11. status = "Enabled"
  12. }
  13. }
  14. resource "aws_s3_bucket_acl" "s3_acl_bucket" {
  15. bucket = aws_s3_bucket.bucket.id
  16. acl = "private"
  17. }
  18. resource "aws_s3_bucket_server_side_encryption_configuration" "s3_sse_bucket" {
  19. bucket = aws_s3_bucket.bucket.id
  20. rule {
  21. apply_server_side_encryption_by_default {
  22. kms_master_key_id = aws_kms_key.bucketkey.arn
  23. sse_algorithm = "aws:kms"
  24. }
  25. }
  26. }
  27. resource "aws_s3_bucket_lifecycle_configuration" "s3_lifecyle_bucket" {
  28. bucket = aws_s3_bucket.bucket.id
  29. rule {
  30. id = "INTELLIGENT_TIERING"
  31. status = "Enabled"
  32. filter {} # Required for noncurrent_version_expiration to work
  33. abort_incomplete_multipart_upload {
  34. days_after_initiation = 2
  35. }
  36. transition {
  37. days = 30
  38. storage_class = "INTELLIGENT_TIERING"
  39. }
  40. noncurrent_version_expiration {
  41. # We always keep the current version and the previous version, and delete any other versions after 90 days
  42. newer_noncurrent_versions = 2
  43. noncurrent_days = 90
  44. }
  45. }
  46. }
  47. resource "aws_s3_bucket_public_access_block" "public_access_block" {
  48. bucket = aws_s3_bucket.bucket.id
  49. block_public_acls = true
  50. block_public_policy = true
  51. ignore_public_acls = true
  52. restrict_public_buckets = true
  53. # Not technically dependent, but prevents a "Conflicting conditional operation" conflict.
  54. # See https://github.com/hashicorp/terraform-provider-aws/issues/7628
  55. depends_on = [aws_s3_bucket_policy.s3_bucket_policy]
  56. }
  57. data "aws_iam_policy_document" "s3_bucket_policy" {
  58. statement {
  59. sid = "AccountAllow"
  60. effect = "Allow"
  61. resources = [
  62. aws_s3_bucket.bucket.arn,
  63. "${aws_s3_bucket.bucket.arn}/*",
  64. ]
  65. actions = [
  66. "s3:GetObject",
  67. "s3:ListBucket",
  68. ]
  69. principals {
  70. type = "AWS"
  71. identifiers = local.principals
  72. }
  73. }
  74. }
  75. resource "aws_s3_bucket_policy" "s3_bucket_policy" {
  76. depends_on = [aws_iam_role.role] # 2022-04-22: FTD: Copied this across, but not sure why this dependency.
  77. bucket = aws_s3_bucket.bucket.id
  78. policy = data.aws_iam_policy_document.s3_bucket_policy.json
  79. }
  80. resource "aws_s3_bucket_cors_configuration" "s3_cors_config" {
  81. count = length(var.cors_rules) > 0 ? 1 : 0
  82. bucket = aws_s3_bucket.bucket.id
  83. dynamic "cors_rule" {
  84. for_each = var.cors_rules
  85. iterator = each
  86. content {
  87. id = try(each.value.id, null)
  88. allowed_methods = each.value.allowed_methods
  89. allowed_origins = each.value.allowed_origins
  90. allowed_headers = try(each.value.allowed_headers, null)
  91. expose_headers = try(each.value.expose_headers, null)
  92. max_age_seconds = try(each.value.max_age_seconds, null)
  93. }
  94. }
  95. }