Creating application insights with azure cli

If you need to associate the generated instrumentation key with another resource, such as a function app, you can use grep and xargs as follows:

# Creates insights component for monitoring. Note generated instrumentation key
# is set in function app.
az resource create \
    --resource-group ${RESOURCE_GROUP_NAME} \
    --resource-type "Microsoft.Insights/components" \
    --name ${FUNCTION_APP_NAME} \
    --location ${LOCATION} \
    --properties '{"Application_Type":"web"}' \
| grep -Po "\"InstrumentationKey\": \K\".*\"" \
| xargs -I % az functionapp config appsettings set \
    --name ${FUNCTION_APP_NAME} \
    --resource-group ${RESOURCE_GROUP_NAME} \
    --settings "APPINSIGHTS_INSTRUMENTATIONKEY = %"

The az monitor app-insights component provide commands for creating, inspecting modifying, and deleting application insights components from the command line.


The link provided by Rohit works

az resource create \
    --resource-group $RESOURCE_GROUP \
    --resource-type "Microsoft.Insights/components" \
    --name $NAMESPACE_PREFIX-appinsights \
    --location $PRIMARY_LOCATION \
    --properties '{"Application_Type":"web"}'

https://github.com/Azure/azure-cli/issues/5543#issuecomment-365001620