-
Notifications
You must be signed in to change notification settings - Fork 41
/
main.tf
348 lines (273 loc) · 9.48 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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
data "aws_iam_account_alias" "current" {
count = var.use_account_alias_prefix ? 1 : 0
}
data "aws_partition" "current" {}
data "aws_caller_identity" "current" {}
locals {
bucket_prefix = var.use_account_alias_prefix ? format("%s-", data.aws_iam_account_alias.current[0].account_alias) : ""
bucket_id = "${local.bucket_prefix}${var.bucket}"
enable_bucket_logging = var.logging_bucket != ""
}
data "aws_iam_policy_document" "supplemental_policy" {
# This should be a single line:
# source_policy_documents = [var.custom_bucket_policy]
#
# However, there appears to be a bug that occurs when source_policy_documents is an empty string:
# - https://github.com/hashicorp/terraform-provider-aws/issues/22959
# - https://github.com/hashicorp/terraform-provider-aws/issues/24366
#
# To work around this, we're using this workaround. It should be replaced
# once the underlying issue is addressed.
source_policy_documents = length(var.custom_bucket_policy) > 0 ? [var.custom_bucket_policy] : null
# Enforce SSL/TLS on all transmitted objects
# We do this by extending the custom_bucket_policy
statement {
sid = "enforce-tls-requests-only"
effect = "Deny"
principals {
type = "AWS"
identifiers = ["*"]
}
actions = ["s3:*"]
resources = [
"arn:${data.aws_partition.current.partition}:s3:::${local.bucket_id}/*"
]
condition {
test = "Bool"
variable = "aws:SecureTransport"
values = ["false"]
}
}
statement {
sid = "inventory-and-analytics"
effect = "Allow"
principals {
type = "Service"
identifiers = ["s3.amazonaws.com"]
}
actions = ["s3:PutObject"]
resources = [
"arn:${data.aws_partition.current.partition}:s3:::${local.bucket_id}/*"
]
condition {
test = "ArnLike"
variable = "aws:SourceArn"
values = ["arn:${data.aws_partition.current.partition}:s3:::${local.bucket_id}"]
}
condition {
test = "StringEquals"
variable = "aws:SourceAccount"
values = [data.aws_caller_identity.current.account_id]
}
condition {
test = "StringEquals"
variable = "s3:x-amz-acl"
values = ["bucket-owner-full-control"]
}
}
}
resource "aws_s3_bucket" "private_bucket" {
bucket = var.use_random_suffix ? null : local.bucket_id
bucket_prefix = var.use_random_suffix ? local.bucket_id : null
tags = var.tags
force_destroy = var.enable_bucket_force_destroy
lifecycle {
# These lifecycle ignore_changes rules exist to permit a smooth upgrade
# path from version 3.x of the AWS provider to version 4.x
ignore_changes = [
# While no special usage instructions are documented for needing this
# ignore_changes rule, this should avoid drift detection if conflicts
# with the aws_s3_bucket_versioning exist.
versioning,
# https://registry.terraform.io/providers/hashicorp%20%20/aws/3.75.2/docs/resources/s3_bucket_acl#usage-notes
grant,
# https://registry.terraform.io/providers/hashicorp%20%20/aws/3.75.2/docs/resources/s3_bucket_cors_configuration#usage-notes
cors_rule,
# https://registry.terraform.io/providers/hashicorp%20%20/aws/3.75.2/docs/resources/s3_bucket_lifecycle_configuration#usage-notes
lifecycle_rule,
# https://registry.terraform.io/providers/hashicorp%20%20/aws/3.75.2/docs/resources/s3_bucket_logging#usage-notes
logging,
# https://registry.terraform.io/providers/hashicorp%20%20/aws/3.75.2/docs/resources/s3_bucket_server_side_encryption_configuration#usage-notes
server_side_encryption_configuration,
]
}
}
resource "aws_s3_bucket_policy" "private_bucket" {
bucket = aws_s3_bucket.private_bucket.id
policy = data.aws_iam_policy_document.supplemental_policy.json
}
resource "aws_s3_bucket_accelerate_configuration" "private_bucket" {
count = var.transfer_acceleration != null ? 1 : 0
bucket = aws_s3_bucket.private_bucket.id
status = var.transfer_acceleration ? "Enabled" : "Suspended"
}
resource "aws_s3_bucket_acl" "private_bucket" {
count = var.s3_bucket_acl != null ? 1 : 0
bucket = aws_s3_bucket.private_bucket.id
acl = var.s3_bucket_acl
depends_on = [aws_s3_bucket_ownership_controls.private_bucket]
}
resource "aws_s3_bucket_ownership_controls" "private_bucket" {
count = var.control_object_ownership ? 1 : 0
bucket = aws_s3_bucket.private_bucket.id
rule {
object_ownership = var.object_ownership
}
depends_on = [
aws_s3_bucket_policy.private_bucket,
aws_s3_bucket_public_access_block.public_access_block,
aws_s3_bucket.private_bucket
]
}
resource "aws_s3_bucket_versioning" "private_bucket" {
bucket = aws_s3_bucket.private_bucket.id
versioning_configuration {
status = var.versioning_status
}
}
resource "aws_s3_bucket_lifecycle_configuration" "private_bucket" {
bucket = aws_s3_bucket.private_bucket.id
rule {
id = "abort-incomplete-multipart-upload"
status = "Enabled"
abort_incomplete_multipart_upload {
days_after_initiation = var.abort_incomplete_multipart_upload_days
}
dynamic "expiration" {
for_each = var.expiration
content {
date = lookup(expiration.value, "date", null)
days = lookup(expiration.value, "days", 0)
expired_object_delete_marker = lookup(expiration.value, "expired_object_delete_marker", false)
}
}
dynamic "transition" {
for_each = var.transitions
content {
days = transition.value.days
storage_class = transition.value.storage_class
}
}
dynamic "noncurrent_version_transition" {
for_each = var.noncurrent_version_transitions
content {
noncurrent_days = noncurrent_version_transition.value.days
storage_class = noncurrent_version_transition.value.storage_class
}
}
noncurrent_version_expiration {
noncurrent_days = var.noncurrent_version_expiration
}
}
rule {
id = "aws-bucket-inventory"
status = "Enabled"
filter {
prefix = "_AWSBucketInventory/"
}
expiration {
days = 14
}
}
rule {
id = "aws-bucket-analytics"
status = "Enabled"
filter {
prefix = "_AWSBucketAnalytics/"
}
expiration {
days = 30
}
}
dynamic "rule" {
for_each = var.additional_lifecycle_rules
content {
id = rule.value["id"]
status = rule.value["status"]
dynamic "filter" {
for_each = rule.value.filter
content {
prefix = filter.value["prefix"]
}
}
dynamic "expiration" {
for_each = rule.value.expiration
content {
days = expiration.value["days"]
}
}
}
}
}
resource "aws_s3_bucket_logging" "private_bucket" {
count = local.enable_bucket_logging ? 1 : 0
bucket = aws_s3_bucket.private_bucket.id
target_bucket = var.logging_bucket
target_prefix = "s3/${local.bucket_id}/"
}
resource "aws_s3_bucket_server_side_encryption_configuration" "private_bucket" {
bucket = aws_s3_bucket.private_bucket.id
rule {
apply_server_side_encryption_by_default {
sse_algorithm = length(var.kms_master_key_id) > 0 ? "aws:kms" : "AES256"
kms_master_key_id = length(var.kms_master_key_id) > 0 ? var.kms_master_key_id : null
}
bucket_key_enabled = var.bucket_key_enabled
}
}
resource "aws_s3_bucket_cors_configuration" "private_bucket" {
count = length(var.cors_rules)
bucket = aws_s3_bucket.private_bucket.bucket
cors_rule {
allowed_methods = var.cors_rules[count.index].allowed_methods
allowed_origins = var.cors_rules[count.index].allowed_origins
allowed_headers = lookup(var.cors_rules[count.index], "allowed_headers", null)
expose_headers = lookup(var.cors_rules[count.index], "expose_headers", null)
max_age_seconds = lookup(var.cors_rules[count.index], "max_age_seconds", null)
}
}
resource "aws_s3_bucket_analytics_configuration" "private_analytics_config" {
count = var.enable_analytics ? 1 : 0
bucket = aws_s3_bucket.private_bucket.bucket
name = "Analytics"
storage_class_analysis {
data_export {
destination {
s3_bucket_destination {
bucket_arn = aws_s3_bucket.private_bucket.arn
prefix = "_AWSBucketAnalytics"
}
}
}
}
}
resource "aws_s3_bucket_public_access_block" "public_access_block" {
count = var.enable_s3_public_access_block ? 1 : 0
bucket = aws_s3_bucket.private_bucket.id
# Block new public ACLs and uploading public objects
block_public_acls = true
# Retroactively remove public access granted through public ACLs
ignore_public_acls = true
# Block new public bucket policies
block_public_policy = true
# Retroactivley block public and cross-account access if bucket has public policies
restrict_public_buckets = true
}
resource "aws_s3_bucket_inventory" "inventory" {
count = var.enable_bucket_inventory ? 1 : 0
bucket = aws_s3_bucket.private_bucket.id
name = "BucketInventory"
included_object_versions = "All"
schedule {
frequency = var.schedule_frequency
}
destination {
bucket {
format = var.inventory_bucket_format
bucket_arn = aws_s3_bucket.private_bucket.arn
prefix = "_AWSBucketInventory/"
}
}
optional_fields = ["Size", "LastModifiedDate", "StorageClass", "ETag", "IsMultipartUploaded", "ReplicationStatus", "EncryptionStatus",
"ObjectLockRetainUntilDate", "ObjectLockMode", "ObjectLockLegalHoldStatus", "IntelligentTieringAccessTier"]
}