-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy paths3.tf
207 lines (172 loc) · 5.12 KB
/
s3.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
locals {
localBucketName = var.bucket_name == null ? var.fqdn : var.bucket_name
localRoutingRules = try(var.routing_rule, null) == null ? [] : [
{
condition = {
http_error_code_returned_equals = try(var.routing_rule.condition.http_error_code_returned_equals, null)
key_prefix_equals = try(var.routing_rule.condition.key_prefix_equals, null)
}
redirect = {
host_name = try(var.routing_rule.redirect.host_name, null)
http_redirect_code = try(var.routing_rule.redirect.http_redirect_code, null)
protocol = try(var.routing_rule.redirect.protocol, null)
replace_key_prefix_with = try(var.routing_rule.redirect.replace_key_prefix_with, null)
replace_key_with = try(var.routing_rule.redirect.replace_key_with, null)
}
}
]
}
#locals {
# buckets = tomap({
# for key, bucket in var.buckets : key => {
# website = try(bucket.website, null) == null ? null : {
# index_document = tostring(try(bucket.website.index_document, null))
# error_document = tostring(try(bucket.website.error_document, null))
# }
# cors_rule = try(bucket.cors_rule, null) == null ? null : {
# allowed_headers = toset(try(bucket.cors_rule.allowed_headers, ["X-Custom-Header"]))
# allowed_methods = toset(try(bucket.cors_rule.allowed_methods, ["GET"]))
# allowed_origins = toset(try(bucket.cors_rule.allowed_origins, ["*"]))
# expose_headers = toset(try(bucket.cors_rule.expose_headers, null))
# max_age_seconds = tostring(try(bucket.cors_rule.max_age_seconds, null))
# }
# }
# })
#}
resource "aws_s3_bucket_accelerate_configuration" "main" {
count = var.acceleration_status != null ? 1 : 0
bucket = aws_s3_bucket.main.id
status = var.acceleration_status
}
resource "aws_s3_bucket_policy" "main" {
bucket = aws_s3_bucket.main.id
policy = data.aws_iam_policy_document.bucket_policy.json
}
resource "aws_s3_bucket_acl" "main" {
bucket = aws_s3_bucket.main.id
acl = "private"
}
resource "aws_s3_bucket_website_configuration" "main" {
bucket = aws_s3_bucket.main.id
# index_document = var.index_document
# error_document = var.error_document
# routing_rules = var.routing_rules
dynamic "routing_rule" {
for_each = local.localRoutingRules
content {
condition {
http_error_code_returned_equals = routing_rule.value.condition.http_error_code_returned_equals
key_prefix_equals = routing_rule.value.condition.key_prefix_equals
}
redirect {
host_name = routing_rule.value.redirect.host_name
http_redirect_code = routing_rule.value.redirect.http_redirect_code
protocol = routing_rule.value.redirect.protocol
replace_key_prefix_with = routing_rule.value.redirect.replace_key_prefix_with
replace_key_with = routing_rule.value.redirect.replace_key_with
}
}
}
# routing_rule {
# condition {
# http_error_code_returned_equals = null
# key_prefix_equals = null
# }
#
# redirect {
# host_name = null
# http_redirect_code = null
# protocol = null
# replace_key_prefix_with = null
# replace_key_with = null
# }
# }
# dynamic "setting" {
# for_each = var.settingscontent {
# namespace = setting.value["namespace"]
# name = setting.value["name"]
# value = setting.value["value"]
# }
# }
index_document {
suffix = var.index_document
}
error_document {
key = "error.html"
}
# routing_rule = var.routing_rules
# index_document {
# suffix = "index.html"
# }
#
# error_document {
# key = "error.html"
# }
}
resource "aws_s3_bucket" "main" {
provider = aws.main
bucket = local.localBucketName
# acl = "private"
# policy = data.aws_iam_policy_document.bucket_policy.json
# website {
# index_document = var.index_document
# error_document = var.error_document
# routing_rules = var.routing_rules
# }
force_destroy = var.force_destroy
# Transfer acceleration is not possible right now for hosted sites,
# as bucket names cannot contain a dot.
# https://docs.aws.amazon.com/AmazonS3/latest/userguide/BucketRestrictions.html
# acceleration_status = var.acceleration_status
tags = merge(
{
"Name" = local.localBucketName
},
var.tags
)
}
data "aws_iam_policy_document" "bucket_policy" {
provider = aws.main
statement {
sid = "AllowedIPReadAccess"
actions = [
"s3:GetObject",
]
resources = [
"arn:aws:s3:::${local.localBucketName}/*",
]
condition {
test = "IpAddress"
variable = "aws:SourceIp"
values = var.allowed_ips
}
principals {
type = "*"
identifiers = [
"*"
]
}
}
statement {
sid = "AllowCFOriginAccess"
actions = [
"s3:GetObject",
]
resources = [
"arn:aws:s3:::${local.localBucketName}/*",
]
condition {
test = "StringEquals"
variable = "aws:UserAgent"
values = [
var.refer_secret,
]
}
principals {
type = "*"
identifiers = [
"*"
]
}
}
}