|
| 1 | +--- |
| 2 | +title: Tutorial - Configure Event Hub for Azure Cloud HSM |
| 3 | +description: Learn how to configure Azure Event Hubs as a destination for Azure Cloud HSM operation event logs for real-time streaming and downstream processing. |
| 4 | +author: keithp |
| 5 | +manager: keithp |
| 6 | +ms.service: azure-cloud-hsm |
| 7 | +ms.topic: tutorial |
| 8 | +ms.date: 04/08/2026 |
| 9 | +ms.author: keithp |
| 10 | + |
| 11 | +#Customer Intent: As an IT pro, I want to stream Azure Cloud HSM operation logs to Event Hub for real-time processing and integration with downstream systems. |
| 12 | + |
| 13 | +--- |
| 14 | + |
| 15 | +# Tutorial: Configure Event Hub for Azure Cloud HSM |
| 16 | + |
| 17 | +If you configured operation event logging for Azure Cloud HSM, you already have a working diagnostic setting on your Cloud HSM cluster that routes `HsmServiceOperations` logs to Storage and Log Analytics. Adding Event Hub is simply adding a third destination to that same diagnostic setting or creating a new one that targets Event Hub. |
| 18 | + |
| 19 | +Azure Monitor diagnostic settings support multiple destinations simultaneously. Since your Log Analytics pipeline already proves that log emission from Cloud HSM is working, Event Hub becomes another sink receiving the same `HsmServiceOperations` category. |
| 20 | + |
| 21 | +In this tutorial, you: |
| 22 | + |
| 23 | +> [!div class="checklist"] |
| 24 | +> |
| 25 | +> - Create an Event Hub namespace and event hub for Cloud HSM logs. |
| 26 | +> - Configure authorization rules with least-privilege permissions. |
| 27 | +> - Update diagnostic settings to stream logs to Event Hub. |
| 28 | +> - Verify that Event Hub receives Cloud HSM operation events. |
| 29 | +
|
| 30 | +## Prerequisites |
| 31 | + |
| 32 | +- An Azure Cloud HSM resource that's deployed and activated. For more information, see the [Azure Cloud HSM onboarding guide](onboarding-guide.md). |
| 33 | +- Diagnostic settings emitting operation event logs to Storage and Log Analytics. For more information, see [Configure and query operation event logging for Azure Cloud HSM](tutorial-operation-event-logging.md). |
| 34 | +- `Contributor` or `Monitoring Contributor` role on the Cloud HSM resource group. |
| 35 | + |
| 36 | +## Verify the logs resource group exists |
| 37 | + |
| 38 | +Event Hub should deploy into the same resource group that contains your storage account and Log Analytics workspace for operational event logging. If you didn't set up operational event logging, first follow the guidance in [Configure and query operation event logging for Azure Cloud HSM](tutorial-operation-event-logging.md). |
| 39 | + |
| 40 | +Verify that your targeted resource group exists: |
| 41 | + |
| 42 | +```azurepowershell |
| 43 | +az group show --name "<resource-group>" --query "{name:name, location:location}" --output table |
| 44 | +``` |
| 45 | + |
| 46 | +## Create an Event Hubs namespace |
| 47 | + |
| 48 | +The namespace is the container that holds one or more event hubs. Use the **Standard** tier, which is required for diagnostic settings integration: |
| 49 | + |
| 50 | +```azurepowershell |
| 51 | +az eventhubs namespace create ` |
| 52 | + --name "<eventhub-namespace>" ` |
| 53 | + --resource-group "<resource-group>" ` |
| 54 | + --location "<location>" ` |
| 55 | + --sku Standard ` |
| 56 | + --capacity 1 ` |
| 57 | + --enable-auto-inflate false |
| 58 | +``` |
| 59 | + |
| 60 | +Key options: |
| 61 | + |
| 62 | +- `--sku Standard`: The Basic tier doesn't support diagnostic settings as a destination. |
| 63 | +- `--capacity 1`: One throughput unit (1 MB/s ingress, 2 MB/s egress) is sufficient for HSM audit logs. |
| 64 | +- `--enable-auto-inflate false`: HSM log volume is low, so auto-inflate isn't necessary. |
| 65 | + |
| 66 | +## Create an event hub inside the namespace |
| 67 | + |
| 68 | +Create an event hub (topic) to receive the Cloud HSM logs: |
| 69 | + |
| 70 | +```azurepowershell |
| 71 | +az eventhubs eventhub create ` |
| 72 | + --name "cloudhsm-logs" ` |
| 73 | + --namespace-name "<eventhub-namespace>" ` |
| 74 | + --resource-group "<resource-group>" ` |
| 75 | + --partition-count 2 ` |
| 76 | + --retention-time-in-hours 168 ` |
| 77 | + --cleanup-policy Delete |
| 78 | +``` |
| 79 | + |
| 80 | +Key options: |
| 81 | + |
| 82 | +- `--partition-count 2`: Two partitions are sufficient for HSM audit log throughput. |
| 83 | +- `--retention-time-in-hours 168`: Keep messages for 7 days (168 hours, which is the maximum for Standard tier). |
| 84 | +- `--cleanup-policy Delete`: Delete messages after the retention period expires. |
| 85 | + |
| 86 | +## Create a consumer group |
| 87 | + |
| 88 | +Create a dedicated consumer group for downstream processing. Reserve the default `$Default` group for other uses: |
| 89 | + |
| 90 | +```azurepowershell |
| 91 | +az eventhubs eventhub consumer-group create ` |
| 92 | + --name "azure-cloud-hsm" ` |
| 93 | + --namespace-name "<eventhub-namespace>" ` |
| 94 | + --eventhub-name "cloudhsm-logs" ` |
| 95 | + --resource-group "<resource-group>" |
| 96 | +``` |
| 97 | + |
| 98 | +## Create an authorization rule |
| 99 | + |
| 100 | +Diagnostic settings need **Send** permission to push logs into the event hub. Create a shared access policy with only the required permission: |
| 101 | + |
| 102 | +```azurepowershell |
| 103 | +az eventhubs namespace authorization-rule create ` |
| 104 | + --name "DiagnosticSettingsSendRule" ` |
| 105 | + --namespace-name "<eventhub-namespace>" ` |
| 106 | + --resource-group "<resource-group>" ` |
| 107 | + --rights Send |
| 108 | +``` |
| 109 | + |
| 110 | +> [!NOTE] |
| 111 | +> This rule grants only `Send` permission, not `Listen` or `Manage`. Follow the principle of least privilege. Your downstream consumers (such as Azure Functions or Stream Analytics) should use a separate rule with `Listen` permission. |
| 112 | +
|
| 113 | +## Get the authorization rule resource ID |
| 114 | + |
| 115 | +Retrieve the authorization rule resource ID for use in the diagnostic setting: |
| 116 | + |
| 117 | +```azurepowershell |
| 118 | +$authRuleId = az eventhubs namespace authorization-rule show ` |
| 119 | + --name "DiagnosticSettingsSendRule" ` |
| 120 | + --namespace-name "<eventhub-namespace>" ` |
| 121 | + --resource-group "<resource-group>" ` |
| 122 | + --query id --output tsv |
| 123 | +
|
| 124 | +Write-Host "Auth Rule ID: $authRuleId" |
| 125 | +``` |
| 126 | + |
| 127 | +Save this value for use in the next step. |
| 128 | + |
| 129 | +## Update the diagnostic setting to add Event Hub |
| 130 | + |
| 131 | +You have two options for adding Event Hub as a destination: |
| 132 | + |
| 133 | +### Option A: Update the existing diagnostic setting (recommended) |
| 134 | + |
| 135 | +This approach updates your existing diagnostic setting to add Event Hub while keeping Storage and Log Analytics: |
| 136 | + |
| 137 | +```azurepowershell |
| 138 | +# Set your resource group variables |
| 139 | +$hsmResourceGroup = "<resource-group>" |
| 140 | +$logsResourceGroup = "<resource-group>" |
| 141 | +
|
| 142 | +# Find the HSM cluster name (auto-generated during deployment) |
| 143 | +$hsmClusterName = az resource list ` |
| 144 | + --resource-group $hsmResourceGroup ` |
| 145 | + --resource-type Microsoft.HardwareSecurityModules/cloudHsmClusters ` |
| 146 | + --query "[0].name" --output tsv |
| 147 | +Write-Host "HSM Cluster: $hsmClusterName" |
| 148 | +
|
| 149 | +# Get the HSM cluster resource ID |
| 150 | +$hsmResourceId = az resource show ` |
| 151 | + --resource-group $hsmResourceGroup ` |
| 152 | + --resource-type Microsoft.HardwareSecurityModules/cloudHsmClusters ` |
| 153 | + --name $hsmClusterName ` |
| 154 | + --query id --output tsv |
| 155 | +
|
| 156 | +# Get your existing storage account ID |
| 157 | +$storageAccountId = az storage account list ` |
| 158 | + --resource-group $logsResourceGroup ` |
| 159 | + --query "[0].id" --output tsv |
| 160 | +
|
| 161 | +# Get your existing Log Analytics workspace ID |
| 162 | +$workspaceId = az monitor log-analytics workspace list ` |
| 163 | + --resource-group $logsResourceGroup ` |
| 164 | + --query "[0].id" --output tsv |
| 165 | +
|
| 166 | +# Get the Event Hub auth rule ID |
| 167 | +$authRuleId = az eventhubs namespace authorization-rule show ` |
| 168 | + --name "DiagnosticSettingsSendRule" ` |
| 169 | + --namespace-name "<eventhub-namespace>" ` |
| 170 | + --resource-group $logsResourceGroup ` |
| 171 | + --query id --output tsv |
| 172 | +
|
| 173 | +# Update the diagnostic setting with all three destinations |
| 174 | +az monitor diagnostic-settings create ` |
| 175 | + --name "<diagnostic-setting-name>" ` |
| 176 | + --resource $hsmResourceId ` |
| 177 | + --storage-account $storageAccountId ` |
| 178 | + --workspace $workspaceId ` |
| 179 | + --event-hub "cloudhsm-logs" ` |
| 180 | + --event-hub-rule $authRuleId ` |
| 181 | + --logs '[{\"category\":\"HsmServiceOperations\",\"enabled\":true}]' |
| 182 | +``` |
| 183 | + |
| 184 | +> [!IMPORTANT] |
| 185 | +> The `az monitor diagnostic-settings create` command performs an upsert operation. If the name matches an existing setting, it replaces the setting entirely. You must include `--storage-account` and `--workspace` again, or those destinations are removed. |
| 186 | +
|
| 187 | +### Option B: Create a separate diagnostic setting for Event Hub only |
| 188 | + |
| 189 | +If you prefer to keep your existing setting unchanged and add a second one: |
| 190 | + |
| 191 | +```azurepowershell |
| 192 | +# Find the HSM cluster name (if you don't already have it from Option A) |
| 193 | +$hsmClusterName = az resource list ` |
| 194 | + --resource-group "<resource-group>" ` |
| 195 | + --resource-type Microsoft.HardwareSecurityModules/cloudHsmClusters ` |
| 196 | + --query "[0].name" --output tsv |
| 197 | +
|
| 198 | +$hsmResourceId = az resource show ` |
| 199 | + --resource-group "<resource-group>" ` |
| 200 | + --resource-type Microsoft.HardwareSecurityModules/cloudHsmClusters ` |
| 201 | + --name $hsmClusterName ` |
| 202 | + --query id --output tsv |
| 203 | +
|
| 204 | +$authRuleId = az eventhubs namespace authorization-rule show ` |
| 205 | + --name "DiagnosticSettingsSendRule" ` |
| 206 | + --namespace-name "<eventhub-namespace>" ` |
| 207 | + --resource-group "<resource-group>" ` |
| 208 | + --query id --output tsv |
| 209 | +
|
| 210 | +az monitor diagnostic-settings create ` |
| 211 | + --name "chsm-eventhub-diagnostic-setting" ` |
| 212 | + --resource $hsmResourceId ` |
| 213 | + --event-hub "cloudhsm-logs" ` |
| 214 | + --event-hub-rule $authRuleId ` |
| 215 | + --logs '[{\"category\":\"HsmServiceOperations\",\"enabled\":true}]' |
| 216 | +``` |
| 217 | + |
| 218 | +> [!NOTE] |
| 219 | +> Azure supports up to five diagnostic settings per resource. A second setting is valid and keeps concerns separated. |
| 220 | +
|
| 221 | +## Verify Event Hub is receiving messages |
| 222 | + |
| 223 | +After you configure the diagnostic setting, verify that Event Hub is receiving Cloud HSM logs. |
| 224 | + |
| 225 | +### Check the diagnostic setting in the portal |
| 226 | + |
| 227 | +1. In the Azure portal, go to your Cloud HSM cluster. |
| 228 | +1. Under **Monitoring**, select **Diagnostic settings**. |
| 229 | +1. Confirm that Event Hub is listed as a destination. |
| 230 | + |
| 231 | +### Check Event Hub metrics |
| 232 | + |
| 233 | +Run the following command to check incoming messages over the last hour: |
| 234 | + |
| 235 | +```azurepowershell |
| 236 | +# Get your subscription ID |
| 237 | +$subId = az account show --query id --output tsv |
| 238 | +
|
| 239 | +# Check incoming messages (last 1 hour) |
| 240 | +az monitor metrics list ` |
| 241 | + --resource "/subscriptions/$subId/resourceGroups/<resource-group>/providers/Microsoft.EventHub/namespaces/<eventhub-namespace>" ` |
| 242 | + --metric "SuccessfulRequests" ` |
| 243 | + --interval PT1H ` |
| 244 | + --output table |
| 245 | +``` |
| 246 | + |
| 247 | +### Peek at messages (optional) |
| 248 | + |
| 249 | +If you want to read a few messages to confirm content, create a **Listen** rule: |
| 250 | + |
| 251 | +```azurepowershell |
| 252 | +# Create a Listen rule for your consumer |
| 253 | +az eventhubs namespace authorization-rule create ` |
| 254 | + --name "ConsumerListenRule" ` |
| 255 | + --namespace-name "<eventhub-namespace>" ` |
| 256 | + --resource-group "<resource-group>" ` |
| 257 | + --rights Listen |
| 258 | +
|
| 259 | +# Get the connection string |
| 260 | +az eventhubs namespace authorization-rule keys list ` |
| 261 | + --name "ConsumerListenRule" ` |
| 262 | + --namespace-name "<eventhub-namespace>" ` |
| 263 | + --resource-group "<resource-group>" ` |
| 264 | + --query primaryConnectionString --output tsv |
| 265 | +``` |
| 266 | + |
| 267 | +You can use this connection string with Azure Event Hub Explorer, the VS Code Event Hub extension, or a Python script to peek at messages. |
| 268 | + |
| 269 | +## Related content |
| 270 | + |
| 271 | +- [Configure and query operation event logging for Azure Cloud HSM](tutorial-operation-event-logging.md) |
| 272 | +- [Azure Event Hubs documentation](/azure/event-hubs/event-hubs-about) |
| 273 | +- [Diagnostic settings in Azure Monitor](/azure/azure-monitor/essentials/diagnostic-settings) |
| 274 | +- [Azure Monitor diagnostic log schema](/azure/azure-monitor/essentials/resource-logs-schema) |
0 commit comments