Categories
Azure Microsoft Notification Hubs

Deploying Azure Notification Hub with ARM template and retrieving primary connection string

In this article we will create an ARM template that will deploy a Notification Hub and output the primary connection string.

Sending push notifications to your mobile application can require a lot of development and maintenance. Fortunately in all the Azure products, one will help you to send push notifications cross-platform with minimum efforts: Azure Notification Hubs.

Today we will focus on creating an Azure Resource Manager template to easily setup and deploy a Notification Hub to a resource group. Our ARM template will be created in a new Azure Resource Group deployment project in Visual Studio.

 

Creation

Let’s declare the parameters of the ARM template:

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "notificationHubNamespaceName": {
      "type": "string",
      "defaultValue": "[concat('myNotifHubNs', uniqueString(resourceGroup().id))]"
    },
    "notificationHubName": {
      "type": "string",
      "defaultValue": "[concat('myNotifHub', uniqueString(resourceGroup().id))]"
    },
    "notificationHubSkuName": {
      "type": "string",
      "defaultValue": "free",
      "allowedValues": [
        "free",
        "basic",
        "standard"
      ]
    }
  }
  ...
}
  • notificationHubNamespaceName: the name of the Notification Hub namespace. If no parameter is provided a default name such as myNotifHubNsesf262thj4rr6 is generated.
  • notificationHubName: the name of the Notification Hub. If no parameter is provided a default name such as myNotifHubesf262thj4rr6 is generated.
  • notificationHubSkuName: the pricing tier of the Notification Hub. If no parameter is provided the pricing tier will be free.

 

Now we will declare the resources composed by the Notification Hub namespace and the Notification Hub:

{
  ...
  "resources": [
    {
      "apiVersion": "2014-09-01",
      "name": "[parameters('notificationHubNamespaceName')]",
      "type": "Microsoft.NotificationHubs/namespaces",
      "location": "[resourceGroup().location]",
      "sku": {
        "name": "[parameters('notificationHubSkuName')]"
      },
      "properties": {
        "name": "[parameters('notificationHubNamespaceName')]",
        "namespaceType": "NotificationHub"
      },
      "resources": [
        {
          "apiVersion": "2014-09-01",
          "name": "[concat(parameters('notificationHubNamespaceName'),'/',parameters('notificationHubName'))]",
          "type": "Microsoft.NotificationHubs/namespaces/notificationHubs",
          "location": "[resourceGroup().location]",
          "dependsOn": [
            "[concat('Microsoft.NotificationHubs/namespaces/', parameters('notificationHubNamespaceName'))]"
          ],
          "properties": {
            "name": "[parameters('notificationHubName')]"
          },
          "tags": {
            "displayName": "NotificationHub"
          }
        }
      ],
      "tags": {
        "displayName": "NotificationHubNamespace"
      }
    }
  ]
  ...
}

We can pay attention to several things here:

  • The namespace is declared with the following type: Microsoft.NotificationHubs/namespaces.
  • The hub is declared with the following type: Microsoft.NotificationHubs/namespaces/notificationHubs.
  • The hub depends on the namespace as hubs are always declared under a namespace.

 

And to finish, we will output the primary connection string of the hub: 

{
  ...
  "outputs": {
    "NotificationHubNamespaceConnectionString": {
      "type": "string",
      "value": "[listKeys(resourceId('Microsoft.NotificationHubs/namespaces/notificationHubs/authorizationRules', parameters('notificationHubNamespaceName'), parameters('notificationHubName'), 'DefaultFullSharedAccessSignature'), providers('Microsoft.NotificationHubs', 'namespaces/notificationHubs').apiVersions[0]).primaryConnectionString]"
    }
  }
}

As you can notice, we take advantage of the ARM template function listKeys and the ARM template function providers.

The function providers is useful to get the latest API version for a specific namespace, whereas listkeys is the function that will allow us to get the properties for a specific key name.

By default when a new hub is created, two access keys are created: DefaultListenSharedAccessSignature and DefaultFullSharedAccessSignature. In our template we retrieve the primary connection string for the second one.

 

Example of use

The ARM template is now ready, let’s open a Windows PowerShell and try it:

.\Deploy-AzureResourceGroup.ps1 -ResourceGroupName 'MyResourceGroupName' -ResourceGroupLocation 'canadaeast' -TemplateFile '.\azuredeploy.json'

...

OutputsString      :
                     Name                                      Type                       Value
                     ===============                           =========================  ==========
                     notificationHubNamespaceConnectionString  String                     Endpoint=sb://mynotifhubnsjibxupcseloca.servicebus.windows.net/;SharedAccessKeyName=DefaultFullSharedAccessSignature;SharedAccessKey=b0zOZuqc/mvrcLq8M49mfLomHBw4PH56uazMM/8r22Q=

If everything goes well, you should see the same kind of message as above.

 

To go further

In the template we are outputting the connection string as an example. But in a more advance scenario with a Website you could directly set the application setting that require the connection string like the following:

{
  ...
  "resources": [
    {
      ...
      "resources": [
        {
          "apiVersion": "2015-08-01",
          "name": "appsettings",
          "type": "config",
          "dependsOn": [
            "[resourceId('Microsoft.NotificationHubs/namespaces/notificationHubs', parameters('notificationHubNamespaceName'), parameters('notificationHubName'))]",
            "[resourceId('Microsoft.Web/sites', parameters('myWebsiteName'))]"
          ],
          "properties": {
            "NotificationHubConnectionString": "[listKeys(resourceId('Microsoft.NotificationHubs/namespaces/notificationHubs/authorizationRules', parameters('notificationHubNamespaceName'), parameters('notificationHubName'), 'DefaultFullSharedAccessSignature'), providers('Microsoft.NotificationHubs', 'namespaces/notificationHubs').apiVersions[0]).primaryConnectionString]",
            "NotificationHubPath": "[parameters('notificationHubName')]"
          }
        }
      ]
      ...
    }
  ]
  ...
}

 

Summary

We have seen how to create an ARM template that will deploy an Azure Notification Hub and output the primary connection string.

 

You can download the example solution here:

Download full sources

Or

Browse the GitHub repository

 

Please feel free to comment or contact me if you have any question about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *