Creating an Azure OMS workspace in less than 30 seconds

If you want to play with OMS it might be helpful and necessary to create new workspaces automatically (and remove them after testing). I am currently playing with the Log analytics http data API and need to create OMS workspaces quite often.
This is how I do it:

There are multiple ways of creating resources in Azure. The fastest way IMHO is to use a JSON resource template. Creating such a template is not always that easy, but a simple OMS workspace can simply look like that:

 {
 "$schema" : "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json",
 "contentVersion" : "1.0.0.0",
 "parameters" : {
 "omsWorkspaceName" : {
 "type" : "string",
 "defaultValue" : "MyWorkspace",
 "metadata" : {
 "description" : "Assign a name for the Log Analytic Workspace Name"
 }
 },
 "omsWorkspaceRegion" : {
 "type" : "string",
 "defaultValue" : "West Europe",
 "allowedValues" : [ "East US", "West Europe", "Southeast Asia", "Australia Southeast" ],
 "metadata" : {
 "description" : "Specify the region for your Workspace"
 }
 }
 },
 "variables" : {},
 "resources" : [
 {
 "apiVersion" : "2015-11-01-preview",
 "location" : "[parameters('omsWorkspaceRegion')]",
 "name" : "[parameters('omsWorkspaceName')]",
 "type" : "Microsoft.OperationalInsights/workspaces",
 "comments" : "Log Analytics workspace",
 "properties" : {
 "sku" : {
 "name" : "Free"
 }
 }
 }
 ],
 "outputs" : {}
 }

If you want to dive deeper into OMS JSON templates have a look here: https://azure.microsoft.com/en-us/documentation/articles/log-analytics-template-workspace-configuration/

To create the workspace via Azure PowerShell, you simply need to

  • save this template as a JSON file (e.g. e:\OMSSimpleWorkspace.json)
  • Create a new ResourceGroup (not really needed, but simplifies dumping the whole thing after testing)
  • Create a new deployment based on the JSON template

The code I use looks like that:

 #Login-AzureRmAccount if needed
#Get and set some general variables
$RGName = read-host "Enter new Resource Group name"
$WorkspaceName = read-host "Enter new OMS workspace name"
$DeploymentName = "Deploy{0}" -f $WorkspaceName
$Location = "West Europe"
$OMSTemplateFile = "e:\OMSSimpleWorkspace.json"
$Subscription = get-azurermsubscription | Out-GridView -PassThru
Select-AzureRmSubscription -SubscriptionId $Subscription.SubscriptionID

#Create new ResourceGroup and new OMS workspace
New-AzureRmResourceGroup -Name $RGName  -Location $Location
$RG = Get-AzureRmResourceGroup -Name $RGName
#Deploy the JSON template and use the specified variables within straight in the PowerShell Cmdlet
New-AzureRmResourceGroupDeployment -Name $DeploymentName -ResourceGroupName $RGName -TemplateFile $OMSTemplateFile -omsworkspacename $WorkspaceName -omsworkspaceregion $Location

#Optional: Get your WorkspaceID and Keys for later use (e.g. sending data via http API)
$WorkSpace = Get-AzureRmOperationalInsightsWorkspace -Name  $WorkspaceName -ResourceGroupName $RGName
$Keys = Get-AzureRmOperationalInsightsWorkspaceSharedKeys -ResourceGroupName  $RGName -Name $WorkspaceName

 

The whole script should take less than 30 seconds to run and your OMS workspace is ready to use!

Once you are done with testing, simply dump your ResourceGroup:

 PS> Remove-AzureRmResourceGroup -Name $RG.ResourceGroupName -force