Skip to content

Commit 5cff76c

Browse files
committed
Divide aws_s3_bucket_lifecycle_configuration resource
1 parent 8b576ee commit 5cff76c

File tree

7 files changed

+215
-92
lines changed

7 files changed

+215
-92
lines changed

modules/s3-archive-bucket/README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ This module creates following resources.
44

55
- `aws_s3_bucket`
66
- `aws_s3_bucket_accelerate_configuration`
7+
- `aws_s3_bucket_lifecycle_configuration`
78
- `aws_s3_bucket_logging` (optional)
89
- `aws_s3_bucket_ownership_controls`
910
- `aws_s3_bucket_policy`
@@ -38,6 +39,7 @@ No modules.
3839
| [aws_s3_bucket.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket) | resource |
3940
| [aws_s3_bucket_accelerate_configuration.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_accelerate_configuration) | resource |
4041
| [aws_s3_bucket_acl.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_acl) | resource |
42+
| [aws_s3_bucket_lifecycle_configuration.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_lifecycle_configuration) | resource |
4143
| [aws_s3_bucket_logging.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_logging) | resource |
4244
| [aws_s3_bucket_ownership_controls.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_ownership_controls) | resource |
4345
| [aws_s3_bucket_policy.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_policy) | resource |
@@ -67,13 +69,13 @@ No modules.
6769
| <a name="input_delivery_elb_enabled"></a> [delivery\_elb\_enabled](#input\_delivery\_elb\_enabled) | (Optional) Allow ELB(Elastic Load Balancer) service to export logs to bucket. | `bool` | `false` | no |
6870
| <a name="input_delivery_elb_key_prefixes"></a> [delivery\_elb\_key\_prefixes](#input\_delivery\_elb\_key\_prefixes) | (Optional) List of the S3 key prefixes that follows the name of the bucket you have allowed for ELB(Elastic Load Balancer) log file delivery. | `list(string)` | `[]` | no |
6971
| <a name="input_force_destroy"></a> [force\_destroy](#input\_force\_destroy) | (Optional) A bool that indicates all objects (including any locked objects) should be deleted from the bucket so the bucket can be destroyed without error. | `bool` | `false` | no |
70-
| <a name="input_grants"></a> [grants](#input\_grants) | (Optional) A list of the ACL policy grant. Conflicts with acl. Valid values for `grant.type` are `CanonicalUser` and `Group`. `AmazonCustomerByEmail` is not supported. Valid values for `grant.permissions` are `READ`, `WRITE`, `READ_ACP`, `WRITE_ACP`, `FULL_CONTROL`. | `list(any)` | `[]` | no |
72+
| <a name="input_grants"></a> [grants](#input\_grants) | (Optional) A list of the ACL policy grant. Conflicts with acl. Valid values for `grant.type` are `CanonicalUser` and `Group`. `AmazonCustomerByEmail` is not supported. Valid values for `grant.permission` are `READ`, `WRITE`, `READ_ACP`, `WRITE_ACP`, `FULL_CONTROL`. | `list(any)` | `[]` | no |
7173
| <a name="input_lifecycle_rules"></a> [lifecycle\_rules](#input\_lifecycle\_rules) | (Optional) Use lifecycle rules to define actions you want Amazon S3 to take during an object's lifetime such as transitioning objects to another storage class, archiving them, or deleting them after a specified period of time. | `list(any)` | `[]` | no |
7274
| <a name="input_logging_enabled"></a> [logging\_enabled](#input\_logging\_enabled) | (Optional) Whether to enable S3 bucket logging for the access log. Defaults to `false`. | `bool` | `false` | no |
7375
| <a name="input_logging_s3_bucket"></a> [logging\_s3\_bucket](#input\_logging\_s3\_bucket) | (Optional) The name of the bucket that will receive the log objects. | `string` | `null` | no |
7476
| <a name="input_logging_s3_key_prefix"></a> [logging\_s3\_key\_prefix](#input\_logging\_s3\_key\_prefix) | (Optional) To specify a key prefix of log objects. | `string` | `null` | no |
7577
| <a name="input_module_tags_enabled"></a> [module\_tags\_enabled](#input\_module\_tags\_enabled) | (Optional) Whether to create AWS Resource Tags for the module informations. | `bool` | `true` | no |
76-
| <a name="input_object_ownership"></a> [object\_ownership](#input\_object\_ownership) | (Optional) Control ownership of objects written to this bucket from other AWS accounts and granted using access control lists (ACLs). Object ownership determines who can specify access to objects. Valid values: `BucketOwnerPreferred` or `ObjectWriter`. | `string` | `"BucketOwnerPreferred"` | no |
78+
| <a name="input_object_ownership"></a> [object\_ownership](#input\_object\_ownership) | (Optional) Control ownership of objects written to this bucket from other AWS accounts and granted using access control lists (ACLs). Object ownership determines who can specify access to objects. Valid values: `BucketOwnerPreferred`, `BucketOwnerEnforced` or `ObjectWriter`. | `string` | `"BucketOwnerPreferred"` | no |
7779
| <a name="input_public_access_enabled"></a> [public\_access\_enabled](#input\_public\_access\_enabled) | (Optional) Whether to enable S3 bucket-level Public Access Block configuration. Block the public access to S3 bucket if the value is `false`. | `bool` | `false` | no |
7880
| <a name="input_resource_group_description"></a> [resource\_group\_description](#input\_resource\_group\_description) | (Optional) The description of Resource Group. | `string` | `"Managed by Terraform."` | no |
7981
| <a name="input_resource_group_enabled"></a> [resource\_group\_enabled](#input\_resource\_group\_enabled) | (Optional) Whether to create Resource Group to find and group AWS resources which are created by this module. | `bool` | `true` | no |
@@ -93,6 +95,7 @@ No modules.
9395
| <a name="output_domain_name"></a> [domain\_name](#output\_domain\_name) | The bucket domain name. Will be of format `bucketname.s3.amazonaws.com`. |
9496
| <a name="output_hosted_zone_id"></a> [hosted\_zone\_id](#output\_hosted\_zone\_id) | The Route 53 Hosted Zone ID for this bucket's region. |
9597
| <a name="output_id"></a> [id](#output\_id) | The ID of the bucket. |
98+
| <a name="output_lifecycle_rules"></a> [lifecycle\_rules](#output\_lifecycle\_rules) | The lifecycle configuration for the bucket. |
9699
| <a name="output_logging"></a> [logging](#output\_logging) | The logging configuration for the bucket. |
97100
| <a name="output_name"></a> [name](#output\_name) | The name of the bucket. |
98101
| <a name="output_region"></a> [region](#output\_region) | The AWS region this bucket resides in. |

modules/s3-archive-bucket/acl.tf renamed to modules/s3-archive-bucket/access-control.tf

+19
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,25 @@ locals {
2525
}
2626

2727

28+
###################################################
29+
# Policy for S3 Bucket
30+
###################################################
31+
32+
data "aws_iam_policy_document" "this" {
33+
source_policy_documents = concat(
34+
var.tls_required ? [data.aws_iam_policy_document.tls_required.json] : [],
35+
var.delivery_cloudtrail_enabled ? [data.aws_iam_policy_document.cloudtrail.json] : [],
36+
var.delivery_config_enabled ? [data.aws_iam_policy_document.config.json] : [],
37+
var.delivery_elb_enabled ? [data.aws_iam_policy_document.elb.json] : [],
38+
)
39+
}
40+
41+
resource "aws_s3_bucket_policy" "this" {
42+
bucket = aws_s3_bucket.this.id
43+
policy = data.aws_iam_policy_document.this.json
44+
}
45+
46+
2847
###################################################
2948
# Object Ownership for S3 Bucket
3049
###################################################
+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
locals {
2+
versioning_mfa_status = {
3+
"ENABLED" = "Enabled"
4+
"DISABLED" = "Disabled"
5+
"SUSPENDED" = "Suspended"
6+
}
7+
8+
lifecycle_rules = {
9+
for rule in var.lifecycle_rules :
10+
rule.id => rule
11+
}
12+
}
13+
14+
15+
###################################################
16+
# Versioning for S3 Bucket
17+
###################################################
18+
19+
# TODO: `expected_bucket_owner`
20+
resource "aws_s3_bucket_versioning" "this" {
21+
bucket = aws_s3_bucket.this.bucket
22+
23+
mfa = try(var.versioning_mfa_deletion.device, null)
24+
25+
versioning_configuration {
26+
status = local.versioning_mfa_status[var.versioning_status]
27+
mfa_delete = try(var.versioning_mfa_deletion.enabled, false) ? "Enabled" : "Disabled"
28+
}
29+
}
30+
31+
32+
###################################################
33+
# Lifecycle Rules for S3 Bucket
34+
###################################################
35+
36+
# TODO: `expected_bucket_owner`
37+
resource "aws_s3_bucket_lifecycle_configuration" "this" {
38+
bucket = aws_s3_bucket.this.bucket
39+
40+
dynamic "rule" {
41+
for_each = var.lifecycle_rules
42+
43+
content {
44+
id = rule.value.id
45+
status = try(rule.value.enabled, true) ? "Enabled" : "Disabled"
46+
47+
dynamic "abort_incomplete_multipart_upload" {
48+
for_each = try([rule.value.days_to_abort_incomplete_multipart_upload], [])
49+
50+
content {
51+
days_after_initiation = abort_incomplete_multipart_upload.value
52+
}
53+
}
54+
55+
## Single Filter
56+
dynamic "filter" {
57+
for_each = sum([
58+
try(rule.value.prefix != null ? 1 : 0, 0),
59+
try(rule.value.min_object_size != null ? 1 : 0, 0),
60+
try(rule.value.max_object_size != null ? 1 : 0, 0),
61+
]) == 1 ? ["go"] : []
62+
63+
content {
64+
prefix = try(rule.value.prefix, null)
65+
66+
object_size_greater_than = try(rule.value.min_object_size, null)
67+
object_size_less_than = try(rule.value.max_object_size, null)
68+
}
69+
}
70+
71+
## Multi Filter
72+
dynamic "filter" {
73+
for_each = sum([
74+
try(rule.value.prefix != null ? 1 : 0, 0),
75+
try(rule.value.tags != null ? 2 : 0, 0),
76+
try(rule.value.min_object_size != null ? 1 : 0, 0),
77+
try(rule.value.max_object_size != null ? 1 : 0, 0),
78+
]) > 1 ? ["go"] : []
79+
80+
content {
81+
and {
82+
prefix = try(rule.value.prefix, null)
83+
tags = try(rule.value.tags, null)
84+
85+
object_size_greater_than = try(rule.value.min_object_size, null)
86+
object_size_less_than = try(rule.value.max_object_size, null)
87+
}
88+
}
89+
}
90+
91+
dynamic "expiration" {
92+
for_each = try([rule.value.expiration], [])
93+
94+
content {
95+
date = try(expiration.value.date, null)
96+
days = try(expiration.value.days, 0)
97+
98+
expired_object_delete_marker = try(expiration.value.expired_object_delete_marker, false)
99+
}
100+
}
101+
102+
dynamic "noncurrent_version_expiration" {
103+
for_each = try([rule.value.noncurrent_version_expiration], [])
104+
105+
content {
106+
noncurrent_days = try(noncurrent_version_expiration.value.days, null)
107+
newer_noncurrent_versions = try(noncurrent_version_expiration.value.count, null)
108+
}
109+
}
110+
111+
dynamic "transition" {
112+
for_each = try(rule.value.transitions, [])
113+
114+
content {
115+
date = try(transition.value.date, null)
116+
days = try(transition.value.days, null)
117+
118+
storage_class = transition.value.storage_class
119+
}
120+
}
121+
122+
dynamic "noncurrent_version_transition" {
123+
for_each = try(rule.value.noncurrent_version_transitions, [])
124+
125+
content {
126+
noncurrent_days = try(noncurrent_version_transition.value.days, null)
127+
newer_noncurrent_versions = try(noncurrent_version_transition.value.count, null)
128+
129+
storage_class = noncurrent_version_transition.value.storage_class
130+
}
131+
}
132+
}
133+
}
134+
135+
depends_on = [
136+
aws_s3_bucket_versioning.this,
137+
]
138+
}

modules/s3-archive-bucket/main.tf

+6-64
Original file line numberDiff line numberDiff line change
@@ -24,58 +24,19 @@ resource "aws_s3_bucket" "this" {
2424
bucket = var.name
2525
force_destroy = var.force_destroy
2626

27-
dynamic "lifecycle_rule" {
28-
for_each = var.lifecycle_rules
29-
30-
content {
31-
id = try(lifecycle_rule.value.id, null)
32-
enabled = try(lifecycle_rule.value.enabled, true)
33-
prefix = try(lifecycle_rule.value.prefix, null)
34-
tags = try(lifecycle_rule.value.tags, null)
35-
36-
abort_incomplete_multipart_upload_days = try(lifecycle_rule.value.abort_incomplete_multipart_upload_days, null)
37-
38-
expiration {
39-
date = try(lifecycle_rule.value.expiration.date, null)
40-
days = try(lifecycle_rule.value.expiration.days, 0)
41-
42-
expired_object_delete_marker = try(lifecycle_rule.value.expiration.expired_object_delete_marker, false)
43-
}
44-
45-
dynamic "transition" {
46-
for_each = try(lifecycle_rule.value.transitions, [])
47-
48-
content {
49-
date = try(transition.value.date, null)
50-
days = try(transition.value.days, null)
51-
52-
storage_class = transition.value.storage_class
53-
}
54-
}
55-
56-
noncurrent_version_expiration {
57-
days = try(lifecycle_rule.value.noncurrent_version_expiration.days, null)
58-
}
59-
60-
dynamic "noncurrent_version_transition" {
61-
for_each = try(lifecycle_rule.value.noncurrent_version_transitions, [])
62-
63-
content {
64-
days = try(noncurrent_version_transition.value.days, null)
65-
66-
storage_class = noncurrent_version_transition.value.storage_class
67-
}
68-
}
69-
}
70-
}
71-
7227
tags = merge(
7328
{
7429
"Name" = local.metadata.name
7530
},
7631
local.module_tags,
7732
var.tags,
7833
)
34+
35+
lifecycle {
36+
ignore_changes = [
37+
lifecycle_rule,
38+
]
39+
}
7940
}
8041

8142

@@ -102,22 +63,3 @@ resource "aws_s3_bucket_server_side_encryption_configuration" "this" {
10263
}
10364
}
10465
}
105-
106-
107-
###################################################
108-
# IAM Policy for S3 Bucket
109-
###################################################
110-
111-
data "aws_iam_policy_document" "this" {
112-
source_policy_documents = concat(
113-
var.tls_required ? [data.aws_iam_policy_document.tls_required.json] : [],
114-
var.delivery_cloudtrail_enabled ? [data.aws_iam_policy_document.cloudtrail.json] : [],
115-
var.delivery_config_enabled ? [data.aws_iam_policy_document.config.json] : [],
116-
var.delivery_elb_enabled ? [data.aws_iam_policy_document.elb.json] : [],
117-
)
118-
}
119-
120-
resource "aws_s3_bucket_policy" "this" {
121-
bucket = aws_s3_bucket.this.id
122-
policy = data.aws_iam_policy_document.this.json
123-
}

modules/s3-archive-bucket/outputs.tf

+18
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,24 @@ output "versioning" {
4747
}
4848
}
4949

50+
output "lifecycle_rules" {
51+
description = "The lifecycle configuration for the bucket."
52+
value = {
53+
for rule in aws_s3_bucket_lifecycle_configuration.this.rule :
54+
rule.id => {
55+
id = rule.id
56+
enabled = rule.status == "Enabled"
57+
58+
filter = {
59+
prefix = try(local.lifecycle_rules[rule.id].prefix, null)
60+
tags = try(local.lifecycle_rules[rule.id].tags, {})
61+
min_object_size = try(local.lifecycle_rules[rule.id].min_object_size, null)
62+
max_object_size = try(local.lifecycle_rules[rule.id].max_object_size, null)
63+
}
64+
}
65+
}
66+
}
67+
5068
output "server_side_encryption" {
5169
description = "The configuration for the S3 bucket server-side encryption."
5270
value = {

0 commit comments

Comments
 (0)