forked from terraform-aws-modules/terraform-aws-s3-bucket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
325 lines (254 loc) · 10.9 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
locals {
attach_policy = var.attach_elb_log_delivery_policy || var.attach_deny_insecure_transport_policy || var.attach_policy
}
resource "aws_s3_bucket" "this" {
count = var.create_bucket ? 1 : 0
bucket = var.bucket
bucket_prefix = var.bucket_prefix
# hack when `null` value can't be used (eg, from terragrunt, https://github.com/gruntwork-io/terragrunt/pull/1367)
acl = var.acl != "null" ? var.acl : null
tags = var.tags
force_destroy = var.force_destroy
acceleration_status = var.acceleration_status
request_payer = var.request_payer
dynamic "website" {
for_each = length(keys(var.website)) == 0 ? [] : [var.website]
content {
index_document = lookup(website.value, "index_document", null)
error_document = lookup(website.value, "error_document", null)
redirect_all_requests_to = lookup(website.value, "redirect_all_requests_to", null)
routing_rules = lookup(website.value, "routing_rules", null)
}
}
dynamic "cors_rule" {
for_each = try(jsondecode(var.cors_rule), var.cors_rule)
content {
allowed_methods = cors_rule.value.allowed_methods
allowed_origins = cors_rule.value.allowed_origins
allowed_headers = lookup(cors_rule.value, "allowed_headers", null)
expose_headers = lookup(cors_rule.value, "expose_headers", null)
max_age_seconds = lookup(cors_rule.value, "max_age_seconds", null)
}
}
dynamic "versioning" {
for_each = length(keys(var.versioning)) == 0 ? [] : [var.versioning]
content {
enabled = lookup(versioning.value, "enabled", null)
mfa_delete = lookup(versioning.value, "mfa_delete", null)
}
}
dynamic "logging" {
for_each = length(keys(var.logging)) == 0 ? [] : [var.logging]
content {
target_bucket = logging.value.target_bucket
target_prefix = lookup(logging.value, "target_prefix", null)
}
}
dynamic "grant" {
for_each = try(jsondecode(var.grant), var.grant)
content {
id = lookup(grant.value, "id", null)
type = grant.value.type
permissions = grant.value.permissions
uri = lookup(grant.value, "uri", null)
}
}
dynamic "lifecycle_rule" {
for_each = try(jsondecode(var.lifecycle_rule), var.lifecycle_rule)
content {
id = lookup(lifecycle_rule.value, "id", null)
prefix = lookup(lifecycle_rule.value, "prefix", null)
tags = lookup(lifecycle_rule.value, "tags", null)
abort_incomplete_multipart_upload_days = lookup(lifecycle_rule.value, "abort_incomplete_multipart_upload_days", null)
enabled = lifecycle_rule.value.enabled
# Max 1 block - expiration
dynamic "expiration" {
for_each = length(keys(lookup(lifecycle_rule.value, "expiration", {}))) == 0 ? [] : [lookup(lifecycle_rule.value, "expiration", {})]
content {
date = lookup(expiration.value, "date", null)
days = lookup(expiration.value, "days", null)
expired_object_delete_marker = lookup(expiration.value, "expired_object_delete_marker", null)
}
}
# Several blocks - transition
dynamic "transition" {
for_each = lookup(lifecycle_rule.value, "transition", [])
content {
date = lookup(transition.value, "date", null)
days = lookup(transition.value, "days", null)
storage_class = transition.value.storage_class
}
}
# Max 1 block - noncurrent_version_expiration
dynamic "noncurrent_version_expiration" {
for_each = length(keys(lookup(lifecycle_rule.value, "noncurrent_version_expiration", {}))) == 0 ? [] : [lookup(lifecycle_rule.value, "noncurrent_version_expiration", {})]
content {
days = lookup(noncurrent_version_expiration.value, "days", null)
}
}
# Several blocks - noncurrent_version_transition
dynamic "noncurrent_version_transition" {
for_each = lookup(lifecycle_rule.value, "noncurrent_version_transition", [])
content {
days = lookup(noncurrent_version_transition.value, "days", null)
storage_class = noncurrent_version_transition.value.storage_class
}
}
}
}
# Max 1 block - replication_configuration
dynamic "replication_configuration" {
for_each = length(keys(var.replication_configuration)) == 0 ? [] : [var.replication_configuration]
content {
role = replication_configuration.value.role
dynamic "rules" {
for_each = replication_configuration.value.rules
content {
id = lookup(rules.value, "id", null)
priority = lookup(rules.value, "priority", null)
prefix = lookup(rules.value, "prefix", null)
status = rules.value.status
dynamic "destination" {
for_each = length(keys(lookup(rules.value, "destination", {}))) == 0 ? [] : [lookup(rules.value, "destination", {})]
content {
bucket = destination.value.bucket
storage_class = lookup(destination.value, "storage_class", null)
replica_kms_key_id = lookup(destination.value, "replica_kms_key_id", null)
account_id = lookup(destination.value, "account_id", null)
dynamic "access_control_translation" {
for_each = length(keys(lookup(destination.value, "access_control_translation", {}))) == 0 ? [] : [lookup(destination.value, "access_control_translation", {})]
content {
owner = access_control_translation.value.owner
}
}
}
}
dynamic "source_selection_criteria" {
for_each = length(keys(lookup(rules.value, "source_selection_criteria", {}))) == 0 ? [] : [lookup(rules.value, "source_selection_criteria", {})]
content {
dynamic "sse_kms_encrypted_objects" {
for_each = length(keys(lookup(source_selection_criteria.value, "sse_kms_encrypted_objects", {}))) == 0 ? [] : [lookup(source_selection_criteria.value, "sse_kms_encrypted_objects", {})]
content {
enabled = sse_kms_encrypted_objects.value.enabled
}
}
}
}
dynamic "filter" {
for_each = length(keys(lookup(rules.value, "filter", {}))) == 0 ? [] : [lookup(rules.value, "filter", {})]
content {
prefix = lookup(filter.value, "prefix", null)
tags = lookup(filter.value, "tags", null)
}
}
}
}
}
}
# Max 1 block - server_side_encryption_configuration
dynamic "server_side_encryption_configuration" {
for_each = length(keys(var.server_side_encryption_configuration)) == 0 ? [] : [var.server_side_encryption_configuration]
content {
dynamic "rule" {
for_each = length(keys(lookup(server_side_encryption_configuration.value, "rule", {}))) == 0 ? [] : [lookup(server_side_encryption_configuration.value, "rule", {})]
content {
bucket_key_enabled = lookup(rule.value, "bucket_key_enabled", null)
dynamic "apply_server_side_encryption_by_default" {
for_each = length(keys(lookup(rule.value, "apply_server_side_encryption_by_default", {}))) == 0 ? [] : [
lookup(rule.value, "apply_server_side_encryption_by_default", {})]
content {
sse_algorithm = apply_server_side_encryption_by_default.value.sse_algorithm
kms_master_key_id = lookup(apply_server_side_encryption_by_default.value, "kms_master_key_id", null)
}
}
}
}
}
}
# Max 1 block - object_lock_configuration
dynamic "object_lock_configuration" {
for_each = length(keys(var.object_lock_configuration)) == 0 ? [] : [var.object_lock_configuration]
content {
object_lock_enabled = object_lock_configuration.value.object_lock_enabled
dynamic "rule" {
for_each = length(keys(lookup(object_lock_configuration.value, "rule", {}))) == 0 ? [] : [lookup(object_lock_configuration.value, "rule", {})]
content {
default_retention {
mode = lookup(lookup(rule.value, "default_retention", {}), "mode")
days = lookup(lookup(rule.value, "default_retention", {}), "days", null)
years = lookup(lookup(rule.value, "default_retention", {}), "years", null)
}
}
}
}
}
}
resource "aws_s3_bucket_policy" "this" {
count = var.create_bucket && local.attach_policy ? 1 : 0
bucket = aws_s3_bucket.this[0].id
policy = data.aws_iam_policy_document.combined[0].json
}
data "aws_iam_policy_document" "combined" {
count = var.create_bucket && local.attach_policy ? 1 : 0
source_policy_documents = compact([
var.attach_elb_log_delivery_policy ? data.aws_iam_policy_document.elb_log_delivery[0].json : "",
var.attach_deny_insecure_transport_policy ? data.aws_iam_policy_document.deny_insecure_transport[0].json : "",
var.attach_policy ? var.policy : ""
])
}
# AWS Load Balancer access log delivery policy
data "aws_elb_service_account" "this" {
count = var.create_bucket && var.attach_elb_log_delivery_policy ? 1 : 0
}
data "aws_iam_policy_document" "elb_log_delivery" {
count = var.create_bucket && var.attach_elb_log_delivery_policy ? 1 : 0
statement {
sid = ""
principals {
type = "AWS"
identifiers = data.aws_elb_service_account.this.*.arn
}
effect = "Allow"
actions = [
"s3:PutObject",
]
resources = [
"${aws_s3_bucket.this[0].arn}/*",
]
}
}
data "aws_iam_policy_document" "deny_insecure_transport" {
count = var.create_bucket && var.attach_deny_insecure_transport_policy ? 1 : 0
statement {
sid = "denyInsecureTransport"
effect = "Deny"
actions = [
"s3:*",
]
resources = [
aws_s3_bucket.this[0].arn,
"${aws_s3_bucket.this[0].arn}/*",
]
principals {
type = "*"
identifiers = ["*"]
}
condition {
test = "Bool"
variable = "aws:SecureTransport"
values = [
"false"
]
}
}
}
resource "aws_s3_bucket_public_access_block" "this" {
count = var.create_bucket && var.attach_public_policy ? 1 : 0
# Chain resources (s3_bucket -> s3_bucket_policy -> s3_bucket_public_access_block)
# to prevent "A conflicting conditional operation is currently in progress against this resource."
bucket = local.attach_policy ? aws_s3_bucket_policy.this[0].id : aws_s3_bucket.this[0].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
}