forked from cloudposse/terraform-aws-s3-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
294 lines (249 loc) · 8.53 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
locals {
enabled = module.this.enabled
bucket_arn = "arn:${data.aws_partition.current.partition}:s3:::${join("", aws_s3_bucket.default.*.id)}"
website_config = {
redirect_all = [
{
redirect_all_requests_to = var.redirect_all_requests_to
}
]
default = [
{
index_document = var.index_document
error_document = var.error_document
routing_rules = var.routing_rules
}
]
}
}
module "logs" {
source = "cloudposse/s3-log-storage/aws"
version = "0.20.0"
attributes = ["logs"]
enabled = local.enabled && var.logs_enabled
standard_transition_days = var.logs_standard_transition_days
glacier_transition_days = var.logs_glacier_transition_days
expiration_days = var.logs_expiration_days
force_destroy = var.force_destroy
context = module.this.context
}
module "default_label" {
source = "cloudposse/label/null"
version = "0.25.0"
attributes = ["origin"]
context = module.this.context
}
resource "aws_s3_bucket" "default" {
count = local.enabled ? 1 : 0
#bridgecrew:skip=BC_AWS_S3_1:The bucket used for a public static website. (https://docs.bridgecrew.io/docs/s3_1-acl-read-permissions-everyone)
#bridgecrew:skip=BC_AWS_S3_14:Skipping `Ensure all data stored in the S3 bucket is securely encrypted at rest` check until bridgecrew will support dynamic blocks (https://github.com/bridgecrewio/checkov/issues/776).
#bridgecrew:skip=CKV_AWS_52:Skipping `Ensure S3 bucket has MFA delete enabled` due to issue using `mfa_delete` by terraform (https://github.com/hashicorp/terraform-provider-aws/issues/629).
acl = "public-read"
bucket = var.hostname
tags = module.default_label.tags
force_destroy = var.force_destroy
dynamic "logging" {
for_each = var.logs_enabled ? ["true"] : []
content {
target_bucket = module.logs.bucket_id
target_prefix = module.logs.prefix
}
}
dynamic "website" {
for_each = local.website_config[var.redirect_all_requests_to == "" ? "default" : "redirect_all"]
content {
error_document = lookup(website.value, "error_document", null)
index_document = lookup(website.value, "index_document", null)
redirect_all_requests_to = lookup(website.value, "redirect_all_requests_to", null)
routing_rules = lookup(website.value, "routing_rules", null)
}
}
cors_rule {
allowed_headers = var.cors_allowed_headers
allowed_methods = var.cors_allowed_methods
allowed_origins = var.cors_allowed_origins
expose_headers = var.cors_expose_headers
max_age_seconds = var.cors_max_age_seconds
}
versioning {
enabled = var.versioning_enabled
}
lifecycle_rule {
id = module.default_label.id
enabled = var.lifecycle_rule_enabled
prefix = var.prefix
tags = module.default_label.tags
noncurrent_version_transition {
days = var.noncurrent_version_transition_days
storage_class = "GLACIER"
}
noncurrent_version_expiration {
days = var.noncurrent_version_expiration_days
}
}
dynamic "server_side_encryption_configuration" {
for_each = var.encryption_enabled ? ["true"] : []
content {
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
}
}
}
# AWS only supports a single bucket policy on a bucket. You can combine multiple Statements into a single policy, but not attach multiple policies.
# https://github.com/hashicorp/terraform/issues/10543
resource "aws_s3_bucket_policy" "default" {
count = local.enabled ? 1 : 0
bucket = aws_s3_bucket.default[0].id
policy = data.aws_iam_policy_document.default[0].json
}
data "aws_iam_policy_document" "default" {
count = local.enabled ? 1 : 0
statement {
actions = ["s3:GetObject"]
resources = ["${aws_s3_bucket.default[0].arn}/*"]
principals {
type = "AWS"
identifiers = ["*"]
}
}
dynamic "statement" {
for_each = var.allow_ssl_requests_only ? [1] : []
content {
sid = "AllowSSLRequestsOnly"
effect = "Deny"
actions = ["s3:*"]
resources = [local.bucket_arn, "${local.bucket_arn}/*"]
principals {
identifiers = ["*"]
type = "*"
}
condition {
test = "Bool"
values = ["false"]
variable = "aws:SecureTransport"
}
}
}
# Support replication ARNs
dynamic "statement" {
for_each = flatten(data.aws_iam_policy_document.replication.*.statement)
content {
actions = lookup(statement.value, "actions", null)
effect = lookup(statement.value, "effect", null)
not_actions = lookup(statement.value, "not_actions", null)
not_resources = lookup(statement.value, "not_resources", null)
resources = lookup(statement.value, "resources", null)
sid = lookup(statement.value, "sid", null)
dynamic "condition" {
for_each = lookup(statement.value, "condition", [])
content {
test = condition.value.test
values = condition.value.values
variable = condition.value.variable
}
}
dynamic "not_principals" {
for_each = lookup(statement.value, "not_principals", [])
content {
identifiers = not_principals.value.identifiers
type = not_principals.value.type
}
}
dynamic "principals" {
for_each = lookup(statement.value, "principals", [])
content {
identifiers = principals.value.identifiers
type = principals.value.type
}
}
}
}
# Support deployment ARNs
dynamic "statement" {
for_each = flatten(data.aws_iam_policy_document.deployment.*.statement)
content {
actions = lookup(statement.value, "actions", null)
effect = lookup(statement.value, "effect", null)
not_actions = lookup(statement.value, "not_actions", null)
not_resources = lookup(statement.value, "not_resources", null)
resources = lookup(statement.value, "resources", null)
sid = lookup(statement.value, "sid", null)
dynamic "condition" {
for_each = lookup(statement.value, "condition", [])
content {
test = condition.value.test
values = condition.value.values
variable = condition.value.variable
}
}
dynamic "not_principals" {
for_each = lookup(statement.value, "not_principals", [])
content {
identifiers = not_principals.value.identifiers
type = not_principals.value.type
}
}
dynamic "principals" {
for_each = lookup(statement.value, "principals", [])
content {
identifiers = principals.value.identifiers
type = principals.value.type
}
}
}
}
}
data "aws_iam_policy_document" "replication" {
count = local.enabled ? signum(length(var.replication_source_principal_arns)) : 0
statement {
principals {
type = "AWS"
identifiers = var.replication_source_principal_arns
}
actions = [
"s3:GetBucketVersioning",
"s3:PutBucketVersioning",
"s3:ReplicateObject",
"s3:ReplicateDelete"
]
resources = [
aws_s3_bucket.default[0].arn,
"${aws_s3_bucket.default[0].arn}/*"
]
}
}
data "aws_iam_policy_document" "deployment" {
count = local.enabled ? length(keys(var.deployment_arns)) : 0
statement {
actions = var.deployment_actions
resources = flatten([
formatlist(
"${aws_s3_bucket.default[0].arn}%s",
var.deployment_arns[keys(var.deployment_arns)[count.index]]
),
formatlist(
"${aws_s3_bucket.default[0].arn}%s/*",
var.deployment_arns[keys(var.deployment_arns)[count.index]]
)
])
principals {
type = "AWS"
identifiers = [keys(var.deployment_arns)[count.index]]
}
}
}
data "aws_partition" "current" {}
module "dns" {
source = "cloudposse/route53-alias/aws"
version = "0.13.0"
enabled = local.enabled
aliases = compact([signum(length(var.parent_zone_id)) == 1 || signum(length(var.parent_zone_name)) == 1 ? var.hostname : ""])
parent_zone_id = var.parent_zone_id
parent_zone_name = var.parent_zone_name
target_dns_name = join("", aws_s3_bucket.default.*.website_domain)
target_zone_id = join("", aws_s3_bucket.default.*.hosted_zone_id)
context = module.this.context
}