-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup.tf
413 lines (345 loc) · 9.96 KB
/
setup.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
data "aws_caller_identity" "current" {}
module "log_storage" {
bucket = "logs.ideas.offby1.net"
s3_logs_path = "s3-log-"
cdn_logs_path = "cf-log-"
readers = [data.aws_caller_identity.current.account_id]
source = "github.com/offbyone/terraform-aws-s3-bucket-for-logs?ref=v0.7.3"
tags = local.tags
}
variable "website_domain" {
type = string
default = "offby1.website"
}
variable "dotnet_subdomain" {
type = string
default = "ideas.offby1.net"
}
variable "s3_origin_id" {
type = string
default = "S3-ideas.offby1.net"
}
data "aws_route53_zone" "zone" {
name = "offby1.net"
}
data "aws_route53_zone" "website" {
name = "offby1.website"
}
locals {
zone_id_map = {
"ideas.offby1.net" = data.aws_route53_zone.zone.zone_id
"offby1.website" = data.aws_route53_zone.website.zone_id
}
domain_names = [
var.dotnet_subdomain,
var.website_domain,
]
domain_name = var.website_domain
bucket_name = var.dotnet_subdomain
tags = {
Project = "ideas.blog"
Domain = "offby1.net"
Source = "https://github.com/offbyone/ideas"
}
}
resource "aws_s3_bucket" "blog" {
depends_on = [module.log_storage.s3_logs_bucket]
bucket = local.bucket_name
tags = local.tags
}
resource "aws_s3_bucket_acl" "blog" {
bucket = aws_s3_bucket.blog.id
}
resource "aws_s3_bucket_website_configuration" "blog" {
bucket = aws_s3_bucket.blog.id
index_document {
suffix = "index.html"
}
error_document {
key = "error.html"
}
}
resource "aws_s3_bucket_server_side_encryption_configuration" "blog" {
bucket = aws_s3_bucket.blog.id
rule {
bucket_key_enabled = false
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
}
resource "aws_s3_bucket_cors_configuration" "blog" {
bucket = aws_s3_bucket.blog.id
cors_rule {
allowed_headers = ["*"]
allowed_methods = ["GET", "HEAD", ]
allowed_origins = formatlist("https://%s", local.domain_names)
expose_headers = ["ETag"]
max_age_seconds = 0
}
}
resource "aws_s3_bucket_logging" "blog" {
bucket = aws_s3_bucket.blog.id
target_bucket = module.log_storage.s3_logs_bucket
target_prefix = module.log_storage.s3_logs_path
}
resource "aws_s3_bucket_public_access_block" "blog" {
bucket = aws_s3_bucket.blog.id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}
data "aws_iam_policy_document" "policy" {
statement {
sid = "AllowCloudFrontServicePrincipal"
principals {
type = "Service"
identifiers = ["cloudfront.amazonaws.com"]
}
actions = ["s3:GetObject"]
resources = ["${aws_s3_bucket.blog.arn}/*"]
condition {
test = "StringEquals"
variable = "AWS:SourceArn"
values = [aws_cloudfront_distribution.frontend.arn]
}
}
statement {
sid = "AllowDeploymentBucket"
principals {
type = "AWS"
identifiers = [
aws_iam_role.blog_deploy.arn,
"arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"
]
}
actions = [
"s3:ListBucketMultipartUploads",
"s3:ListBucket",
"s3:GetBucketLocation",
]
resources = [aws_s3_bucket.blog.arn]
}
statement {
sid = "AllowDeploymentFiles"
principals {
type = "AWS"
identifiers = [
aws_iam_role.blog_deploy.arn,
"arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"
]
}
actions = [
"s3:AbortMultipartUpload",
"s3:DeleteObject",
"s3:DeleteObjectVersion",
"s3:GetObject",
"s3:GetObjectAcl",
"s3:GetObjectVersion",
"s3:GetObjectVersionAcl",
"s3:PutObject",
"s3:PutObjectAcl",
]
resources = ["${aws_s3_bucket.blog.arn}/*"]
}
}
resource "aws_s3_bucket_policy" "policy" {
bucket = aws_s3_bucket.blog.id
policy = data.aws_iam_policy_document.policy.json
}
resource "aws_s3_bucket" "wwwblog" {
bucket = "www.${local.bucket_name}"
tags = local.tags
}
resource "aws_s3_bucket_acl" "wwwblog" {
bucket = aws_s3_bucket.wwwblog.id
}
resource "aws_s3_bucket_server_side_encryption_configuration" "wwwblog" {
bucket = aws_s3_bucket.wwwblog.id
rule {
bucket_key_enabled = false
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
}
resource "aws_s3_bucket_website_configuration" "wwwblog" {
bucket = aws_s3_bucket.wwwblog.id
redirect_all_requests_to {
host_name = local.bucket_name
}
}
resource "aws_cloudfront_distribution" "frontend" {
depends_on = [aws_s3_bucket.blog]
comment = "offby1.website hosting"
origin {
// Important to use this format of origin domain name, it is the only format that
// supports S3 redirects with CloudFront
domain_name = aws_s3_bucket.blog.bucket_domain_name
origin_id = var.s3_origin_id
origin_access_control_id = aws_cloudfront_origin_access_control.blog.id
}
enabled = true
http_version = "http1.1"
is_ipv6_enabled = false
default_root_object = "index.html"
aliases = local.domain_names
logging_config {
bucket = module.log_storage.cdn_logs_bucket
include_cookies = false
prefix = module.log_storage.cdn_logs_path
}
default_cache_behavior {
allowed_methods = ["GET", "HEAD"]
cached_methods = ["GET", "HEAD"]
target_origin_id = var.s3_origin_id
cache_policy_id = data.aws_cloudfront_cache_policy.none.id
origin_request_policy_id = data.aws_cloudfront_origin_request_policy.cors-s3.id
response_headers_policy_id = data.aws_cloudfront_response_headers_policy.gnu.id
viewer_protocol_policy = "redirect-to-https"
// Using CloudFront defaults, tune to liking
min_ttl = 0
default_ttl = 0
max_ttl = 0
}
price_class = "PriceClass_100"
viewer_certificate {
acm_certificate_arn = aws_acm_certificate.certificate.arn
ssl_support_method = "sni-only"
minimum_protocol_version = "TLSv1.1_2016"
}
restrictions {
geo_restriction {
restriction_type = "none"
}
}
tags = local.tags
}
resource "aws_cloudfront_origin_access_control" "blog" {
name = aws_s3_bucket.blog.bucket_domain_name
description = "Access to ${local.bucket_name}"
origin_access_control_origin_type = "s3"
signing_behavior = "always"
signing_protocol = "sigv4"
}
data "aws_cloudfront_cache_policy" "none" {
name = "Managed-CachingDisabled"
}
data "aws_cloudfront_origin_request_policy" "cors-s3" {
name = "Managed-CORS-S3Origin"
}
data "aws_cloudfront_response_headers_policy" "gnu" {
name = "GNU"
}
resource "aws_acm_certificate" "certificate" {
provider = aws.useast1
domain_name = local.domain_names[0]
tags = merge(local.tags, {
Name = "ideas-offby1-net"
})
validation_method = "DNS"
subject_alternative_names = slice(local.domain_names, 1, length(local.domain_names))
lifecycle {
create_before_destroy = true
}
}
resource "aws_route53_record" "cert_validation" {
for_each = {
for dvo in aws_acm_certificate.certificate.domain_validation_options : dvo.domain_name => {
name = dvo.resource_record_name
record = dvo.resource_record_value
type = dvo.resource_record_type
zone_id = local.zone_id_map[dvo.domain_name]
}
}
allow_overwrite = true
name = each.value.name
type = each.value.type
zone_id = each.value.zone_id
records = [each.value.record]
ttl = 60
}
resource "aws_acm_certificate_validation" "certificate" {
provider = aws.useast1
certificate_arn = aws_acm_certificate.certificate.arn
validation_record_fqdns = [for record in aws_route53_record.cert_validation : record.fqdn]
}
resource "aws_route53_record" "blog" {
zone_id = data.aws_route53_zone.website.zone_id
name = local.domain_name
type = "A"
alias {
name = aws_cloudfront_distribution.frontend.domain_name
zone_id = aws_cloudfront_distribution.frontend.hosted_zone_id
evaluate_target_health = true
}
}
data "aws_iam_user" "me" {
user_name = "chrisros"
}
data "aws_iam_openid_connect_provider" "github" {
url = "https://token.actions.githubusercontent.com"
}
# OIDC provider policy document
# see https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/configuring-openid-connect-in-amazon-web-services
data "aws_iam_policy_document" "deployer" {
statement {
effect = "Allow"
principals {
type = "AWS"
identifiers = [
data.aws_iam_user.me.arn
]
}
actions = ["sts:AssumeRole"]
}
statement {
effect = "Allow"
principals {
type = "Federated"
identifiers = [
data.aws_iam_openid_connect_provider.github.arn
]
}
actions = ["sts:AssumeRoleWithWebIdentity"]
condition {
test = "StringEquals"
variable = "token.actions.githubusercontent.com:aud"
values = ["sts.amazonaws.com"]
}
condition {
test = "StringLike"
variable = "token.actions.githubusercontent.com:sub"
values = ["repo:offbyone/ideas:*"]
}
}
}
resource "aws_iam_role" "blog_deploy" {
name = "ideas-deployer"
tags = local.tags
assume_role_policy = data.aws_iam_policy_document.deployer.json
}
data "aws_iam_policy_document" "deploy" {
statement {
sid = "AllowS3"
effect = "Allow"
actions = ["s3:*"]
resources = [
"${aws_s3_bucket.blog.arn}/*",
aws_s3_bucket.blog.arn
]
}
statement {
sid = "AllowCloudFront"
effect = "Allow"
actions = ["cloudfront:CreateInvalidation"]
resources = [
aws_cloudfront_distribution.frontend.arn
]
}
}
resource "aws_iam_role_policy" "cloudfront" {
role = aws_iam_role.blog_deploy.name
policy = data.aws_iam_policy_document.deploy.json
}