Question:
Imagine you are tasked with deploying a web application on Azure App Service. As part of the task, you are also expected to set up Application Insights for monitoring the performance and usage data of the application, located in resource group $resourceGroup and app service plan $appServicePlan in location $location. All of these activities should be performed using the Azure CLI. How would you go about accomplishing this?
appName="MyWebApp"
appInsightsName="MyAppInsights"
Answer:
appName="MyWebApp"
appInsightsName="MyAppInsights"
# Create a web app
az webapp create --name $appName --resource-group $resourceGroup --plan $appServicePlan
# Create a new Application Insights resource in the resource group
az monitor app-insights component create --app $appInsightsName --location "$location" --resource-group $resourceGroup
# Enable Application Insights for the web app
az webapp config appsettings set --name $appName --resource-group $resourceGroup --settings APPINSIGHTS_INSTRUMENTATIONKEY="$(az monitor app-insights component show --app $appInsightsName --resource-group $resourceGroup --query instrumentationKey --output tsv)"
In the last command, we are using the Azure CLI to get the Instrumentation Key of the newly created Application Insights resource. The --query parameter allows us to specify the data to extract, and --output tsv is used to format the output as Tab-separated values, which gives us a clean output to use in setting the APPINSIGHTS_INSTRUMENTATIONKEY setting.