-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
308 lines (255 loc) · 9.14 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
304
305
306
307
308
# Configure the Azure provider
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 3.92.0"
}
}
required_version = ">= 1.1.0"
}
provider "azurerm" {
features {}
}
variable "application" {
type = string
default = "frontend"
}
variable "location" {
type = string
default = "eastus"
}
locals {
resource_group_name = "rg-${var.application}-${var.location}"
storage_name = "stg${var.application}${var.location}"
storage_share_name = "stg-shr-${var.application}${var.location}"
service_plan_name = "sp-${var.application}-${var.location}"
function_app_name = "fa-${var.application}-${var.location}"
application_insights_name = "ai-${var.application}-${var.location}"
api_management = "apim-${var.application}-${var.location}"
api_management_api = "products-service-api"
api_management_backend = "products-service-backend"
db_name = "db-${var.application}-${var.location}"
}
resource "azurerm_resource_group" "rg_workshop" {
name = local.resource_group_name
location = var.location
}
resource "azurerm_storage_account" "stg_workshop" {
name = local.storage_name
location = var.location
account_replication_type = "LRS"
account_tier = "Standard"
account_kind = "StorageV2"
resource_group_name = azurerm_resource_group.rg_workshop.name
static_website {
index_document = "index.html"
}
}
resource "azurerm_storage_share" "stg_shr_workshop" {
name = local.storage_share_name
quota = 2
storage_account_name = azurerm_storage_account.stg_workshop.name
}
resource "azurerm_service_plan" "sp_workshop" {
name = local.service_plan_name
location = var.location
os_type = "Windows"
sku_name = "Y1"
resource_group_name = azurerm_resource_group.rg_workshop.name
}
resource "azurerm_application_insights" "ai_workshop" {
name = local.application_insights_name
application_type = "web"
location = var.location
resource_group_name = azurerm_resource_group.rg_workshop.name
}
resource "azurerm_windows_function_app" "fa_workshop" {
name = local.function_app_name
location = var.location
service_plan_id = azurerm_service_plan.sp_workshop.id
resource_group_name = azurerm_resource_group.rg_workshop.name
storage_account_name = azurerm_storage_account.stg_workshop.name
storage_account_access_key = azurerm_storage_account.stg_workshop.primary_access_key
functions_extension_version = "~4"
builtin_logging_enabled = false
site_config {
always_on = false
application_insights_key = azurerm_application_insights.ai_workshop.instrumentation_key
application_insights_connection_string = azurerm_application_insights.ai_workshop.connection_string
# For production systems set this to false, but consumption plan supports only 32bit workers
use_32_bit_worker = true
# Enable function invocations from Azure Portal.
cors {
allowed_origins = ["https://portal.azure.com"]
}
application_stack {
node_version = "~16"
}
}
app_settings = {
WEBSITE_CONTENTAZUREFILECONNECTIONSTRING = azurerm_storage_account.stg_workshop.primary_connection_string
WEBSITE_CONTENTSHARE = azurerm_storage_share.stg_shr_workshop.name
}
# The app settings changes cause downtime on the Function App. e.g. with Azure Function App Slots
# Therefore it is better to ignore those changes and manage app settings separately off the Terraform.
lifecycle {
ignore_changes = [
app_settings,
site_config["application_stack"], // workaround for a bug when azure just "kills" your app
tags["hidden-link: /app-insights-instrumentation-key"],
tags["hidden-link: /app-insights-resource-id"],
tags["hidden-link: /app-insights-conn-string"]
]
}
}
resource "azurerm_api_management" "apim_workshop" {
name = local.api_management
location = azurerm_resource_group.rg_workshop.location
resource_group_name = azurerm_resource_group.rg_workshop.name
publisher_name = "Artyom Gospodarsky"
publisher_email = "azure@gospodarsky.simplelogin.com"
sku_name = "Consumption_0"
}
resource "azurerm_api_management_api" "apim_api_workshop" {
name = local.api_management_api
resource_group_name = azurerm_resource_group.rg_workshop.name
api_management_name = azurerm_api_management.apim_workshop.name
revision = "1"
display_name = "Products Service API"
protocols = ["https"]
subscription_required = false
}
data "azurerm_function_app_host_keys" "products_keys" {
name = azurerm_windows_function_app.fa_workshop.name
resource_group_name = azurerm_resource_group.rg_workshop.name
}
resource "azurerm_api_management_backend" "apim_backend_workshop" {
name = local.api_management_backend
resource_group_name = azurerm_resource_group.rg_workshop.name
api_management_name = azurerm_api_management.apim_workshop.name
protocol = "http"
url = "https://${azurerm_windows_function_app.fa_workshop.name}.azurewebsites.net/api"
description = "Products API"
credentials {
certificate = []
query = {}
header = {
"x-functions-key" = data.azurerm_function_app_host_keys.products_keys.default_function_key
}
}
}
resource "azurerm_api_management_api_policy" "api_policy" {
api_management_name = azurerm_api_management.apim_workshop.name
api_name = azurerm_api_management_api.apim_api_workshop.name
resource_group_name = azurerm_resource_group.rg_workshop.name
xml_content = <<XML
<policies>
<inbound>
<set-backend-service backend-id="${azurerm_api_management_backend.apim_backend_workshop.name}"/>
<cors allow-credentials="false">
<allowed-origins>
<origin>*</origin>
</allowed-origins>
<allowed-methods>
<method>GET</method>
<method>POST</method>
<method>PUT</method>
</allowed-methods>
</cors>
<base/>
</inbound>
<backend>
<base/>
</backend>
<outbound>
<base/>yes
</outbound>
<on-error>
<base/>
</on-error>
</policies>
XML
}
resource "azurerm_api_management_api_operation" "get_products" {
resource_group_name = azurerm_resource_group.rg_workshop.name
api_management_name = azurerm_api_management.apim_workshop.name
api_name = azurerm_api_management_api.apim_api_workshop.name
display_name = "Get Products"
method = "GET"
operation_id = "get-products"
url_template = "/products"
}
resource "azurerm_api_management_api_operation" "create_product" {
resource_group_name = azurerm_resource_group.rg_workshop.name
api_management_name = azurerm_api_management.apim_workshop.name
api_name = azurerm_api_management_api.apim_api_workshop.name
display_name = "Create Product"
method = "POST"
operation_id = "create-product"
url_template = "/products"
}
resource "azurerm_api_management_api_operation" "get_product_by_id" {
resource_group_name = azurerm_resource_group.rg_workshop.name
api_management_name = azurerm_api_management.apim_workshop.name
api_name = azurerm_api_management_api.apim_api_workshop.name
display_name = "Get Product by ID"
method = "GET"
operation_id = "get-product-by-id"
url_template = "/products/{productId}"
template_parameter {
name = "productId"
type = "number"
required = true
}
}
resource "azurerm_cosmosdb_account" "db_workshop" {
resource_group_name = azurerm_resource_group.rg_workshop.name
location = azurerm_resource_group.rg_workshop.location
name = local.db_name
offer_type = "Standard"
kind = "GlobalDocumentDB"
consistency_policy {
consistency_level = "Eventual"
}
capabilities {
name = "EnableServerless"
}
geo_location {
failover_priority = 0
location = azurerm_resource_group.rg_workshop.location
}
}
resource "azurerm_cosmosdb_sql_database" "products" {
account_name = azurerm_cosmosdb_account.db_workshop.name
resource_group_name = azurerm_resource_group.rg_workshop.name
name = "products-db"
}
resource "azurerm_cosmosdb_sql_container" "products_container" {
resource_group_name = azurerm_resource_group.rg_workshop.name
account_name = azurerm_cosmosdb_account.db_workshop.name
database_name = azurerm_cosmosdb_sql_database.products.name
name = "products"
partition_key_path = "/id"
# Cosmos DB supports TTL for the records
default_ttl = -1
indexing_policy {
excluded_path {
path = "/*"
}
}
}
resource "azurerm_cosmosdb_sql_container" "stocks_container" {
resource_group_name = azurerm_resource_group.rg_workshop.name
account_name = azurerm_cosmosdb_account.db_workshop.name
database_name = azurerm_cosmosdb_sql_database.products.name
name = "stocks"
partition_key_path = "/product_id"
# Cosmos DB supports TTL for the records
default_ttl = -1
indexing_policy {
excluded_path {
path = "/*"
}
}
}