Skip to content

Commit 109656b

Browse files
authored
Merge pull request #107 from ks6088ts-labs/feature/issue-106_add-logging
add log analytics workspace to enable logging on IoT Hub
2 parents fd7c6f7 + f00a566 commit 109656b

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

infra/main.bicep

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ param cosmosDbName string = '${prefix}cosmosdb'
3535
@description('Specifies the name of the Azure IoT Hub resource.')
3636
param iotHubName string = '${prefix}iothub'
3737

38+
@description('Specifies the name of the Azure Log Analytics workspace.')
39+
param logAnalyticsWorkspaceName string = '${prefix}law'
40+
3841
module openAi './modules/openAi.bicep' = {
3942
name: 'openAi'
4043
params: {
@@ -101,6 +104,16 @@ module iotHub './modules/iotHub.bicep' = {
101104
name: iotHubName
102105
location: location
103106
tags: tags
107+
workspaceId: logAnalytics.outputs.id
108+
}
109+
}
110+
111+
module logAnalytics './modules/logAnalytics.bicep' = {
112+
name: 'logAnalytics'
113+
params: {
114+
name: logAnalyticsWorkspaceName
115+
location: location
116+
tags: tags
104117
}
105118
}
106119

infra/modules/iotHub.bicep

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ param skuName string = 'S1'
1717
@description('The number of IoT Hub units.')
1818
param skuUnits int = 1
1919

20+
@description('Specifies the resource id of the Log Analytics workspace.')
21+
param workspaceId string
22+
2023
resource iotHub 'Microsoft.Devices/IotHubs@2023-06-30' = {
2124
name: name
2225
location: location
@@ -27,6 +30,27 @@ resource iotHub 'Microsoft.Devices/IotHubs@2023-06-30' = {
2730
}
2831
}
2932

33+
resource diagnosticSettings 'Microsoft.Insights/diagnosticSettings@2021-05-01-preview' = {
34+
name: 'diagnosticSettings'
35+
scope: iotHub
36+
properties: {
37+
workspaceId: workspaceId
38+
metrics: [
39+
for metric in ['AllMetrics']: {
40+
category: metric
41+
enabled: true
42+
timeGrain: null
43+
}
44+
]
45+
logs: [
46+
for categoryGroup in ['allLogs', 'audit']: {
47+
categoryGroup: categoryGroup
48+
enabled: true
49+
}
50+
]
51+
}
52+
}
53+
3054
// Outputs
3155
output iotHubId string = iotHub.id
3256
output iotHubName string = iotHub.name

infra/modules/logAnalytics.bicep

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Parameters
2+
@description('Specifies the name of the Log Analytics workspace.')
3+
param name string
4+
5+
@description('Specifies the service tier of the workspace: Free, Standalone, PerNode, Per-GB.')
6+
@allowed([
7+
'Free'
8+
'Standalone'
9+
'PerNode'
10+
'PerGB2018'
11+
])
12+
param sku string = 'PerGB2018'
13+
14+
@description('Specifies the workspace data retention in days. -1 means Unlimited retention for the Unlimited Sku. 730 days is the maximum allowed for all other Skus.')
15+
param retentionInDays int = 60
16+
17+
@description('Specifies the location.')
18+
param location string = resourceGroup().location
19+
20+
@description('Specifies the resource tags.')
21+
param tags object
22+
23+
// Resources
24+
resource logAnalyticsWorkspace 'Microsoft.OperationalInsights/workspaces@2023-09-01' = {
25+
name: name
26+
tags: tags
27+
location: location
28+
properties: {
29+
sku: {
30+
name: sku
31+
}
32+
retentionInDays: retentionInDays
33+
}
34+
}
35+
36+
//Outputs
37+
output id string = logAnalyticsWorkspace.id
38+
output name string = logAnalyticsWorkspace.name
39+
output customerId string = logAnalyticsWorkspace.properties.customerId

0 commit comments

Comments
 (0)