Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output an IotHub endpoint with Azure Resource Manager template

I am about to script the deployment of our Azure solution. For that reason I create an Azure IoTHub with a Resource Manager Template. This works very well. But the problem is, I need the Event Hub-compatible endpoint string for further deployments.

See: https://picload.org/image/rrdopcia/untitled.png

I think, the solution would be, to output it in the template, but I cant get it to work.

The output-section of my template.json actually looks like this:

    "outputs": {
    "clusterProperties": {
        "value": "[reference(parameters('clusterName'))]",
        "type": "object"
    },
    "iotHubHostName": {
        "type": "string",
        "value": "[reference(variables('iotHubResourceId')).hostName]"
    },
    "iotHubConnectionString": {
        "type": "string",
        "value": "[concat('HostName=', reference(variables('iotHubResourceId')).hostName, ';SharedAccessKeyName=', variables('iotHubKeyName'), ';SharedAccessKey=', listkeys(variables('iotHubKeyResource'), variables('iotHubVersion')).primaryKey)]"
    }
   }

And here are the variables I used:

    "variables": {
    "iotHubVersion": "2016-02-03",
    "iotHubResourceId": "[resourceId('Microsoft.Devices/Iothubs', parameters('iothubname'))]",
    "iotHubKeyName": "iothubowner",
    "iotHubKeyResource": "[resourceId('Microsoft.Devices/Iothubs/Iothubkeys', parameters('iothubname'), variables('iotHubKeyName'))]",
},
like image 206
ErBeEn Avatar asked Jul 11 '16 05:07

ErBeEn


1 Answers

You can read the endpoint from the provisioned IoT Hub within the ARM template and build a connection string like this:

"EventHubConnectionString": "[concat('Endpoint=',reference(resourceId('Microsoft.Devices/IoTHubs',parameters('iothub_name'))).eventHubEndpoints.events.endpoint,';SharedAccessKeyName=iothubowner;SharedAccessKey=',listKeys(resourceId('Microsoft.Devices/IotHubs',parameters('iothub_name')),variables('devices_provider_apiversion')).value[0].primaryKey)]"

The important bit to get the EventHub-compatible endpoint was: resourceId('Microsoft.Devices/IoTHubs', parameters('iothub_name'))).eventHubEndpoints.events.endpoint

That was ripped out of my working ARM template. For clarity, here are some details about the variables/parameters in the above:

  1. variables('devices_provider_apiversion') is "2016-02-03"
  2. parameters('iothub_name') is the name of the IoT Hub that same ARM template is provisioning elsewhere in the template
  3. The output of "listKeys" returns a array of key objects, where in my case the first item was "iothubowner". (... I like the approach to get this described in the question better. :)

One helpful trick that helped me learn what is available for me to read from the resources during execution of the ARM template is to output the entire resource and then find the property I am interested in. Here is how I output all details of the IoT Hub from running the ARM template:

"outputs": {
    "iotHub": {
        "value": "[reference(resourceId('Microsoft.Devices/IoTHubs',parameters('iothub_name')))]",
        "type": "object"
    }
}

You can also use this method to output the endpoint (among other things) to be used as input to other templates.

like image 133
bojingo Avatar answered Nov 19 '22 14:11

bojingo