/
apim.tf
179 lines (157 loc) · 5.56 KB
/
apim.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
resource "azurerm_api_management" "apim" {
name = local.apimName
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
publisher_name = var.publisher_name
publisher_email = var.publisher_email
sku_name = var.apim_sku
public_ip_address_id = azurerm_public_ip.pip.id
identity {
type = "SystemAssigned"
}
virtual_network_type = "External"
virtual_network_configuration {
subnet_id = azurerm_subnet.apimsubnet.id
}
tags = var.tags
}
resource "azurerm_api_management_api" "apimapi" {
name = local.apiName
resource_group_name = azurerm_resource_group.rg.name
api_management_name = azurerm_api_management.apim.name
revision = "1"
display_name = local.apiName
path = local.apiPath
protocols = ["https"]
subscription_required = false
}
resource "azurerm_api_management_backend" "apimbackend" {
name = local.functionAppName
resource_group_name = azurerm_resource_group.rg.name
api_management_name = azurerm_api_management.apim.name
protocol = "http"
url = "https://${azurerm_windows_function_app.fn.name}.azurewebsites.net/api/"
}
resource "azurerm_api_management_named_value" "apimnv" {
name = "${azurerm_windows_function_app.fn.name}-key"
resource_group_name = azurerm_resource_group.rg.name
api_management_name = azurerm_api_management.apim.name
display_name = "${azurerm_windows_function_app.fn.name}-key"
secret = true
value_from_key_vault {
secret_id = data.azurerm_key_vault_secret.fnhk.id
}
}
resource "azurerm_api_management_api_policy" "apimBasePolicy" {
api_name = azurerm_api_management_api.apimapi.name
api_management_name = azurerm_api_management.apim.name
resource_group_name = azurerm_resource_group.rg.name
xml_content = <<XML
<policies>
<inbound>
<base />
<set-backend-service id="apim-generated-policy" backend-id="${azurerm_api_management_backend.apimbackend.name}" />
<validate-jwt header-name="Authorization" failed-validation-httpcode="401" failed-validation-error-message="Invalid token">
<openid-config url="https://login.microsoftonline.com/${data.azurerm_client_config.current.tenant_id}/v2.0/.well-known/openid-configuration" />
<audiences>
<audience>${azuread_application.app.application_id}</audience>
</audiences>
</validate-jwt>
<set-query-parameter name="code" exists-action="override">
<value>{{${azurerm_api_management_named_value.apimnv.name}}}</value>
</set-query-parameter>
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>
XML
}
# APIM operations
resource "azurerm_api_management_api_operation" "apimtprop" {
operation_id = "TakePictureRequestBase64"
api_name = azurerm_api_management_api.apimapi.name
api_management_name = azurerm_api_management.apim.name
resource_group_name = azurerm_resource_group.rg.name
display_name = "TakePictureRequestBase64"
method = "GET"
url_template = "/TakePictureRequestBase64"
description = "Take camera picture and return base64 encoded image"
response {
status_code = 200
}
}
resource "azurerm_api_management_api_operation" "apimrpaop" {
operation_id = "RequestPictureAnalysis"
api_name = azurerm_api_management_api.apimapi.name
api_management_name = azurerm_api_management.apim.name
resource_group_name = azurerm_resource_group.rg.name
display_name = "RequestPictureAnalysis"
method = "POST"
url_template = "/RequestPictureAnalysis"
description = "Send a camera picture to the visual inspection extension service for analysis"
response {
status_code = 200
}
}
# Create a logger to send logs to Application Insights
resource "azurerm_api_management_logger" "apimlogger" {
name = "apimlogger"
api_management_name = azurerm_api_management.apim.name
resource_group_name = azurerm_resource_group.rg.name
resource_id = azurerm_application_insights.appinsights.id
application_insights {
instrumentation_key = azurerm_application_insights.appinsights.instrumentation_key
}
}
# Create a diagnostic setting to send logs to Application Insights
resource "azurerm_api_management_api_diagnostic" "apimdiag" {
identifier = "applicationinsights"
resource_group_name = azurerm_resource_group.rg.name
api_management_name = azurerm_api_management.apim.name
api_name = azurerm_api_management_api.apimapi.name
api_management_logger_id = azurerm_api_management_logger.apimlogger.id
sampling_percentage = 5.0
always_log_errors = true
log_client_ip = true
verbosity = "verbose"
http_correlation_protocol = "W3C"
frontend_request {
body_bytes = 32
headers_to_log = [
"content-type",
"accept",
"origin",
]
}
frontend_response {
body_bytes = 32
headers_to_log = [
"content-type",
"content-length",
"origin",
]
}
backend_request {
body_bytes = 32
headers_to_log = [
"content-type",
"accept",
"origin",
]
}
backend_response {
body_bytes = 32
headers_to_log = [
"content-type",
"content-length",
"origin",
]
}
}