-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
211 lines (164 loc) · 7.01 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
resource "aws_s3_bucket" "this" {
bucket = var.bucket
force_destroy = var.force_destroy
}
resource "aws_s3_bucket_versioning" "this" {
bucket = aws_s3_bucket.this.id
mfa = try(var.versioning["mfa"], null)
versioning_configuration {
# Valid values: "Enabled" or "Suspended"
status = try(var.versioning["enabled"] ? "Enabled" : "Suspended", tobool(var.versioning["status"]) ? "Enabled" : "Suspended", title(lower(var.versioning["status"])), "Enabled")
# Valid values: "Enabled" or "Disabled"
mfa_delete = try(tobool(var.versioning["mfa_delete"]) ? "Enabled" : "Disabled", title(lower(var.versioning["mfa_delete"])), null)
}
}
resource "aws_s3_bucket_cors_configuration" "this" {
count = length(var.cors_rule) > 0 ? 1 : 0
bucket = aws_s3_bucket.this.id
dynamic "cors_rule" {
for_each = var.cors_rule
content {
id = try(cors_rule.value.id, null)
allowed_methods = cors_rule.value.allowed_methods
allowed_origins = cors_rule.value.allowed_origins
allowed_headers = try(cors_rule.value.allowed_headers, null)
expose_headers = try(cors_rule.value.expose_headers, null)
max_age_seconds = try(cors_rule.value.max_age_seconds, null)
}
}
}
resource "aws_s3_bucket_lifecycle_configuration" "this" {
count = length(var.lifecycle_rule) > 0 ? 1 : 0
bucket = aws_s3_bucket.this.id
dynamic "rule" {
for_each = var.lifecycle_rule
content {
id = try(rule.value.id, null)
status = try(rule.value.enabled ? "Enabled" : "Disabled", tobool(rule.value.status) ? "Enabled" : "Disabled", title(lower(rule.value.status)))
# Max 1 block - abort_incomplete_multipart_upload
dynamic "abort_incomplete_multipart_upload" {
for_each = try([rule.value.abort_incomplete_multipart_upload_days], [])
content {
days_after_initiation = try(rule.value.abort_incomplete_multipart_upload_days, null)
}
}
# Max 1 block - expiration
dynamic "expiration" {
for_each = try(flatten([rule.value.expiration]), [])
content {
date = try(expiration.value.date, null)
days = try(expiration.value.days, null)
expired_object_delete_marker = try(expiration.value.expired_object_delete_marker, null)
}
}
# Several blocks - transition
dynamic "transition" {
for_each = try(flatten([rule.value.transition]), [])
content {
date = try(transition.value.date, null)
days = try(transition.value.days, null)
storage_class = transition.value.storage_class
}
}
# Max 1 block - noncurrent_version_expiration
dynamic "noncurrent_version_expiration" {
for_each = try(flatten([rule.value.noncurrent_version_expiration]), [])
content {
newer_noncurrent_versions = try(noncurrent_version_expiration.value.newer_noncurrent_versions, null)
noncurrent_days = try(noncurrent_version_expiration.value.days, noncurrent_version_expiration.value.noncurrent_days, null)
}
}
# Several blocks - noncurrent_version_transition
dynamic "noncurrent_version_transition" {
for_each = try(flatten([rule.value.noncurrent_version_transition]), [])
content {
newer_noncurrent_versions = try(noncurrent_version_transition.value.newer_noncurrent_versions, null)
noncurrent_days = try(noncurrent_version_transition.value.days, noncurrent_version_transition.value.noncurrent_days, null)
storage_class = noncurrent_version_transition.value.storage_class
}
}
# Max 1 block - filter - without any key arguments or tags
dynamic "filter" {
for_each = length(try(flatten([rule.value.filter]), [])) == 0 ? [true] : []
content {
# prefix = ""
}
}
# Max 1 block - filter - with one key argument or a single tag
dynamic "filter" {
for_each = [for v in try(flatten([rule.value.filter]), []) : v if max(length(keys(v)), length(try(rule.value.filter.tags, rule.value.filter.tag, []))) == 1]
content {
object_size_greater_than = try(filter.value.object_size_greater_than, null)
object_size_less_than = try(filter.value.object_size_less_than, null)
prefix = try(filter.value.prefix, null)
dynamic "tag" {
for_each = try(filter.value.tags, filter.value.tag, [])
content {
key = tag.key
value = tag.value
}
}
}
}
# Max 1 block - filter - with more than one key arguments or multiple tags
dynamic "filter" {
for_each = [for v in try(flatten([rule.value.filter]), []) : v if max(length(keys(v)), length(try(rule.value.filter.tags, rule.value.filter.tag, []))) > 1]
content {
and {
object_size_greater_than = try(filter.value.object_size_greater_than, null)
object_size_less_than = try(filter.value.object_size_less_than, null)
prefix = try(filter.value.prefix, null)
tags = try(filter.value.tags, filter.value.tag, null)
}
}
}
}
}
# Must have bucket versioning enabled first
depends_on = [aws_s3_bucket_versioning.this]
}
data "aws_iam_policy_document" "combined" {
count = var.attach_policy ? 1 : 0
source_policy_documents = compact([
var.attach_policy ? var.policy : ""
])
}
resource "aws_s3_bucket_policy" "this" {
# Chain resources (s3_bucket -> s3_bucket_public_access_block -> s3_bucket_policy )
# to prevent "A conflicting conditional operation is currently in progress against this resource."
# Ref: https://github.com/hashicorp/terraform-provider-aws/issues/7628
bucket = aws_s3_bucket.this.id
policy = data.aws_iam_policy_document.combined[0].json
depends_on = [
aws_s3_bucket_public_access_block.this
]
}
resource "aws_s3_bucket_public_access_block" "this" {
count = var.attach_public_policy ? 1 : 0
bucket = aws_s3_bucket.this.id
block_public_acls = var.block_public_acls
block_public_policy = var.block_public_policy
ignore_public_acls = var.ignore_public_acls
restrict_public_buckets = var.restrict_public_buckets
}
resource "aws_s3_bucket_intelligent_tiering_configuration" "this" {
for_each = { for k, v in var.intelligent_tiering : k => v }
name = each.key
bucket = aws_s3_bucket.this.id
status = try(tobool(each.value.status) ? "Enabled" : "Disabled", title(lower(each.value.status)), null)
# Max 1 block - filter
dynamic "filter" {
for_each = length(try(flatten([each.value.filter]), [])) == 0 ? [] : [true]
content {
prefix = try(each.value.filter.prefix, null)
tags = try(each.value.filter.tags, null)
}
}
dynamic "tiering" {
for_each = each.value.tiering
content {
access_tier = tiering.key
days = tiering.value.days
}
}
}