| 1 | terraform { |
| 2 | required_version = ">= 1.11.0, < 2.0.0" |
| 3 | required_providers { |
| 4 | aws = { |
| 5 | source = "hashicorp/aws" |
| 6 | version = "~> 6.0" |
| 7 | } |
| 8 | } |
| 9 | } |
| 10 | |
| 11 | variable "name" { type = string } |
| 12 | resource "aws_s3_bucket" "this" { |
| 13 | bucket = var.name |
| 14 | force_destroy = false |
| 15 | lifecycle { prevent_destroy = true } |
| 16 | } |
| 17 | resource "aws_s3_bucket_versioning" "this" { |
| 18 | bucket = aws_s3_bucket.this.id |
| 19 | versioning_configuration { status = "Enabled" } |
| 20 | } |
| 21 | resource "aws_s3_bucket_server_side_encryption_configuration" "this" { |
| 22 | bucket = aws_s3_bucket.this.id |
| 23 | rule { |
| 24 | apply_server_side_encryption_by_default { sse_algorithm = "AES256" } |
| 25 | } |
| 26 | } |
| 27 | resource "aws_s3_bucket_public_access_block" "this" { |
| 28 | bucket = aws_s3_bucket.this.id |
| 29 | block_public_acls = true |
| 30 | block_public_policy = true |
| 31 | ignore_public_acls = true |
| 32 | restrict_public_buckets = true |
| 33 | } |
| 34 | resource "aws_s3_bucket_ownership_controls" "this" { |
| 35 | bucket = aws_s3_bucket.this.id |
| 36 | rule { object_ownership = "BucketOwnerEnforced" } |
| 37 | } |
| 38 | resource "aws_s3_bucket_policy" "this" { |
| 39 | bucket = aws_s3_bucket.this.id |
| 40 | policy = jsonencode({ |
| 41 | Version = "2012-10-17" |
| 42 | Statement = [{ |
| 43 | Effect = "Deny" |
| 44 | Principal = "*" |
| 45 | Action = "s3:*" |
| 46 | Resource = [aws_s3_bucket.this.arn, "${aws_s3_bucket.this.arn}/*"] |
| 47 | Condition = { Bool = { "aws:SecureTransport" = "false" } } |
| 48 | }] |
| 49 | }) |
| 50 | depends_on = [aws_s3_bucket_public_access_block.this] |
| 51 | } |
| 52 | output "name" { |
| 53 | value = aws_s3_bucket.this.id |
| 54 | depends_on = [ |
| 55 | aws_s3_bucket_versioning.this, |
| 56 | aws_s3_bucket_server_side_encryption_configuration.this, |
| 57 | aws_s3_bucket_public_access_block.this, |
| 58 | aws_s3_bucket_ownership_controls.this, |
| 59 | aws_s3_bucket_policy.this, |
| 60 | aws_s3_bucket_lifecycle_configuration.this, |
| 61 | ] |
| 62 | } |
| 63 | output "arn" { |
| 64 | value = aws_s3_bucket.this.arn |
| 65 | depends_on = [ |
| 66 | aws_s3_bucket_versioning.this, |
| 67 | aws_s3_bucket_server_side_encryption_configuration.this, |
| 68 | aws_s3_bucket_public_access_block.this, |
| 69 | aws_s3_bucket_ownership_controls.this, |
| 70 | aws_s3_bucket_policy.this, |
| 71 | aws_s3_bucket_lifecycle_configuration.this, |
| 72 | ] |
| 73 | } |
| 74 | |
| 75 | variable "control_plane" { |
| 76 | type = bool |
| 77 | default = false |
| 78 | } |
| 79 | resource "aws_s3_bucket_lifecycle_configuration" "this" { |
| 80 | bucket = aws_s3_bucket.this.id |
| 81 | rule { |
| 82 | id = "abandoned-uploads" |
| 83 | status = "Enabled" |
| 84 | filter {} |
| 85 | abort_incomplete_multipart_upload { days_after_initiation = 7 } |
| 86 | } |
| 87 | dynamic "rule" { |
| 88 | for_each = var.control_plane ? [1] : [] |
| 89 | content { |
| 90 | id = "geoip-noncurrent" |
| 91 | status = "Enabled" |
| 92 | filter { prefix = "control/geoip/" } |
| 93 | noncurrent_version_expiration { noncurrent_days = 1 } |
| 94 | abort_incomplete_multipart_upload { days_after_initiation = 1 } |
| 95 | } |
| 96 | } |
| 97 | dynamic "rule" { |
| 98 | for_each = var.control_plane ? [1] : [] |
| 99 | content { |
| 100 | id = "db-reads-expire" |
| 101 | status = "Enabled" |
| 102 | filter { prefix = "db-reads/" } |
| 103 | expiration { days = 1 } |
| 104 | noncurrent_version_expiration { noncurrent_days = 1 } |
| 105 | abort_incomplete_multipart_upload { days_after_initiation = 1 } |
| 106 | } |
| 107 | } |
| 108 | dynamic "rule" { |
| 109 | for_each = var.control_plane ? [1] : [] |
| 110 | content { |
| 111 | id = "db-reads-delete-markers" |
| 112 | status = "Enabled" |
| 113 | filter { prefix = "db-reads/" } |
| 114 | expiration { expired_object_delete_marker = true } |
| 115 | } |
| 116 | } |
| 117 | depends_on = [aws_s3_bucket_versioning.this] |
| 118 | } |
| 119 | |