mattruma
mattruma

Reputation: 16687

Add AppInsights support to WebApp or FunctionApp from azure-cli

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

Digging through the documentation now.

Upvotes: 3

Views: 981

Answers (3)

harshityadav95
harshityadav95

Reputation: 83

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

Upvotes: 0

Lloyd Smith
Lloyd Smith

Reputation: 1187

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

Upvotes: 2

Joy Wang
Joy Wang

Reputation: 42163

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

Upvotes: 3

Related Questions