Terraformを使ってApplication Insightsの設定を行う

こんにちは。cloud.config Divの神田です。今回はTerraformを使ってAppServiceとApplication Insightsの連携を行う方法です。
TerraformでApplication Insightsを有効にするには
AppServiceにApplication Insightsを設定すると、AppServiceの「構成」ブレードの「アプリケーション設定」に設定が反映されます。Terraformではこの「アプリケーション設定」の値を設定することが可能なので、こちらを利用してApplication Insightsを設定していきます。(残念ながらGUIに対応したパラメータは用意されておりません。)

とりあえず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"
}
}

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"
}
}

スナップショットデバッガーを有効にする
以下がスナップショットデバッガーを有効にするコードです。ただし、「 例外がスローされたときに、アプリケーションのローカル変数を表示します。 」の設定は無効です。
# 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"
}
}

スナップショットデバッガー+ローカル変数の表示を有効にする
以下がスナップショットデバッガーと「 例外がスローされたときに、アプリケーションのローカル変数を表示します。 」を有効にするコードです。
# 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"
}
}

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"
}
}

全部盛りにする
以下が上記すべての設定を有効にするコードです。
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"
}
}

最後に
同じ設定のリソースを繰り返し作成する場合などは、GUIを操作するよりもコードにしたほうが作成、管理の手間が省けるのでいいですね。