5

I have a resource group in azure which contains a Relay which contains a hybrid connection. I'm trying to deploy another resourcegroup containing a web app which should link the existing hybrid connection in the other resourcegroup.

Performing this task in the azure portal is trivial but since I want to run "complete mode" during my ARM-template deploy I need to do this during deployment.

I cannot find any good documentation for this and lots of answers seems outdated. Is this possible, and if then, how can it be accomplished?

martenolofsson
  • 501
  • 1
  • 4
  • 20

1 Answers1

9

You can use this code to create hybrid connection on Relay:

{
  "name": "[concat(relayName, '/', hybridConnectionName]",
  "type": "Microsoft.Relay/namespaces/hybridConnections",
  "apiVersion": "2017-04-01",      
  "dependsOn": [
    "relayName"
  ],
  "properties": {
    "requiresClientAuthorization": true,
    "userMetadata": [
       {
          "key": "endpoint",
          "value": "google.com:443"
       }
    ]
  },
  "resources": []
}

And then connect it to the web app:

"variables": { 
   "hybridConnectionResourceId": "[resourceId(relayResourceGroup, 'Microsoft.Relay/Namespaces/Hybridconnections', relayName, hybridConnectionName)]"
},
{
  "name": "[concat(webAppName, '/', relayName, '/', hybridConnectionName)]",
  "type": "Microsoft.Web/sites/hybridConnectionNamespaces/relays",
  "apiVersion": "2018-02-01",
  "dependsOn": [
    "webAppName"
  ],
  "location": "[resourceGroup().location]",
  "properties": {
    "serviceBusNamespace": "relayName",
    "relayName": "hybridConnectionName",
    "relayArmUri": "[variables('hybridConnectionResourceId')]",
    "hostName": "[split(json(reference(variables('hybridConnectionResourceId'), '2017-04-01').userMetadata)[0].value, ':')[0]]",
    "port": "[split(json(reference(variables('hybridConnectionResourceId'), '2017-04-01').userMetadata)[0].value, ':')[1]]",
    "sendKeyName": "defaultSender",
    "sendKeyValue": "[listkeys(concat(variables('hybridConnectionResourceId'), '/authorizationRules/defaultSender'), '2017-04-01').primaryKey]"
  }
}

Hope this helps.

Nick Soroka
  • 91
  • 1
  • 3
  • 3
    It helps but be careful : userMedata property must be a json array as string : For example, "userMetadata": " [concat('[{\"key\":\"endpoint\",\"value\":\"', variables('myEndpoint'), '\"}]')]" – christophe.chapron May 07 '19 at 14:40
  • Link above is dead but here's a working one (https://learn.microsoft.com/en-us/azure/templates/microsoft.web/sites/hybridconnectionnamespaces/relays?pivots=deployment-language-bicep) in the namespace `Microsoft.Web sites/hybridConnectionNamespaces/relays` – michal krzych Apr 21 '23 at 10:07