azurewebsocketsocket.ioterraformazure-web-pubsub

How to enable Socket.IO in Azure Web PubSub using Terraform?


I'm trying to create an Azure Web PubSub service with Socket.IO support using Terraform. I've tried different approaches but haven't been able to get Socket.IO working properly.

Here's my current configuration, and using like this will only create the Web PubSub as the native one, and not "Web PubSub for Socket.IO":

resource "azurerm_web_pubsub" "websocket-webpubsub" {
  name                = "cca-${var.EnvironmentShort}"
  location            = azurerm_resource_group.wps-rg.location
  resource_group_name = azurerm_resource_group.wps-rg.name
  sku                = "Free_F1"
  capacity           = 1

  identity {
    type = "SystemAssigned"
  }

  live_trace {
    enabled                   = true
    messaging_logs_enabled    = true
    connectivity_logs_enabled = true
  }

  public_network_access_enabled = true

  tags = local.common_tags
}

resource "azurerm_web_pubsub_hub" "socketio" {
  name          = "socketio"
  web_pubsub_id = azurerm_web_pubsub.websocket-webpubsub.id
  event_handler {
    url_template       = "https://${azurerm_linux_web_app.websocket-app.default_hostname}/api/socket"
    user_event_pattern = "*"
    system_events      = ["connect", "connected", "disconnected"]
  }
}
 

I've tried:

However, when I deploy this configuration, the Web PubSub service is created as the native Web PubSub API. Here's some of Terraform and azure docs, but lacks Socket.IO

Terraform Web PubSub docs
Microsoft Terraform WebPubSub docs

Is this the correct approach to enable Socket.IO in Azure Web PubSub using Terraform?
Is there any additional configuration I need to do or is there an alternative way to get this Socket.IO without having to create it directly through Azure portal?


Solution

  • Enable Socket.IO in Azure Web PubSub is not directly supporting to provision using Terraform

    Enabling Socket.IO support in Azure Web PubSub using Terraform is currently not directly supported through the native Terraform azurerm provider due to limitation as a third party provider, instead you can use ARM JSON template as mentioned below.

    ARM template:

    {
      "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
      "contentVersion": "1.0.0.0",
      "parameters": {
        "webPubSubName": {
          "type": "string",
          "metadata": {
            "description": "The name of the Web PubSub resource."
          }
        },
        "location": {
          "type": "string",
          "metadata": {
            "description": "The Azure region for deployment."
          }
        }
      },
      "resources": [
        {
          "type": "Microsoft.SignalRService/webPubSub",
          "apiVersion": "2024-03-01",
          "name": "[parameters('webPubSubName')]",
          "location": "[parameters('location')]",
          "sku": {
            "name": "Free_F1",
            "tier": "Free", 
            "capacity": 1
          },
          "identity": {
            "type": "SystemAssigned"
          },
          "properties": {
            "publicNetworkAccess": "Enabled",
            "liveTraceConfiguration": {
              "enabled": true,
              "categories": [
                {
                  "name": "ConnectivityLogs",
                  "enabled": true
                },
                {
                  "name": "MessagingLogs",
                  "enabled": true
                }
              ]
            }
          }
        }
      ]
    }
    

    If you still want to provision, this setup using terraform then upload this ARM JSON to blob and run to terraform as mentioned below

    Configuration:

    resource "azurerm_storage_blob" "example" {
      name                   = "webpubsub.json"
      storage_account_name   = azurerm_storage_account.example.name
      storage_container_name = azurerm_storage_container.example.name
      type                   = "Block"
      source                 = "${path.module}/webpubsub.json"
    
      depends_on = [azurerm_storage_container.example]
    }
    
    resource "azurerm_resource_group_template_deployment" "example" {
      name                = "WebPubSubDeployment"
      resource_group_name = azurerm_resource_group.example.name
      deployment_mode     = "Incremental"
    
      template_content = file("${path.module}/webpubsub.json")
    
      parameters_content = jsonencode({
        "webPubSubName" = {
          "value" = "cca-dev"
        }
        "location" = {
          "value" = "eastus2"
        }
      })
    
      depends_on = [
        azurerm_storage_blob.example
      ]
    }
    

    Deployment:

    enter image description here

    enter image description here

    enter image description here

    Refer:

    https://learn.microsoft.com/en-us/azure/templates/microsoft.signalrservice/webpubsub?pivots=deployment-language-arm-template

    Create an Azure Web PubSub resource - Azure Web PubSub | Microsoft Learn