I recently started using the Azure command-line interface (CLI) as my tool of choice for Infrastructure as Code. Before the CLI I was using Azure Resource Manager Templates (ARM). I find ARM difficult to author. Of course, there are tradeoffs and once the authoring of ARM is fixed, I think I will return. But until then I want to share some things I have learned while experimenting with the CLI.
While preparing a demo for VS Live I was disappointed when I could not find a why to create an Application Insights resource using CLI. I was sharing my frustration with Steven Murawski when he told me that is was possible to create an Application Insights resource using the CLI and showed me some sample code.
The key was the az resource command. Using this command, you can create any resource you want. The catch is you need to know the string that represents the resource type and the properties required to create it. Thankfully Jim Reid shared a link to all the Azure Resource definitions. There you can find the names for the resource type property. Following the links to the quick starts you can find ARM templates to use to figure out the required properties.
Below is a PowerShell example of creating an Application Insights resource and storing the instrumentation key for later use.
###############################################################################
# This creates the application insights and stores the InstrumentationKey in
# an environment variable of the same name.
Write-Output "Creating the application insights resource for $webSiteName."
az resource create --name $webSiteName `
--resource-group $resourceGroupName `
--resource-type "Microsoft.Insights/components" `
--location westus2 `
--properties "{""""ApplicationId"""": """"$webSiteName""""}" `
--output $output
$instrumentationKey = $(az resource show --name $webSiteName `
--resource-group $resourceGroupName `
--namespace Microsoft.Insights `
--resource-type components `
--query properties.InstrumentationKey `
--output tsv)
Write-Verbose "Instrumentation Key = $instrumentationKey"