Skip to content

Commit 5493aa3

Browse files
authored
fix: Update module default lifecycle rules to work better with AWS Provider 5.89+ (#413)
* fix: Update module default lifecycle rules to work with AWS 5.89+ * Update Readme * Remove commented out lines * Found bug
1 parent 961bdd9 commit 5493aa3

File tree

4 files changed

+78
-54
lines changed

4 files changed

+78
-54
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ module "aws-s3-bucket" {
4545
| Name | Version |
4646
|------|---------|
4747
| terraform | >= 1.0 |
48-
| aws | >= 5.43.0 |
48+
| aws | >= 5.89.0 |
4949

5050
## Providers
5151

5252
| Name | Version |
5353
|------|---------|
54-
| aws | >= 5.43.0 |
54+
| aws | >= 5.89.0 |
5555

5656
## Resources
5757

@@ -90,19 +90,19 @@ module "aws-s3-bucket" {
9090
| enable_bucket_force_destroy | If set to true, Bucket will be emptied and destroyed when terraform destroy is run. | `bool` | `false` | no |
9191
| enable_bucket_inventory | If set to true, Bucket Inventory will be enabled. | `bool` | `false` | no |
9292
| enable_s3_public_access_block | Bool for toggling whether the s3 public access block resource should be enabled. | `bool` | `true` | no |
93-
| expiration | expiration blocks | `list(any)` | ```[ { "expired_object_delete_marker": true } ]``` | no |
9493
| inventory_bucket_format | The format for the inventory file. Default is ORC. Options are ORC or CSV. | `string` | `"ORC"` | no |
9594
| kms_master_key_id | The AWS KMS master key ID used for the SSE-KMS encryption. If blank, bucket encryption configuration defaults to AES256. | `string` | `""` | no |
95+
| lifecycle_abort_incomplete_upload | Default values for the abort incomplete mutlipart uploads lifecycle rule | `map` | ```{ "expiration": { "date": null, "days": null, "expired_object_delete_marker": true }, "nve": { "newer_noncurrent_versions": null, "noncurrent_days": 365 }, "nvt": { "newer_noncurrent_versions": null, "noncurrent_days": 30, "storage_class": "STANDARD_IA" }, "transition": null }``` | no |
96+
| lifecycle_aws_bucket_analytics_expiration | Number of days to keep aws bucket analytics objects | `number` | `30` | no |
97+
| lifecycle_aws_bucket_inventory_expiration | Number of days unused items expire from AWS Inventory | `number` | `14` | no |
9698
| logging_bucket | The S3 bucket to send S3 access logs. | `string` | `""` | no |
97-
| noncurrent_version_expiration | Number of days until non-current version of object expires | `number` | `365` | no |
98-
| noncurrent_version_transitions | Non-current version transition blocks | `list(any)` | ```[ { "days": 30, "storage_class": "STANDARD_IA" } ]``` | no |
9999
| object_ownership | Object ownership. Valid values: BucketOwnerEnforced, BucketOwnerPreferred or ObjectWriter. | `string` | `"BucketOwnerEnforced"` | no |
100100
| s3_bucket_acl | Set bucket ACL per [AWS S3 Canned ACL](<https://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html#canned-acl>) list. | `string` | `null` | no |
101101
| schedule_frequency | The S3 bucket inventory frequency. Defaults to Weekly. Options are 'Weekly' or 'Daily'. | `string` | `"Weekly"` | no |
102102
| tags | A mapping of tags to assign to the bucket. | `map(string)` | `{}` | no |
103103
| transfer_acceleration | Whether or not to enable bucket acceleration. | `bool` | `null` | no |
104-
| transitions | Current version transition blocks | `list(any)` | `[]` | no |
105-
| use_account_alias_prefix | Whether to prefix the bucket name with the AWS account alias. | `string` | `true` | no |
104+
| transition_default_minimum_object_size | Minimum object size to transition for lifecycle rule | `string` | `"all_storage_classes_128K"` | no |
105+
| use_account_alias_prefix | Whether to prefix the bucket name with the AWS account alias. | `bool` | `true` | no |
106106
| use_random_suffix | Whether to add a random suffix to the bucket name. | `bool` | `false` | no |
107107
| versioning_status | A string that indicates the versioning status for the log bucket. | `string` | `"Enabled"` | no |
108108

main.tf

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ locals {
99
bucket_prefix = var.use_account_alias_prefix ? format("%s-", data.aws_iam_account_alias.current[0].account_alias) : ""
1010
bucket_id = "${local.bucket_prefix}${var.bucket}"
1111
enable_bucket_logging = var.logging_bucket != ""
12+
13+
# Detect aspects of default lifecycle rules
14+
aiu_has_expiration = var.lifecycle_abort_incomplete_upload.expiration != null ? true : false
15+
aiu_has_transition = var.lifecycle_abort_incomplete_upload.transition != null ? true : false
16+
aiu_has_nvt = var.lifecycle_abort_incomplete_upload.nvt != null ? true : false
17+
aiu_has_nve = var.lifecycle_abort_incomplete_upload.nve != null ? true : false
18+
1219
}
1320

1421
data "aws_iam_policy_document" "supplemental_policy" {
@@ -152,7 +159,8 @@ resource "aws_s3_bucket_versioning" "private_bucket" {
152159
}
153160

154161
resource "aws_s3_bucket_lifecycle_configuration" "private_bucket" {
155-
bucket = aws_s3_bucket.private_bucket.id
162+
bucket = aws_s3_bucket.private_bucket.id
163+
transition_default_minimum_object_size = var.transition_default_minimum_object_size
156164

157165
rule {
158166
id = "abort-incomplete-multipart-upload"
@@ -164,61 +172,65 @@ resource "aws_s3_bucket_lifecycle_configuration" "private_bucket" {
164172
}
165173

166174
dynamic "expiration" {
167-
for_each = var.expiration
175+
for_each = local.aiu_has_expiration == true ? [var.lifecycle_abort_incomplete_upload.expiration] : []
168176
content {
169-
date = lookup(expiration.value, "date", null)
170-
days = lookup(expiration.value, "days", 0)
171-
172-
expired_object_delete_marker = lookup(expiration.value, "expired_object_delete_marker", false)
177+
date = expiration.value.date
178+
days = expiration.value.days
179+
expired_object_delete_marker = expiration.value.expired_object_delete_marker
173180
}
174181
}
175182

176183
dynamic "transition" {
177-
for_each = var.transitions
184+
for_each = local.aiu_has_transition == true ? [var.lifecycle_abort_incomplete_upload.transition] : []
178185
content {
179186
days = transition.value.days
180187
storage_class = transition.value.storage_class
181188
}
182189
}
183190

184191
dynamic "noncurrent_version_transition" {
185-
for_each = var.noncurrent_version_transitions
192+
for_each = local.aiu_has_nvt == true ? [var.lifecycle_abort_incomplete_upload.nvt] : []
186193
content {
187-
noncurrent_days = noncurrent_version_transition.value.days
188-
storage_class = noncurrent_version_transition.value.storage_class
194+
newer_noncurrent_versions = noncurrent_version_transition.value.newer_noncurrent_versions
195+
noncurrent_days = noncurrent_version_transition.value.noncurrent_days
196+
storage_class = noncurrent_version_transition.value.storage_class
189197
}
190198
}
191199

192-
noncurrent_version_expiration {
193-
noncurrent_days = var.noncurrent_version_expiration
200+
dynamic "noncurrent_version_expiration" {
201+
for_each = local.aiu_has_nve == true ? [var.lifecycle_abort_incomplete_upload.nve] : []
202+
content {
203+
newer_noncurrent_versions = noncurrent_version_expiration.value.newer_noncurrent_versions
204+
noncurrent_days = noncurrent_version_expiration.value.noncurrent_days
205+
}
194206
}
195207
}
196208

197209
rule {
198210
id = "aws-bucket-inventory"
199211

200-
status = "Enabled"
212+
status = var.enable_bucket_inventory ? "Enabled" : "Disabled"
201213

202214
filter {
203215
prefix = "_AWSBucketInventory/"
204216
}
205217

206218
expiration {
207-
days = 14
219+
days = var.lifecycle_aws_bucket_inventory_expiration
208220
}
209221
}
210222

211223
rule {
212224
id = "aws-bucket-analytics"
213225

214-
status = "Enabled"
226+
status = var.enable_analytics ? "Enabled" : "Disabled"
215227

216228
filter {
217229
prefix = "_AWSBucketAnalytics/"
218230
}
219231

220232
expiration {
221-
days = 30
233+
days = var.lifecycle_aws_bucket_analytics_expiration
222234
}
223235
}
224236

variables.tf

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,6 @@ variable "enable_s3_public_access_block" {
6363
default = true
6464
}
6565

66-
variable "expiration" {
67-
description = "expiration blocks"
68-
type = list(any)
69-
default = [
70-
{
71-
expired_object_delete_marker = true
72-
}
73-
]
74-
}
75-
7666
variable "inventory_bucket_format" {
7767
type = string
7868
default = "ORC"
@@ -85,27 +75,49 @@ variable "kms_master_key_id" {
8575
default = ""
8676
}
8777

88-
variable "logging_bucket" {
89-
description = "The S3 bucket to send S3 access logs."
90-
type = string
91-
default = ""
78+
variable "lifecycle_abort_incomplete_upload" {
79+
description = "Default values for the abort incomplete mutlipart uploads lifecycle rule"
80+
default = {
81+
expiration = {
82+
expired_object_delete_marker = true
83+
days = null
84+
date = null
85+
}
86+
# No transition block necessary by default
87+
transition = null
88+
89+
# noncurrent_version_transition (nvt) block attributes
90+
nvt = {
91+
newer_noncurrent_versions = null
92+
noncurrent_days = 30
93+
storage_class = "STANDARD_IA"
94+
}
95+
96+
# noncurrent_version_expiration (nve) block attributes
97+
# Number of days until non-current version of object expires
98+
nve = {
99+
newer_noncurrent_versions = null,
100+
noncurrent_days = 365
101+
}
102+
}
92103
}
93104

94-
variable "noncurrent_version_expiration" {
95-
description = "Number of days until non-current version of object expires"
105+
variable "lifecycle_aws_bucket_analytics_expiration" {
106+
description = "Number of days to keep aws bucket analytics objects"
96107
type = number
97-
default = 365
108+
default = 30
98109
}
99110

100-
variable "noncurrent_version_transitions" {
101-
description = "Non-current version transition blocks"
102-
type = list(any)
103-
default = [
104-
{
105-
days = 30
106-
storage_class = "STANDARD_IA"
107-
}
108-
]
111+
variable "lifecycle_aws_bucket_inventory_expiration" {
112+
description = "Number of days unused items expire from AWS Inventory"
113+
type = number
114+
default = 14
115+
}
116+
117+
variable "logging_bucket" {
118+
description = "The S3 bucket to send S3 access logs."
119+
type = string
120+
default = ""
109121
}
110122

111123
variable "object_ownership" {
@@ -138,10 +150,10 @@ variable "transfer_acceleration" {
138150
default = null
139151
}
140152

141-
variable "transitions" {
142-
description = "Current version transition blocks"
143-
type = list(any)
144-
default = []
153+
variable "transition_default_minimum_object_size" {
154+
description = "Minimum object size to transition for lifecycle rule"
155+
type = string
156+
default = "all_storage_classes_128K"
145157
}
146158

147159
variable "use_account_alias_prefix" {

versions.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ terraform {
22
required_version = ">= 1.0"
33

44
required_providers {
5-
aws = ">= 5.43.0"
5+
aws = ">= 5.89.0"
66
}
77
}

0 commit comments

Comments
 (0)