Terraformを使ってApplication Insightsの設定を行う
![azblob://2022/11/11/eyecatch/2019-12-30-terraform-application-insights-000-2.png](https://prtechblogfd-crhzb5g6hkhqafdm.z01.azurefd.net/drupalimages/2022/11/11/eyecatch/2019-12-30-terraform-application-insights-000-2.png)
こんにちは。cloud.config Divの神田です。今回はTerraformを使ってAppServiceとApplication Insightsの連携を行う方法です。
TerraformでApplication Insightsを有効にするには
AppServiceにApplication Insightsを設定すると、AppServiceの「構成」ブレードの「アプリケーション設定」に設定が反映されます。Terraformではこの「アプリケーション設定」の値を設定することが可能なので、こちらを利用してApplication Insightsを設定していきます。(残念ながらGUIに対応したパラメータは用意されておりません。)
![](https://prtechblogfd-crhzb5g6hkhqafdm.z01.azurefd.net/drupalimages/beforemigration/2019-12-30-terraform-application-insights-001-1.png?fit=856%2C415&ssl=1)
とりあえずApplication Insightsを有効にする
以下がAppServiceに対して、Application Insightsを有効にし、コレクションレベルを「推奨」に設定するコードです。しかし、この状態ではProfilerやスナップショットデバッガーといった設定は有効になっていません。
# Application Insightsの生成
resource "azurerm_application_insights" "appin01" {
name = "Application Insightsのリソース名"
location = "リージョン"
resource_group_name = "リソースグループ名"
application_type = "web"
}
# AppServiceの生成
resource "azurerm_app_service" "app01" {
name = "AppServiceのリソース名"
location = "リージョン"
resource_group_name = "リソースグループ名"
app_service_plan_id = "AppServicePlanのリソースID"
app_settings = {
"APPINSIGHTS_INSTRUMENTATIONKEY" = "${azurerm_application_insights.appin01.instrumentation_key}"
"ApplicationInsightsAgent_EXTENSION_VERSION" = "~2"
"XDT_MicrosoftApplicationInsights_Mode" = "recommended"
}
}
![](https://prtechblogfd-crhzb5g6hkhqafdm.z01.azurefd.net/drupalimages/beforemigration/2019-12-30-terraform-application-insights-002-1.png?fit=856%2C417&ssl=1)
Profilerを有効にする
以下がProfilerを有効にするコードです。
# AppServiceの生成
resource "azurerm_app_service" "app02" {
name = "AppServiceのリソース名"
location = "リージョン"
resource_group_name = "リソースグループ名"
app_service_plan_id = "AppServicePlanのリソースID"
app_settings = {
"APPINSIGHTS_INSTRUMENTATIONKEY" = "${azurerm_application_insights.appin01.instrumentation_key}"
"ApplicationInsightsAgent_EXTENSION_VERSION" = "~2"
"APPINSIGHTS_PROFILERFEATURE_VERSION" = "1.0.0"
"DiagnosticServices_EXTENSION_VERSION" = "~3"
"XDT_MicrosoftApplicationInsights_Mode" = "recommended"
}
}
![](https://prtechblogfd-crhzb5g6hkhqafdm.z01.azurefd.net/drupalimages/beforemigration/2019-12-30-terraform-application-insights-003-1.png?fit=856%2C416&ssl=1)
スナップショットデバッガーを有効にする
以下がスナップショットデバッガーを有効にするコードです。ただし、「 例外がスローされたときに、アプリケーションのローカル変数を表示します。 」の設定は無効です。
# AppServiceの生成
resource "azurerm_app_service" "app03" {
name = "AppServiceのリソース名"
location = "リージョン"
resource_group_name = "リソースグループ名"
app_service_plan_id = "AppServicePlanのリソースID"
app_settings = {
"APPINSIGHTS_INSTRUMENTATIONKEY" = "${azurerm_application_insights.appin01.instrumentation_key}"
"ApplicationInsightsAgent_EXTENSION_VERSION" = "~2"
"APPINSIGHTS_SNAPSHOTFEATURE_VERSION" = "1.0.0"
"DiagnosticServices_EXTENSION_VERSION" = "~3"
"XDT_MicrosoftApplicationInsights_Mode" = "recommended"
}
}
![](https://prtechblogfd-crhzb5g6hkhqafdm.z01.azurefd.net/drupalimages/beforemigration/2019-12-30-terraform-application-insights-004-1.png?fit=856%2C417&ssl=1)
スナップショットデバッガー+ローカル変数の表示を有効にする
以下がスナップショットデバッガーと「 例外がスローされたときに、アプリケーションのローカル変数を表示します。 」を有効にするコードです。
# AppServiceの生成
resource "azurerm_app_service" "app04" {
name = "AppServiceのリソース名"
location = "リージョン"
resource_group_name = "リソースグループ名"
app_service_plan_id = "AppServicePlanのリソースID"
app_settings = {
"APPINSIGHTS_INSTRUMENTATIONKEY" = "${azurerm_application_insights.appin01.instrumentation_key}"
"ApplicationInsightsAgent_EXTENSION_VERSION" = "~2"
"APPINSIGHTS_SNAPSHOTFEATURE_VERSION" = "1.0.0"
"DiagnosticServices_EXTENSION_VERSION" = "~3"
"InstrumentationEngine_EXTENSION_VERSION" = "~1"
"SnapshotDebugger_EXTENSION_VERSION" = "~1"
"XDT_MicrosoftApplicationInsights_BaseExtensions" = "disabled"
"XDT_MicrosoftApplicationInsights_Mode" = "recommended"
}
}
![](https://prtechblogfd-crhzb5g6hkhqafdm.z01.azurefd.net/drupalimages/beforemigration/2019-12-30-terraform-application-insights-005-1.png?fit=856%2C416&ssl=1)
SQLコマンドを有効にする
以下がSQLコマンドを有効にするコードです。
resource "azurerm_app_service" "app05" {
name = "AppServiceのリソース名"
location = "リージョン"
resource_group_name = "リソースグループ名"
app_service_plan_id = "AppServicePlanのリソースID"
app_settings = {
"APPINSIGHTS_INSTRUMENTATIONKEY" = "${azurerm_application_insights.appin01.instrumentation_key}"
"InstrumentationEngine_EXTENSION_VERSION" = "~1"
"ApplicationInsightsAgent_EXTENSION_VERSION" = "~2"
"XDT_MicrosoftApplicationInsights_BaseExtensions" = "~1"
"XDT_MicrosoftApplicationInsights_Mode" = "recommended"
}
}
![](https://prtechblogfd-crhzb5g6hkhqafdm.z01.azurefd.net/drupalimages/beforemigration/2019-12-30-terraform-application-insights-006.png?fit=856%2C415&ssl=1)
全部盛りにする
以下が上記すべての設定を有効にするコードです。
resource "azurerm_app_service" "app06" {
name = "AppServiceのリソース名"
location = "リージョン"
resource_group_name = "リソースグループ名"
app_service_plan_id = "AppServicePlanのリソースID"
app_settings = {
"APPINSIGHTS_INSTRUMENTATIONKEY" = "${azurerm_application_insights.appin01.instrumentation_key}"
"APPINSIGHTS_PROFILERFEATURE_VERSION" = "1.0.0"
"APPINSIGHTS_SNAPSHOTFEATURE_VERSION" = "1.0.0"
"ApplicationInsightsAgent_EXTENSION_VERSION" = "~2"
"DiagnosticServices_EXTENSION_VERSION" = "~3"
"InstrumentationEngine_EXTENSION_VERSION" = "~1"
"SnapshotDebugger_EXTENSION_VERSION" = "~1"
"XDT_MicrosoftApplicationInsights_BaseExtensions" = "~1"
"XDT_MicrosoftApplicationInsights_Mode" = "recommended"
}
}
![](https://prtechblogfd-crhzb5g6hkhqafdm.z01.azurefd.net/drupalimages/beforemigration/2019-12-30-terraform-application-insights-007-1.png?fit=856%2C415&ssl=1)
最後に
同じ設定のリソースを繰り返し作成する場合などは、GUIを操作するよりもコードにしたほうが作成、管理の手間が省けるのでいいですね。