-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
232 lines (196 loc) · 6.18 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
provider "azurerm" {
skip_provider_registration = true # This is only required when the User, Service Principal, or Identity running Terraform lacks the permissions to register Azure Resource Providers.
features {}
}
resource "azurerm_resource_group" "resource" {
name = "res02test"
location = var.azure_region
}
resource "azurerm_storage_account" "storage" {
name = "testdev01"
depends_on = [azurerm_resource_group.resource]
resource_group_name = azurerm_resource_group.resource.name
location = azurerm_resource_group.resource.location
account_tier = "Standard"
account_replication_type = "LRS"
}
resource "azurerm_app_service_plan" "service_plan" {
name = "testdev01"
depends_on = [azurerm_storage_account.storage]
resource_group_name = azurerm_resource_group.resource.name
location = azurerm_resource_group.resource.location
kind = "elastic"
sku {
tier = "ElasticPremium"
size = "EP1"
}
}
resource "azurerm_application_insights" "app_insights" {
name = "testdev01"
depends_on = [azurerm_app_service_plan.service_plan]
resource_group_name = azurerm_resource_group.resource.name
location = azurerm_resource_group.resource.location
application_type = "Node.JS"
}
resource "azurerm_cosmosdb_account" "db" {
name = "testdev01"
depends_on = [azurerm_application_insights.app_insights]
location = azurerm_resource_group.resource.location
resource_group_name = azurerm_resource_group.resource.name
offer_type = "Standard"
# kind = "NoSQL"
lifecycle {
prevent_destroy = true
}
enable_automatic_failover = true
capacity {
total_throughput_limit = -1
}
consistency_policy {
consistency_level = "BoundedStaleness"
max_interval_in_seconds = 300
max_staleness_prefix = 100000
}
geo_location {
location = "westeurope"
failover_priority = 0
}
}
variable "windows_function_app_name" {
type = string
default = "testdev01"
}
resource "azurerm_windows_function_app" "function_app" {
depends_on = [azurerm_cosmosdb_account.db]
name = var.windows_function_app_name
resource_group_name = azurerm_resource_group.resource.name
location = azurerm_resource_group.resource.location
service_plan_id = azurerm_app_service_plan.service_plan.id
storage_account_name = azurerm_storage_account.storage.name
storage_account_access_key = azurerm_storage_account.storage.primary_access_key
https_only = true
identity {
type = "SystemAssigned"
}
app_settings = {
FUNCTIONS_WORKER_RUNTIME = "node"
WEBSITE_NODE_DEFAULT_VERSION = "~18"
WEBSITE_RUN_FROM_PACKAGE = "1"
FUNCTIONS_WORKER_RUNTIME = "node"
APPINSIGHTS_INSTRUMENTATIONKEY = azurerm_application_insights.app_insights.instrumentation_key
WEBSITE_CONTENTAZUREFILECONNECTIONSTRING = azurerm_storage_account.storage.primary_connection_string
AZURE_CONNSTR_BLOB_CONNECTION_STRING = azurerm_storage_account.storage.primary_connection_string
AZURE_CONNSTR_COSMOS_CONNECTION_STRING = azurerm_cosmosdb_account.db.connection_strings[0]
WEBSITE_CONTENTSHARE = var.windows_function_app_name
}
site_config {
application_stack {
node_version = "~16"
}
cors {
allowed_origins = [
"https://portal.azure.com"
]
}
}
}
variable "azure_functions" {
type = any
default = {
hello = {
name = "hello"
content = {
"bindings" = [
{
"authLevel" = "anonymous"
"direction" = "in"
"route" = "hello"
"methods" = [
"get",
"post"
]
"name" = "req"
"type" = "httpTrigger"
},
{
"direction" = "out"
"name" = "res"
"type" = "http"
},
],
"entryPoint" = "handler",
"scriptFile" = "../src/handlers/hello.js"
}
}
goodbye = {
name = "goodbye"
content = {
"bindings" = [
{
"authLevel" = "anonymous"
"direction" = "in"
"route" = "goodbye"
"methods" = [
"get",
"post"
]
"name" = "req"
"type" = "httpTrigger"
},
{
"direction" = "out"
"name" = "res"
"type" = "http"
},
],
"entryPoint" = "handler",
"scriptFile" = "../src/handlers/goodbye.js"
}
}
}
}
module "function_function" {
source = "./tf/modules/function"
depends_on = [azurerm_windows_function_app.function_app]
for_each = var.azure_functions
name = each.key
json_content = each.value.content
}
resource "local_file" "hostfile" {
depends_on = [module.function_function]
content = jsonencode(
{
"version" = "2.0",
"extensionBundle" = {
"id" = "Microsoft.Azure.Functions.ExtensionBundle",
"version" = "[4.*, 5.0.0)"
}
}
)
filename = "host.json"
}
locals {
files_to_zip = join(" ", keys(var.azure_functions))
folders_to_zip = join(" ", [ "src" ])
publish_code_command = <<EOT
zip -r -q -o backend.zip ${local.files_to_zip} host.json ${local.folders_to_zip} &&
rm ${local.files_to_zip} -rf &&
az functionapp deployment source config-zip -g ${azurerm_resource_group.resource.name} -n ${azurerm_windows_function_app.function_app.name} --src backend.zip
EOT
# az functionapp deployment source config-zip -g ${azurerm_resource_group.resource.name} -n ${azurerm_windows_function_app.function_app.name} --src backend.zip
# publish_code_command = "echo 'deploying...'"
}
resource "null_resource" "function_app_publish" {
provisioner "local-exec" {
command = local.publish_code_command
}
depends_on = [
local_file.hostfile,
module.function_function,
local.publish_code_command,
azurerm_windows_function_app.function_app
]
triggers = {
always_run = "${timestamp()}"
}
}