|
1 | | -resource "aws_kms_key" "bucket" { |
2 | | - for_each = var.encryption_key_arn != null ? toset([]) : toset(["this"]) |
3 | | - |
4 | | - description = "Encryption key for bucket ${local.bucket_name}" |
5 | | - deletion_window_in_days = var.key_recovery_period |
6 | | - enable_key_rotation = true |
7 | | - policy = jsonencode(yamldecode(templatefile("${path.module}/templates/key-policy.yaml.tftpl", { |
8 | | - account : data.aws_caller_identity.identity.account_id |
9 | | - bucket : local.bucket_name |
10 | | - partition : data.aws_partition.current.partition |
11 | | - principals : var.allowed_principals |
12 | | - }))) |
| 1 | +resource "random_string" "suffix" { |
| 2 | + count = var.add_suffix ? 1 : 0 |
| 3 | + |
| 4 | + length = local.suffix_length |
| 5 | + lower = true |
| 6 | + numeric = true |
| 7 | + special = false |
| 8 | + upper = false |
| 9 | +} |
| 10 | + |
| 11 | +resource "aws_s3_bucket" "this" { |
| 12 | + bucket = local.bucket_name |
| 13 | + force_destroy = var.force_delete |
13 | 14 |
|
14 | 15 | tags = local.tags |
| 16 | + |
| 17 | + lifecycle { |
| 18 | + precondition { |
| 19 | + condition = length(local.bucket_name) >= 3 && length(local.bucket_name) <= 63 |
| 20 | + error_message = <<-EOT |
| 21 | + The bucket name must be between 3 and 63 characters. Shorten project, |
| 22 | + environment, or name, or set add_suffix to true to truncate the name |
| 23 | + automatically. |
| 24 | + EOT |
| 25 | + } |
| 26 | + } |
15 | 27 | } |
16 | 28 |
|
17 | | -resource "aws_kms_alias" "bucket" { |
18 | | - for_each = var.encryption_key_arn != null ? toset([]) : toset(["this"]) |
| 29 | +resource "aws_s3_bucket_public_access_block" "this" { |
| 30 | + bucket = aws_s3_bucket.this.id |
19 | 31 |
|
20 | | - name = "alias/${local.bucket_name}" |
21 | | - target_key_id = aws_kms_key.bucket["this"].arn |
| 32 | + block_public_acls = true |
| 33 | + block_public_policy = true |
| 34 | + ignore_public_acls = true |
| 35 | + restrict_public_buckets = true |
22 | 36 | } |
23 | 37 |
|
24 | | -module "this" { |
25 | | - source = "boldlink/s3/aws" |
26 | | - version = "2.6.0" |
| 38 | +resource "aws_s3_bucket_ownership_controls" "this" { |
| 39 | + bucket = aws_s3_bucket.this.id |
27 | 40 |
|
28 | | - bucket = local.bucket_name |
29 | | - force_destroy = var.force_delete |
| 41 | + rule { |
| 42 | + object_ownership = "BucketOwnerEnforced" |
| 43 | + } |
| 44 | +} |
30 | 45 |
|
31 | | - bucket_policy = jsonencode(yamldecode(templatefile("${path.module}/templates/bucket-policy.yaml.tftpl", { |
32 | | - partition : data.aws_partition.current.partition |
33 | | - bucket : local.bucket_name |
34 | | - }))) |
| 46 | +resource "aws_s3_bucket_versioning" "this" { |
| 47 | + bucket = aws_s3_bucket.this.id |
35 | 48 |
|
36 | | - lifecycle_configuration = [{ |
| 49 | + versioning_configuration { |
| 50 | + status = "Enabled" |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +resource "aws_s3_bucket_object_lock_configuration" "this" { |
| 55 | + for_each = var.object_lock.enabled ? toset(["this"]) : toset([]) |
| 56 | + |
| 57 | + # Object lock requires versioning to be enabled on the bucket first. |
| 58 | + depends_on = [aws_s3_bucket_versioning.this] |
| 59 | + |
| 60 | + bucket = aws_s3_bucket.this.id |
| 61 | + |
| 62 | + dynamic "rule" { |
| 63 | + for_each = var.object_lock.days != null ? toset(["this"]) : toset([]) |
| 64 | + |
| 65 | + content { |
| 66 | + default_retention { |
| 67 | + mode = var.object_lock.mode |
| 68 | + days = var.object_lock.days |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +resource "aws_s3_bucket_server_side_encryption_configuration" "this" { |
| 75 | + bucket = aws_s3_bucket.this.id |
| 76 | + |
| 77 | + rule { |
| 78 | + apply_server_side_encryption_by_default { |
| 79 | + kms_master_key_id = local.kms_key_arn |
| 80 | + sse_algorithm = "aws:kms" |
| 81 | + } |
| 82 | + |
| 83 | + bucket_key_enabled = true |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +resource "aws_s3_bucket_logging" "this" { |
| 88 | + bucket = aws_s3_bucket.this.id |
| 89 | + |
| 90 | + target_bucket = var.logging_bucket |
| 91 | + target_prefix = "${local.logs_path}/s3accesslogs/${local.bucket_name}" |
| 92 | +} |
| 93 | + |
| 94 | +resource "aws_s3_bucket_lifecycle_configuration" "this" { |
| 95 | + # Referencing noncurrent-version behavior requires versioning to be enabled |
| 96 | + # first. |
| 97 | + depends_on = [aws_s3_bucket_versioning.this] |
| 98 | + |
| 99 | + bucket = aws_s3_bucket.this.id |
| 100 | + |
| 101 | + rule { |
37 | 102 | id = "state" |
38 | 103 | status = "Enabled" |
39 | 104 |
|
40 | | - filter = { |
| 105 | + filter { |
41 | 106 | prefix = "" |
42 | 107 | } |
43 | 108 |
|
44 | | - abort_incomplete_multipart_upload_days = var.abort_incomplete_multipart_upload_days |
| 109 | + abort_incomplete_multipart_upload { |
| 110 | + days_after_initiation = var.abort_incomplete_multipart_upload_days |
| 111 | + } |
45 | 112 |
|
46 | | - noncurrent_version_expiration = [{ |
| 113 | + noncurrent_version_expiration { |
47 | 114 | noncurrent_days = var.noncurrent_version_expiration_days |
48 | | - }] |
49 | | - |
50 | | - transition = var.storage_class_transitions |
51 | | - }] |
52 | | - |
53 | | - sse_bucket_key_enabled = true |
54 | | - sse_kms_master_key_arn = local.kms_key_arn |
55 | | - sse_sse_algorithm = "aws:kms" |
| 115 | + } |
56 | 116 |
|
57 | | - versioning_status = "Enabled" |
| 117 | + dynamic "expiration" { |
| 118 | + for_each = var.expiration != null ? toset(["this"]) : toset([]) |
| 119 | + content { |
| 120 | + days = var.expiration |
| 121 | + } |
| 122 | + } |
58 | 123 |
|
59 | | - s3_logging = { |
60 | | - target_bucket = var.logging_bucket |
61 | | - target_prefix = "${local.logs_path}/s3accesslogs/${local.bucket_name}" |
| 124 | + dynamic "transition" { |
| 125 | + for_each = var.storage_class_transitions |
| 126 | + content { |
| 127 | + days = transition.value.days |
| 128 | + storage_class = transition.value.storage_class |
| 129 | + } |
| 130 | + } |
62 | 131 | } |
| 132 | +} |
63 | 133 |
|
64 | | - tags = local.tags |
| 134 | +resource "aws_s3_bucket_policy" "this" { |
| 135 | + # The public access block must be in place before a bucket policy can be |
| 136 | + # applied. |
| 137 | + depends_on = [aws_s3_bucket_public_access_block.this] |
| 138 | + |
| 139 | + bucket = aws_s3_bucket.this.id |
| 140 | + policy = jsonencode(merge(local.base_bucket_policy, { |
| 141 | + Statement = concat(local.base_bucket_policy.Statement, var.additional_policy_statements) |
| 142 | + })) |
65 | 143 | } |
0 commit comments