Bicepで クラシック Application Insightsは2024年2月29日に廃止されました と出力されたことに対応したメモ

# 概要
一番シンプルな形でApplication Insightsを作成したら下記の警告が出た。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/99876/33a18382-c769-860b-76db-944a6419fc7d.png)

bicepに追記をおこなったら警告が消えたのでメモをしておく。

[ソースコード](https://github.com/hibohiboo/async-ttrpg/blob/69e085ba734d04aeac2b5c3587af239184bff5ed/infra/biceps/core/host/applications.bicep)

## Bicep

```diff:infra/biceps/core/host/appplications.bicep
param location string
param applicationInsightsName string
+ param logAnalyticsName string

+ resource logAnalytics 'Microsoft.OperationalInsights/workspaces@2023-+ 09-01' = {
+   name: logAnalyticsName
+   location: location
+ }

resource applicationInsights 'Microsoft.Insights/components@2020-02-02' = {
  name: applicationInsightsName
  location: location
  kind: 'web'
  properties: {
    Application_Type: 'web'
    Request_Source: 'rest'
    WorkspaceResourceId: logAnalytics.id
  }
}
output applicationInsightsInstrumentationKey string = applicationInsights.properties.InstrumentationKey

```

```diff:infra/biceps/main.bicep
param location string = resourceGroup().location
param allowedOrigin string
param keyVaultName string
@description('The runtime version of the Azure Functions app.')
param functionsRuntime object

var storageAccountName = '${uniqueString(resourceGroup().id)}azfunctions'
var applicationInsightsName = '${uniqueString(resourceGroup().id)}applicationinsights'
+ var logAnalyticsName = '${uniqueString(resourceGroup().id)}logAnalytics'


resource keyVault 'Microsoft.KeyVault/vaults@2023-07-01' existing = {
  name: keyVaultName
}

module myFunctionsApplicationInsights 'core/host/applications.bicep' = {
  name: 'myFunctionsApplicationInsights'
  params: {
    location: location
    applicationInsightsName: applicationInsightsName
+    logAnalyticsName: logAnalyticsName
  }
}


module myFunctionsStorage 'core/storage/storage-account.bicep' = {
  name: 'myFunctionsStorage'
  params: {
    location: location
    storageAccountName: storageAccountName
  }
}

module myFunctions 'core/host/functions.bicep' = {
  name: 'myFunctions'
  params: {
    location: location
    allowedOrigin: allowedOrigin
    databaseUrl: keyVault.getSecret('AsyncTrpgDatabaseURL')
    storageAccountName: storageAccountName
    kind: functionsRuntime.kind
    linuxFxVersion: functionsRuntime.linuxFxVersion
    applicationInsightsInstrumentationKey: myFunctionsApplicationInsights.outputs.applicationInsightsInstrumentationKey
    extensionVersion: functionsRuntime.extensionVersion
    connectionString: keyVault.getSecret('AsyncTrpgConnectionString')
  }
}

output appServiceAppHostName string = myFunctions.outputs.appServiceAppHostName
```

# 2025.01.03 追記 インストルメンテーション キーから接続文字列への移行
移行した場合、インストルメンテーション キーの環境変数は削除する必要があるので注意すること。

[Application Insights インストルメンテーション キーから接続文字列への移行](https://learn.microsoft.com/ja-jp/azure/azure-monitor/app/migrate-from-instrumentation-keys-to-connection-strings#supported-sdk-versions)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/99876/8566a5f0-8489-4af0-1376-b8abe7f5d50a.png)

[ソースコード(移行コミット)](https://github.com/hibohiboo/async-ttrpg/commit/15f6c62fa727376205da9a27fa5ef5aa5cb394ff)

# 参考
[Microsoft.Insights コンポーネント](https://learn.microsoft.com/ja-jp/azure/templates/microsoft.insights/components?pivots=deployment-language-bicep)
[Quickstart: Create and deploy Azure Functions resources using Bicep](https://learn.microsoft.com/en-us/azure/azure-functions/functions-create-first-function-bicep?tabs=CLI)
[Workspace](https://learn.microsoft.com/ja-jp/azure/templates/microsoft.operationalinsights/workspaces?pivots=deployment-language-bicep)
[Qiita - Application Insights の環境変数について](https://qiita.com/saeyamanashi/items/0065dd5e9fee722ecc03)