-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
163 lines (132 loc) · 5.23 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
locals {
s3_origin_id = "s3"
use_authorizer = var.authorizer != null
use_website_endpoint = var.website_configuration != null
}
resource "random_string" "refer_secret" {
count = local.use_website_endpoint ? 1 : 0
length = 16
special = false
}
module "s3_bucket" {
source = "github.com/terraform-aws-modules/terraform-aws-s3-bucket?ref=8a0b697adfbc673e6135c70246cff7f8052ad95a" # v4.1.2
bucket = coalesce(var.bucket_name, var.name)
attach_public_policy = true
block_public_acls = local.use_website_endpoint == false
block_public_policy = local.use_website_endpoint == false
ignore_public_acls = local.use_website_endpoint == false
restrict_public_buckets = local.use_website_endpoint == false
attach_policy = true
policy = data.aws_iam_policy_document.s3_policy.json
server_side_encryption_configuration = {
rule = {
bucket_key_enabled = true
apply_server_side_encryption_by_default = {
sse_algorithm = "AES256"
}
}
}
website = coalesce(var.website_configuration, {})
}
module "authorizer" {
source = "github.com/chatloop/terraform-aws-cloudfront-auth?ref=695c3eb440a90e2441a006f9d091e6f73218fb0d" # v0.2.0
count = local.use_authorizer ? 1 : 0
providers = {
aws = aws.us-east-1
}
function_name = coalesce(var.authorizer.function_name, "${var.name}-oidc-auth")
tenant = var.authorizer.tenant
client_id = var.authorizer.client_id
client_secret = var.authorizer.client_secret
redirect_uri = coalesce(var.authorizer.redirect_uri, "https://${var.domain_name}/_callback")
session_duration = coalesce(var.authorizer.session_duration, 24)
trailing_slash_redirects_enabled = coalesce(var.authorizer.trailing_slash_redirects_enabled, true)
simple_urls_enabled = coalesce(var.authorizer.simple_urls_enabled, true)
}
module "cloudfront" {
source = "github.com/terraform-aws-modules/terraform-aws-cloudfront?ref=a0f0506106a4c8815c1c32596e327763acbef2c2" # v3.4.0
aliases = [var.domain_name]
comment = var.comment
default_root_object = var.default_root_object
http_version = "http2and3"
is_ipv6_enabled = true
price_class = "PriceClass_100"
# The OAC is not used when using website endpoints as these can't be restricted
create_origin_access_control = local.use_website_endpoint == false
origin_access_control = {
(var.name) = {
description = "OAC to restrict access to S3"
origin_type = "s3"
signing_behavior = "always"
signing_protocol = "sigv4"
}
}
origin = {
(local.s3_origin_id) = merge(
{
domain_name = local.use_website_endpoint == false ? module.s3_bucket.s3_bucket_bucket_regional_domain_name : module.s3_bucket.s3_bucket_website_endpoint
origin_access_control = var.name
},
local.use_website_endpoint == false ? {} : {
custom_header = [{
name = "User-Agent"
value = random_string.refer_secret[0].result
}]
},
local.use_website_endpoint == false ? {} : {
custom_origin_config = {
http_port = 80
https_port = 443
origin_ssl_protocols = ["TLSv1.2"]
origin_protocol_policy = "http-only"
}
}
)
}
default_cache_behavior = {
for k, v in merge(var.default_cache_behavior, {
target_origin_id = coalesce(var.default_cache_behavior.target_origin_id, local.s3_origin_id)
use_forwarded_values = false # this is a legacy CloudFront feature and policies should be used instead
lambda_function_association = (
local.use_authorizer == false || var.default_cache_behavior.use_authorizer == false ? {} : {
viewer-request = {
lambda_arn = module.authorizer[0].lambda_qualified_arn
}
}
)
}) : k => v if v != null
}
ordered_cache_behavior = [
for behavior in var.ordered_cache_behavior : {
for k, v in merge(behavior, {
target_origin_id = coalesce(behavior.target_origin_id, local.s3_origin_id)
use_forwarded_values = false # this is a legacy CloudFront feature and policies should be used instead
lambda_function_association = (
local.use_authorizer == false || behavior.use_authorizer == false ? {} : {
viewer-request = {
lambda_arn = module.authorizer[0].lambda_qualified_arn
}
}
)
}) : k => v if v != null
}
]
# [{}] is an odd default value but this avoids a bug in the module:
# https://github.com/terraform-aws-modules/terraform-aws-cloudfront/issues/121#issuecomment-1852172451
custom_error_response = length(var.custom_error_response) > 0 ? var.custom_error_response : [{}]
viewer_certificate = {
acm_certificate_arn = data.aws_acm_certificate.this.arn
minimum_protocol_version = "TLSv1.2_2021"
ssl_support_method = "sni-only"
}
}
resource "aws_route53_record" "this" {
zone_id = data.aws_route53_zone.this.id
name = var.domain_name
type = "A"
alias {
name = module.cloudfront.cloudfront_distribution_domain_name
zone_id = module.cloudfront.cloudfront_distribution_hosted_zone_id
evaluate_target_health = false
}
}