Azure-cli: Application Insights resources

Created on 10 Feb 2018  路  14Comments  路  Source: Azure/azure-cli

Is there a roadmap for creating App Insights resources via the CLI without resorting to resource create?

New Service

Most helpful comment

Thanks for the command..
The below should help retrieve application instrumentation keys

az resource show -g $RESOURCEGROUPNAME -n $APPLICATION_NAME --resource-type "Microsoft.Insights/components" --query properties.InstrumentationKey

All 14 comments

For the record, this is how I'm doing it now. But being able to do this in a cleaner way and also having a command to create and retrieve application instrumentation keys would be nice:

    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"}'

Thanks for your interest @rcarmo.

cc/ @mayurid @twitchax

Thanks for the command..
The below should help retrieve application instrumentation keys

az resource show -g $RESOURCEGROUPNAME -n $APPLICATION_NAME --resource-type "Microsoft.Insights/components" --query properties.InstrumentationKey

Thanks for the tips, but any ETA on when there will be an az insights cli command?

Does anyone know how to create an API key through the CLI?

cc @nikmd23, this would be helpful for programmatically setting up an app insights space and referencing the instrumentation key.

This does work. However, when I "connect" the resulting Application Insights to an app service (function app) by using the instrumentation key the Application Map seems not to work. Any ideas on that?

I've looped in the right people at AppInsights to see this thread!

@yugangw-msft
I'm trying following command (Azure CLI version - 2.0.52) but its not working for me:

az resource create --resource-group myresourcegroup --name myappinsights --resource-type Microsoft.Insights/components --location southcentralus --properties '{"Application_Type":"web"}'

Error:
Expecting value: line 1 column 1 (char 0)
Traceback (most recent call last):
File "C:\Users\VSSADM1\AppData\Local\Temp\pip-install-bb1gu7gd\knack\knack\cli.py", line 206, in invoke
File "C:\Users\VSSADM1\AppData\Local\Temp\pip-install-bb1gu7gd\azure-cli-core\azure\cli\core\commands__init__.py", line 328, in execute
File "C:\Users\VSSADM1\AppData\Local\Temp\pip-install-bb1gu7gd\azure-cli-core\azure\cli\core\commands__init__.py", line 386, in _run_jobs_serially
File "C:\Users\VSSADM1\AppData\Local\Temp\pip-install-bb1gu7gd\azure-cli-core\azure\cli\core\commands__init__.py", line 379, in _run_job
File "C:\Users\VSSADM1\AppData\Local\Temp\pip-install-bb1gu7gd\six\six.py", line 693, in reraise
File "C:\Users\VSSADM1\AppData\Local\Temp\pip-install-bb1gu7gd\azure-cli-core\azure\cli\core\commands__init__.py", line 356, in _run_job
File "C:\Users\VSSADM1\AppData\Local\Temp\pip-install-bb1gu7gd\azure-cli-core\azure\cli\core\commands__init__.py", line 171, in __call__
File "C:\Users\VSSADM1\AppData\Local\Temp\pip-install-bb1gu7gd\azure-cli-core\azure\cli\core__init__.py", line 441, in default_command_handler
File "C:\Users\VSSADM1\AppData\Local\Temp\pip-install-bb1gu7gd\azure-cli-resource\azure\cli\command_modules\resource\custom.py", line 754, in create_resource
File "C:\Users\VSSADM1\AppData\Local\Temp\pip-install-bb1gu7gd\azure-cli-resource\azure\cli\command_modules\resource\custom.py", line 1753, in create_resource
File "C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\lib\json__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\lib\json\decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\lib\json\decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Am I missing anything here ?

@kuvinodms It can be really hard to escape the properties JSON correctly. So I tend to save it to a JSON file and then pass it to the Azure CLI with the --properties @props.json syntax

Here's an example using PowerShell and the Azure CLI:

'{"Application_Type":"web"}' | Out-File props.json
az resource create `
    -g $resourceGroup -n $appInsightsName `
    --resource-type "Microsoft.Insights/components" `
    --properties "@props.json"

see my article here

Thank you @markheath, file approach did work for me.

The file approach works just as expected, but if anybody wants to use the command in PowerShell without writing to a file, the JSON can be escaped adding a \ in the double quotes works, like this:

az resource create --resource-group myresourcegroup --name myappinsights --resource-type Microsoft.Insights/components --location southcentralus --properties '{\"Application_Type\":\"web\"}'
(taking the @kuvinodms code as an example)

@NicoSorrenti thanks - I thought I'd tried that, but I'll give it another go.

Closing. PR made from @alexeldeib has taken care of this.
To use it, please run "az extension add -n application-insights", and you will see related commands under az monitor app-insights. Once commands are stabilized, we will move them to CLI's core monitor command module

Was this page helpful?
0 / 5 - 0 ratings