Skip to content

Commit

Permalink
add log analytics workspace to enable logging on IoT Hub
Browse files Browse the repository at this point in the history
  • Loading branch information
ks6088ts committed Jun 17, 2024
1 parent fd7c6f7 commit f00a566
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
13 changes: 13 additions & 0 deletions infra/main.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ param cosmosDbName string = '${prefix}cosmosdb'
@description('Specifies the name of the Azure IoT Hub resource.')
param iotHubName string = '${prefix}iothub'

@description('Specifies the name of the Azure Log Analytics workspace.')
param logAnalyticsWorkspaceName string = '${prefix}law'

module openAi './modules/openAi.bicep' = {
name: 'openAi'
params: {
Expand Down Expand Up @@ -101,6 +104,16 @@ module iotHub './modules/iotHub.bicep' = {
name: iotHubName
location: location
tags: tags
workspaceId: logAnalytics.outputs.id
}
}

module logAnalytics './modules/logAnalytics.bicep' = {
name: 'logAnalytics'
params: {
name: logAnalyticsWorkspaceName
location: location
tags: tags
}
}

Expand Down
24 changes: 24 additions & 0 deletions infra/modules/iotHub.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ param skuName string = 'S1'
@description('The number of IoT Hub units.')
param skuUnits int = 1

@description('Specifies the resource id of the Log Analytics workspace.')
param workspaceId string

resource iotHub 'Microsoft.Devices/IotHubs@2023-06-30' = {
name: name
location: location
Expand All @@ -27,6 +30,27 @@ resource iotHub 'Microsoft.Devices/IotHubs@2023-06-30' = {
}
}

resource diagnosticSettings 'Microsoft.Insights/diagnosticSettings@2021-05-01-preview' = {
name: 'diagnosticSettings'
scope: iotHub
properties: {
workspaceId: workspaceId
metrics: [
for metric in ['AllMetrics']: {
category: metric
enabled: true
timeGrain: null
}
]
logs: [
for categoryGroup in ['allLogs', 'audit']: {
categoryGroup: categoryGroup
enabled: true
}
]
}
}

// Outputs
output iotHubId string = iotHub.id
output iotHubName string = iotHub.name
39 changes: 39 additions & 0 deletions infra/modules/logAnalytics.bicep
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Parameters
@description('Specifies the name of the Log Analytics workspace.')
param name string

@description('Specifies the service tier of the workspace: Free, Standalone, PerNode, Per-GB.')
@allowed([
'Free'
'Standalone'
'PerNode'
'PerGB2018'
])
param sku string = 'PerGB2018'

@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.')
param retentionInDays int = 60

@description('Specifies the location.')
param location string = resourceGroup().location

@description('Specifies the resource tags.')
param tags object

// Resources
resource logAnalyticsWorkspace 'Microsoft.OperationalInsights/workspaces@2023-09-01' = {
name: name
tags: tags
location: location
properties: {
sku: {
name: sku
}
retentionInDays: retentionInDays
}
}

//Outputs
output id string = logAnalyticsWorkspace.id
output name string = logAnalyticsWorkspace.name
output customerId string = logAnalyticsWorkspace.properties.customerId

0 comments on commit f00a566

Please sign in to comment.