forked from shibayan/terraform-azurerm-keyvault-acmebot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
303 lines (265 loc) · 10.1 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
295
296
297
298
299
300
301
302
303
resource "azurerm_storage_account" "storage" {
name = var.storage_account_name
resource_group_name = var.resource_group_name
location = var.location
account_kind = "StorageV2"
account_tier = "Standard"
account_replication_type = "LRS"
enable_https_traffic_only = true
allow_nested_items_to_be_public = false
min_tls_version = "TLS1_2"
tags = merge(var.tags, {})
network_rules {
default_action = "Deny"
ip_rules = length(var.allowed_ip_addresses ) > 0 ? var.allowed_ip_addresses : null
virtual_network_subnet_ids = concat(
length(var.virtual_network_subnet_ids_pe ) > 0 ? var.virtual_network_subnet_ids_pe : [],
length(var.virtual_network_subnet_ids_integration) > 0 ? var.virtual_network_subnet_ids_integration : []
)
}
lifecycle {
ignore_changes = [
tags
]
}
}
resource "azurerm_service_plan" "serverfarm" {
name = var.app_service_plan_name
resource_group_name = var.resource_group_name
location = var.location
tags = merge(var.tags, {})
os_type = "Windows"
sku_name = var.sku_name
lifecycle {
ignore_changes = [
tags
]
}
}
resource "azurerm_log_analytics_workspace" "workspace" {
name = var.workspace_name
resource_group_name = var.resource_group_name
location = var.location
sku = "PerGB2018"
retention_in_days = 30
tags = merge(var.tags, {})
lifecycle {
ignore_changes = [
tags
]
}
}
resource "azurerm_application_insights" "insights" {
name = var.app_insights_name
resource_group_name = var.resource_group_name
location = var.location
application_type = "web"
workspace_id = azurerm_log_analytics_workspace.workspace.id
tags = merge(var.tags, {})
depends_on = [
azurerm_log_analytics_workspace.workspace
]
lifecycle {
ignore_changes = [
tags
]
}
}
resource "azurerm_windows_function_app" "function" {
name = var.function_app_name
resource_group_name = var.resource_group_name
location = var.location
service_plan_id = azurerm_service_plan.serverfarm.id
storage_account_name = azurerm_storage_account.storage.name
storage_account_access_key = azurerm_storage_account.storage.primary_access_key
tags = merge(var.tags, {})
functions_extension_version = "~4"
https_only = true
app_settings = merge({
"WEBSITE_RUN_FROM_PACKAGE" = "https://stacmebotprod.blob.core.windows.net/keyvault-acmebot/v4/latest.zip"
"WEBSITE_TIME_ZONE" = var.time_zone
}, local.acmebot_app_settings, var.app_settings)
virtual_network_subnet_id = length(var.virtual_network_subnet_ids_integration) > 0 ? var.virtual_network_subnet_ids_integration[0] : null
identity {
type = "SystemAssigned"
}
# dynamic "auth_settings" {
# for_each = toset(var.auth_settings != null ? [1] : [])
# content {
# enabled = var.auth_settings.enabled
# unauthenticated_client_action = var.auth_settings.unauthenticated_client_action
# issuer = var.auth_settings.issuer
# token_store_enabled = var.auth_settings.token_store_enabled
# active_directory {
# allowed_audiences = var.auth_settings.active_directory.allowed_audiences
# client_id = var.auth_settings.active_directory.client_id
# }
# }
# }
dynamic "auth_settings_v2" {
for_each = toset(var.auth_settings != null ? [1] : [])
content {
# https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/windows_function_app#login
auth_enabled = var.auth_settings.enabled
unauthenticated_action = var.auth_settings.unauthenticated_client_action
runtime_version = "~1"
config_file_path = null
require_authentication = true
require_https = true
default_provider = "azureactivedirectory"
excluded_paths = []
http_route_api_prefix = "/.auth"
forward_proxy_convention = "NoProxy"
active_directory_v2 {
client_id = var.auth_settings.active_directory.client_id
client_secret_setting_name = var.auth_settings.active_directory.client_secret_setting_name
tenant_auth_endpoint = "https://login.microsoftonline.com/${var.auth_settings.active_directory.tenant_id}/v2.0/"
allowed_audiences = var.auth_settings.active_directory.allowed_audiences
}
login {
allowed_external_redirect_urls = var.allowed_external_redirect_urls == null ? [] : var.allowed_external_redirect_urls
cookie_expiration_convention = "FixedTime"
cookie_expiration_time = "08:00:00"
nonce_expiration_time = "00:05:00"
preserve_url_fragments_for_logins = false
token_refresh_extension_time = 72
token_store_enabled = var.auth_settings.token_store_enabled
validate_nonce = true
}
# apple_v2 {
# login_scopes = []
# }
# facebook_v2 {
# login_scopes = []
# }
# github_v2 {
# login_scopes = []
# }
# google_v2 {
# allowed_audiences = []
# login_scopes = []
# }
# microsoft_v2 {
# allowed_audiences = []
# login_scopes = []
# }
# twitter_v2 {}
}
}
site_config {
application_insights_connection_string = azurerm_application_insights.insights.connection_string
application_insights_key = azurerm_application_insights.insights.instrumentation_key
ftps_state = "Disabled"
minimum_tls_version = "1.2"
http2_enabled = true
health_check_path = "/dashboard"
always_on = var.always_on
app_scale_limit = var.app_scale_limit
vnet_route_all_enabled = var.vnet_route_all_enabled
application_stack {
dotnet_version = "v6.0"
}
dynamic "ip_restriction" {
for_each = local.function_ip_restrictions
content {
ip_address = ip_restriction.value.ip_address
virtual_network_subnet_id = ip_restriction.value.virtual_network_subnet_id
}
}
dynamic "scm_ip_restriction" {
for_each = local.function_ip_restrictions
content {
ip_address = scm_ip_restriction.value.ip_address
virtual_network_subnet_id = scm_ip_restriction.value.virtual_network_subnet_id
}
}
dynamic "cors" {
for_each = toset(var.cors_allowed_origins != null ? [1] : [])
content {
allowed_origins = var.cors_allowed_origins
support_credentials = true
}
}
}
depends_on = [
azurerm_storage_account.storage,
azurerm_application_insights.insights,
azurerm_service_plan.serverfarm
]
lifecycle {
ignore_changes = [
tags,
app_settings["MICROSOFT_PROVIDER_AUTHENTICATION_SECRET"],
sticky_settings["app_setting_names"]
]
}
}
resource "azurerm_private_endpoint" "func-pe" {
for_each = local.virtual_network_subnet_ids_pe_dict
name = "${var.function_app_name}-pe"
location = var.location
resource_group_name = var.resource_group_name
subnet_id = each.value
tags = merge(var.tags, {})
private_service_connection {
name = "${var.function_app_name}-psc"
private_connection_resource_id = azurerm_windows_function_app.function.id
is_manual_connection = false
subresource_names = ["sites"]
}
depends_on = [
azurerm_windows_function_app.function
]
}
resource "azurerm_private_endpoint" "sto-pe" {
for_each = local.storage_pe
name = "${var.storage_account_name}-${each.value.subresource_name}-pe"
location = var.location
resource_group_name = var.resource_group_name
subnet_id = each.value.subnet_id
tags = merge(var.tags, {})
private_service_connection {
name = "${var.storage_account_name}-psc"
private_connection_resource_id = azurerm_storage_account.storage.id
is_manual_connection = false
subresource_names = [each.value.subresource_name]
}
depends_on = [
azurerm_storage_account.storage
]
}
resource "azurerm_private_dns_a_record" "dns_a_storage" {
for_each = local.storage_pe
zone_name = each.value.private_dns_zone_name
resource_group_name = var.private_dns_zone_rg
name = var.storage_account_name
records = azurerm_private_endpoint.sto-pe[each.key].custom_dns_configs[0].ip_addresses
tags = merge(var.tags, {})
ttl = 300
depends_on = [
azurerm_private_endpoint.sto-pe
]
}
resource "azurerm_private_dns_a_record" "dns_a_function_web" {
for_each = merge(
flatten([
for k, v in local.virtual_network_subnet_ids_pe_dict: [
for l in [0, 1]: {
"${k}-${l}" = {
key = k
conf = l
}
}
]
])
...)
zone_name = var.private_dns_zone_names_function["web"]
resource_group_name = var.private_dns_zone_rg
ttl = 300
name = replace(azurerm_private_endpoint.func-pe[each.value.key].custom_dns_configs[each.value.conf].fqdn, ".azurewebsites.net", "")
records = azurerm_private_endpoint.func-pe[each.value.key].custom_dns_configs[each.value.conf].ip_addresses
tags = merge(var.tags, {})
depends_on = [
azurerm_private_endpoint.func-pe
]
}