In your temp.json file add the availabilitySetName as a variable (change accordingly):
"variables": {
...
"availabilitySetName": "myAvSet",
...
}
Then add it as a resource:
"resources": [
...
{
"type": "Microsoft.Compute/availabilitySets",
"name": "[variables('availabilitySetName')]",
"apiVersion": "2015-06-15",
"location": "[resourceGroup().location]",
"properties": {}
}
...
Down further still inside "resources", find the virtual machine that you want to add to an availabilitySet and make it dependable on your availabilitySet resource. Just after modifying dependsOn, add it to the properties object.
{
"apiVersion": "2015-06-15",
"type": "Microsoft.Compute/virtualMachines",
...
"dependsOn": [
...
"[concat('Microsoft.Compute/availabilitySets/', variables('availabilitySetName'))]"
...
],
"properties": {
...
"availabilitySet": {
"id": "[resourceId('Microsoft.Compute/availabilitySets',variables('availabilitySetName')) ]"
}
...
}
Update:
When creating a VM from an image, the easiest way is to just create the availability set before deploying the template just like we already do with the network interface. In this case, you only have to reference the resource in "properties" object.
"properties": {
...
"availabilitySet": {
"id": "[resourceId('Microsoft.Compute/availabilitySets', 'myAsName') ]"
}
...