-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.tf
199 lines (198 loc) · 7.8 KB
/
config.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
#################################################
# General Configuration and Settings
#################################################
locals {
env = var.env
is_anon = var.anonymous
prod_envs = ["main", "prod", "production"]
is_prod = contains(local.prod_envs, local.env)
use_subdom = !local.is_prod
env_prefix = (local.is_prod ? "" : "${var.env}-")
env_subdom = (local.is_prod ? "" : "${var.env}.")
region = "us-east-1"
dev_endpoint = var.local_dev_endpoint
app_name = var.app_name
app_slug = lower(replace(var.app_name, "/[\\s\\W]+/", "-"))
app_domain_root = var.app_domain
app_domain = "${local.env_subdom}${var.app_domain}"
api_domain = "api.${local.app_domain}"
auth_domain = "id.${local.app_domain}"
web_app_domain = "app.${local.app_domain}"
web_apps = var.web_apps
cert_sans = ["${local.app_domain}", local.api_domain, local.web_app_domain, local.auth_domain, "*.${local.app_domain}"]
s3w_origin_id = "${local.app_slug}-${local.env_prefix}origin"
idps = local.is_anon ? var.identity_providers : []
cognito = {
user_pool_name = "${local.app_slug}-${local.env_prefix}users"
authorizer_name = "${local.app_slug}-${local.env_prefix}authorizer"
passwords = var.password_rules
id_token = {
units = var.token_validity.id_token.units
duration = var.token_validity.id_token.duration
}
access_token = {
units = var.token_validity.access_token.units
duration = var.token_validity.access_token.duration
}
refresh_token = {
units = var.token_validity.refresh_token.units
duration = var.token_validity.refresh_token.duration
}
}
default_tags = merge({
Name = local.app_name
Domain = local.app_domain_root
Subdomain = local.app_domain
}, var.tags)
api_gateway = {
name = "${local.app_slug}-${local.env_prefix}api"
stage = var.env
description = "The ${local.app_name} API Gateway."
}
roles = {
lambda_exec = "${local.app_slug}-${local.env_prefix}lambda-exec"
}
regex_origin = "^lambda://([^+\n]*)\\+?(.*)?$"
idp_names = [for idp in var.identity_providers : idp.name]
web_app_cnames = [for app, target in local.web_apps : "${app}.${local.app_domain}"]
web_app_origins = tomap({ for app in
compact(
[for app, target in local.web_apps :
can(regex(local.regex_origin, target)) ? app : ""
]
) :
app => "https://${local.api_domain}/${trimprefix(tolist(
lookup(
var.lambda_configs,
regex(
local.regex_origin,
lookup(local.web_apps, app)
)[0]
).routes
)[0].path, "/")}"
})
web_app_origin_plus = tomap({ for app in keys(local.web_apps) :
app => length(distinct(concat(compact(flatten(regexall(local.regex_origin, lookup(local.web_apps, app)))), [null, null]))) >= 2 ?
distinct(concat(compact(flatten(regexall(local.regex_origin, lookup(local.web_apps, app)))), [null, null]))[1] : lookup(local.web_apps, app)
})
web_app_origin_groups = tomap({ for app in keys(local.web_app_origins) :
app => {
lambda = lookup(local.web_app_origins, app, null)
fileset = lookup(local.web_app_origin_plus, app, null)
}
})
web_apps_count = length(keys(local.web_apps))
web_app_file_sources = merge(local.web_apps, local.web_app_origin_plus)
web_apps_files = { for obj in tolist(
flatten([for app, target in local.web_app_file_sources :
target == null ? [] :
tolist([for f in fileset(target, "**") : {
target = local.web_apps_count > 1 ? "${app}/${f}" : f
source = "${target}/${f}"
}])
])
) : obj.target => obj.source }
lambda_routes = { for obj in
flatten(
flatten([for key, lambda in var.lambda_configs :
flatten([for ri, route in tolist(lambda.routes) : {
key = "${local.env_prefix}${key}${(ri > 0 ? "-${format("%03s", ri)}" : "")}"
lambda = key
path = route.path
method = route.method
anon = lambda.anonymous || local.is_anon
auth = (lambda.anonymous ? "NONE" : "JWT")
}])
])
) : obj.key => obj
}
lambdas_cloudfront_name = "${local.app_slug}-${local.env_prefix}cf-subdom-router"
lambdas_cloudfront = { for key in compact([for k, l in var.lambda_configs : l.cloudfront_event != "" ? k : ""]) : key => lookup(var.lambda_configs, key) }
lambda_endpoints = { for key in compact([for k, l in var.lambda_configs : length(l.routes) > 0 ? k : ""]) : key => lookup(var.lambda_configs, key) }
lambda_routes_anon = { for key in compact([for k, l in local.lambda_routes : l.anon ? k : ""]) : key => lookup(local.lambda_routes, key) }
lambda_routes_auth = { for key in compact([for k, l in local.lambda_routes : !l.anon ? k : ""]) : key => lookup(local.lambda_routes, key) }
token_map = merge({
"ROOT_DOMAIN" = local.app_domain
}, var.token_map)
token_keys = keys(local.token_map)
mime_map = {
"aac" = "audio/aac"
"abw" = "application/x-abiword"
"arc" = "application/x-freearc"
"avif" = "image/avif"
"avi" = "video/x-msvideo"
"azw" = "application/vnd.amazon.ebook"
"bin" = "application/octet-stream"
"bmp" = "image/bmp"
"bz" = "application/x-bzip"
"bz2" = "application/x-bzip2"
"cda" = "application/x-cdf"
"csh" = "application/x-csh"
"css" = "text/css"
"csv" = "text/csv"
"doc" = "application/msword"
"docx" = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
"eot" = "application/vnd.ms-fontobject"
"epub" = "application/epub+zip"
"gz" = "application/gzip"
"gif" = "image/gif"
"htm" = "text/html"
"html" = "text/html"
"ico" = "image/vnd.microsoft.icon"
"ics" = "text/calendar"
"jar" = "application/java-archive"
"jpeg" = "image/jpeg"
"jpg" = "image/jpeg"
"js" = "text/javascript"
"json" = "application/json"
"jsonld" = "application/ld+json"
"mid" = "audio/midi audio/x-midi"
"midi" = "audio/midi audio/x-midi"
"mjs" = "text/javascript"
"mp3" = "audio/mpeg"
"mp4" = "video/mp4"
"mpeg" = "video/mpeg"
"mpkg" = "application/vnd.apple.installer+xml"
"odp" = "application/vnd.oasis.opendocument.presentation"
"ods" = "application/vnd.oasis.opendocument.spreadsheet"
"odt" = "application/vnd.oasis.opendocument.text"
"oga" = "audio/ogg"
"ogv" = "video/ogg"
"ogx" = "application/ogg"
"opus" = "audio/opus"
"otf" = "font/otf"
"png" = "image/png"
"pdf" = "application/pdf"
"php" = "application/x-httpd-php"
"ppt" = "application/vnd.ms-powerpoint"
"pptx" = "application/vnd.openxmlformats-officedocument.presentationml.presentation"
"rar" = "application/vnd.rar"
"rtf" = "application/rtf"
"sh" = "application/x-sh"
"svg" = "image/svg+xml"
"swf" = "application/x-shockwave-flash"
"tar" = "application/x-tar"
"tif" = "image/tiff"
"tiff" = "image/tiff"
"ts" = "video/mp2t"
"ttf" = "font/ttf"
"txt" = "text/plain"
"vsd" = "application/vnd.visio"
"wasm" = "application/wasm"
"wav" = "audio/wav"
"weba" = "audio/webm"
"webm" = "video/webm"
"webp" = "image/webp"
"woff" = "font/woff"
"woff2" = "font/woff2"
"xhtml" = "application/xhtml+xml"
"xls" = "application/vnd.ms-excel"
"xlsx" = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
"xml" = "application/xml"
"xul" = "application/vnd.mozilla.xul+xml"
"zip" = "application/zip"
"3gp" = "video/3gpp"
"3g2" = "video/3gpp2"
"7z" = "application/x-7z-compressed"
}
}