3

Is there a way to create and/or activate AppInsights for a WebApp or FunctionApp from the azure-cli?

Digging through the documentation now.

mattruma
  • 16,589
  • 32
  • 107
  • 171

3 Answers3

3

I have also thought about your issue ago. To create an Application insight, it seems that the az resource create will be the only possible way currently as mentioned in the another answer. But I am not sure it 100% works, you could have a try.

To activate Application insight for a WebApp or FunctionApp, we just need to add a setting called APPINSIGHTS_INSTRUMENTATIONKEY in the Application settings of WebApp or FunctionApp. Just use the command below, it works fine on my side.

#retrive the InstrumentationKey of your application insight
$key = az resource show -g <resource group name> -n <appinsight name> --resource-type "Microsoft.Insights/components" --query properties.InstrumentationKey

#set the application insight for function app
az functionapp config appsettings set -g <resource group name> -n <function app name> --settings "APPINSIGHTS_INSTRUMENTATIONKEY = $key"

#set the application insight for web app
az webapp config appsettings set -g <resource group name> -n <web app name> --settings "APPINSIGHTS_INSTRUMENTATIONKEY = $key"

Test sample( the result of web app is the same):

enter image description here Check in the portal:

enter image description here

Joy Wang
  • 39,905
  • 3
  • 30
  • 54
  • If AppInsights key already in the function, the az functionapp config command fails. Is there a workaround? – Sai Dec 03 '19 at 18:17
2

There is currently an open issue for creating an app insights resource via cli https://github.com/Azure/azure-cli/issues/5543

You can see the user is creating them like so currently

az resource create \
    --resource-group $RESOURCE_GROUP \
    --resource-type "Microsoft.Insights/components" \
    --name $NAMESPACE_PREFIX-appinsights \
    --location $PRIMARY_LOCATION \
    --properties '{"ApplicationId":"facerecognition","Application_Type":"other", "Flow_Type":"Redfield", "Request_Source":"IbizaAIExtension"}'

================================================================

According to the latest CLI documentation (https://learn.microsoft.com/en-us/cli/azure/ext/application-insights/monitor/app-insights?view=azure-cli-latest) , you can create a new App Insight resource by using the following

az monitor app-insights component create --app demoApp --location westus2 --kind web -g demoRg --application-type web
Lloyd Smith
  • 1,187
  • 12
  • 21
0

Similarly connection string for application insight can be fetched using Az cli

az resource show -g $RESOURCE_GROUP -n $APP_INSIGHTS --resource-type "microsoft.insights/components" --query properties.ConnectionString