2

I have a Bicep template to create an Azure SignalR Service per the following script. How can I obtain the upstream's code value within the bicep template and populate the urlTemplate's code value based on it? (the keyword TBD exhibits the exact spot in the following code.)

targetScope = 'resourceGroup'
param location string = resourceGroup().location
param signalRName string

resource signalR 'Microsoft.SignalRService/signalR@2022-02-01' = {
  name: signalRName
  location: location
  kind: 'SignalR'
  sku: {
    name: 'Free_F1'
    tier: 'Free'
    capacity: 1
  }
  properties: {
    features: [
      {
        flag: 'ServiceMode'
        value: 'Serverless'
      }
      {
        flag: 'EnableConnectivityLogs'
        value: 'true'
      }
      {
        flag: 'EnableMessagingLogs'
        value: 'true'
      }
      {
        flag: 'EnableLiveTrace'
        value: 'true'
      }
    ]
    liveTraceConfiguration: {
      enabled: 'true'
      categories: [
        {
          name: 'ConnectivityLogs'
          enabled: 'true'
        }
        {
          name: 'MessagingLogs'
          enabled: 'true'
        }
        {
          name: 'HttpRequestLogs'
          enabled: 'true'
        }
      ]
    }
    cors: {
      allowedOrigins: [
        '*'
      ]
    }
    upstream: {
      templates: [
        {
          hubPattern: '*'
          eventPattern: '*'
          categoryPattern: '*'
          auth: {
            type: 'None'
          }
          urlTemplate: 'https://${signalRName}.azurewebsites.net/runtime/webhooks/signalr?code=TBD'
        }
      ]
    }
  }
}

Thomas
  • 24,234
  • 6
  • 81
  • 125
Arash
  • 3,628
  • 5
  • 46
  • 70

1 Answers1

2

I'm assuming that you are using a function app that has the same name as your signalR service.

Looking at the documentation:

  • The url always looks like that <Function_App_URL>/runtime/webhooks/signalr?code=<API_KEY>
  • the API_KEY is a function app systemkey called signalr_extension.

So you should be able to retrieve the key and use it like that:

var signalRKey = listKeys(resourceId('Microsoft.Web/sites/host', signalRName, 'default'), '2022-03-01').systemkeys.signalr_extension

resource signalR 'Microsoft.SignalRService/signalR@2022-02-01' = {
  name: signalRName
  ...
  properties: {
    ...
    upstream: {
      templates: [
        {
          ...
          urlTemplate: 'https://${signalRName}.azurewebsites.net/runtime/webhooks/signalr?code=${signalRKey}'
        }
      ]
    }
  }
}
Thomas
  • 24,234
  • 6
  • 81
  • 125
  • There is a problem though: I'm creating the function app in the bicep prior to creating the signalr resource (of course the dependency is declared). The or signalr_extension is not populated and seen on azure portal. Creating the signalR resource fails due to this lack. – Arash May 24 '22 at 13:18
  • Ho yeah the signalr_extension is created once the code is deployed. – Thomas May 24 '22 at 19:52
  • so, technically I cannot populate `urlTemplate` when running the bicep template, can I? In that case, that will be a manual process to setup the signalr extension through the portal, right? – Arash May 24 '22 at 20:10
  • I'm wondering if you can force creating `hostkey` when de ploying the functionapp using bicep. let me check that – Thomas May 24 '22 at 20:23
  • is there a way? I doubt. – Arash May 24 '22 at 23:12
  • I made the changes per your updated answer but I started getting the following error when running it: Warning BCP081: Resource type "Microsoft.Web/sites/host/systemkeys@2021-03-01" does not have types available. – Arash May 25 '22 at 11:28
  • the deployment failed due to the following error message: Encountered an error (ServiceUnavailable) from host runtime. – Arash May 25 '22 at 11:53
  • which function runtime, language and OS are you using? I tried on a function app v4 + .net + windows. – Thomas May 25 '22 at 20:02
  • .NET 6.0, V4 and Linux. – Arash May 25 '22 at 20:12
  • 1
    ok will try with linux then – Thomas May 25 '22 at 20:27