Provision Azure Notification Hub with Azure Resource Manager Templates

20/02/2016

This weeks ARM challenge was to provision an Azure Notification hub, with client credentials for Google Cloud Messaging (GCM) as part of a larger ARM template. Unfortunately how to do so is not documented, but we are not completely out of luck because the schema for the notification hub is available in the azure-resoruce-manager-schemas git hub repository.

Having the schema definition is quite a big help, and it lets us author the template without much trouble (at least for the GCM notification type).

Here is the template I ended up using
[js]
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"Gcm.GoogleApiKey": {
"type": "string",
"metadata": {
"description": "Google Cloud Messaging API Key"
},
"defaultValue": "AIzaSyAyp9MernKgMS3wFNM3yNWByiP-TaGrqEg"
},
},
"variables": {
"hubVersion": "[providers(‘Microsoft.NotificationHubs’, ‘namespaces’).apiVersions[0]]",
"notificationHubNamespace": "[concat(‘hubv2’, uniqueString(resourceGroup().id))]",
"notificationHubName": "notificationhub"
},
"resources": [
{
"name": "[variables(‘NotificationHubNamespace’)]",
"location": "[resourceGroup().location]",
"type": "Microsoft.NotificationHubs/namespaces",
"apiVersion": "[variables(‘hubVersion’)]",
"comments": "Notification hub namespace",
"properties": {
"namespaceType": "NotificationHub"
},
"resources": [
{
"name": "[concat(variables(‘NotificationHubNamespace’),’/’,variables(‘NotificationHubName’))]",
"location": "[resourceGroup().location]",
"type": "Microsoft.NotificationHubs/namespaces/notificationHubs",
"apiVersion": "[variables(‘hubVersion’)]",
"properties": {
"GcmCredential": {
"properties": {
"googleApiKey": "[parameters(‘Gcm.GoogleApiKey’)]",
"gcmEndpoint": "https://android.googleapis.com/gcm/send"
}
}
},
"dependsOn": [
"[concat(‘Microsoft.NotificationHubs/namespaces/’, variables(‘NotificationHubNamespace’))]"
]
}
]
}
],
"outputs": {
}
}
[/js]

There is not much to this basic template, all you need to know it to use the resource type Microsoft.NotificationHubs/namespaces, to create a notification hub namespace. And then the nested resource should look like, to create the actual hub.

[js]
{
"name": "[concat(variables(‘NotificationHubNamespace’),’/’,variables(‘NotificationHubName’))]",
"location": "[resourceGroup().location]",
"type": "Microsoft.NotificationHubs/namespaces/notificationHubs",
"apiVersion": "[variables(‘hubVersion’)]",
"properties": {
"GcmCredential": {
"properties": {
"googleApiKey": "[parameters(‘Gcm.GoogleApiKey’)]",
"gcmEndpoint": "https://android.googleapis.com/gcm/send"
}
}
},
"dependsOn": [
"[concat(‘Microsoft.NotificationHubs/namespaces/’, variables(‘NotificationHubNamespace’))]"
]
}
[/js].

For GCM credentials, the properties should contain
[js]
"GcmCredential": {
"properties": {
"googleApiKey": "[parameters(‘Gcm.GoogleApiKey’)]",
"gcmEndpoint": "https://android.googleapis.com/gcm/send"
}
}
[/js]
Note that the googleApiKey should be a correct one as the ARM resource provider actually do some input validation.

If you want to try to use some of the other messaging systems that notification hub supports, you can find the properties in the aforementioned schema. I tried getting the apple push notification service configured as well, but it requires a real certificate from apple that I don’t currently have. Once I have one, I will post a follow up.

[js]
"apnsCredential": {
"properties": {
"apnsCertificate": "[parameters(‘APNS.Certificate’)]",
"certificateKey": "[parameters(‘APNS.certificateKey’)]",
"endpoint": " gateway.sandbox.push.apple.com or gateway.push.apple.com",
}
}
[/js]

You can grab my entire project from github https://github.com/sjkp/ARM.NotificationHub