Skip to content

Commit

Permalink
Add diagnostic settings to storage account
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtiomMatiom committed Jul 5, 2024
1 parent d188b8e commit c7e74fe
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 1 deletion.
116 changes: 116 additions & 0 deletions modules/azure/storage_account_public/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,119 @@ resource "azurerm_storage_management_policy" "storage_management_policy" {
}
}
}

data "azurerm_monitor_diagnostic_categories" "blob" {
count = var.loganalytics_diagnostic_setting.workspace_id == null || var.loganalytics_diagnostic_setting.blob == null ? 0 : 1
resource_id = "${azurerm_storage_account.storage_account.id}/blobServices/default/"
}

data "azurerm_monitor_diagnostic_categories" "queue" {
count = var.loganalytics_diagnostic_setting.workspace_id == null || var.loganalytics_diagnostic_setting.queue == null ? 0 : 1
resource_id = "${azurerm_storage_account.storage_account.id}/queueServices/default/"
}

data "azurerm_monitor_diagnostic_categories" "table" {
count = var.loganalytics_diagnostic_setting.workspace_id == null || var.loganalytics_diagnostic_setting.table == null ? 0 : 1
resource_id = "${azurerm_storage_account.storage_account.id}/tableServices/default/"
}

data "azurerm_monitor_diagnostic_categories" "file" {
count = var.loganalytics_diagnostic_setting.workspace_id == null || var.loganalytics_diagnostic_setting.file == null ? 0 : 1
resource_id = "${azurerm_storage_account.storage_account.id}/tableServices/default/"
}

resource "azurerm_monitor_diagnostic_setting" "blob" {
count = var.loganalytics_diagnostic_setting.workspace_id == null || var.loganalytics_diagnostic_setting.blob == null ? 0 : 1
name = "diag-blob-${var.name}"
target_resource_id = "${azurerm_storage_account.storage_account.id}/blobServices/default/"
log_analytics_workspace_id = var.loganalytics_diagnostic_setting.workspace_id

dynamic "enabled_log" {
for_each = var.loganalytics_diagnostic_setting.blob.categories == null ? data.azurerm_monitor_diagnostic_categories.blob[0].log_category_types : var.loganalytics_diagnostic_setting.blob.categories

content {
category = enabled_log.value
}
}

dynamic "metric" {
for_each = var.loganalytics_diagnostic_setting.blob.metrics == null ? data.azurerm_monitor_diagnostic_categories.blob[0].metrics : var.loganalytics_diagnostic_setting.blob.metrics

content {
category = metric.value
enabled = true
}
}
}

resource "azurerm_monitor_diagnostic_setting" "queue" {
count = var.loganalytics_diagnostic_setting.workspace_id == null || var.loganalytics_diagnostic_setting.queue == null ? 0 : 1
name = "diag-blob-${var.name}"
target_resource_id = "${azurerm_storage_account.storage_account.id}/queueServices/default/"
log_analytics_workspace_id = var.loganalytics_diagnostic_setting.workspace_id

dynamic "enabled_log" {
for_each = var.loganalytics_diagnostic_setting.queue.categories == null ? data.azurerm_monitor_diagnostic_categories.queue[0].log_category_types : var.loganalytics_diagnostic_setting.queue.categories

content {
category = enabled_log.value
}
}

dynamic "metric" {
for_each = var.loganalytics_diagnostic_setting.queue.metrics == null ? data.azurerm_monitor_diagnostic_categories.queue[0].metrics : var.loganalytics_diagnostic_setting.queue.metrics

content {
category = metric.value
enabled = true
}
}
}

resource "azurerm_monitor_diagnostic_setting" "table" {
count = var.loganalytics_diagnostic_setting.workspace_id == null || var.loganalytics_diagnostic_setting.table == null ? 0 : 1
name = "diag-blob-${var.name}"
target_resource_id = "${azurerm_storage_account.storage_account.id}/tableServices/default/"
log_analytics_workspace_id = var.loganalytics_diagnostic_setting.workspace_id

dynamic "enabled_log" {
for_each = var.loganalytics_diagnostic_setting.table.categories == null ? data.azurerm_monitor_diagnostic_categories.table[0].log_category_types : var.loganalytics_diagnostic_setting.table.categories

content {
category = enabled_log.value
}
}

dynamic "metric" {
for_each = var.loganalytics_diagnostic_setting.table.metrics == null ? data.azurerm_monitor_diagnostic_categories.table[0].metrics : var.loganalytics_diagnostic_setting.table.metrics

content {
category = metric.value
enabled = true
}
}
}

resource "azurerm_monitor_diagnostic_setting" "file" {
count = var.loganalytics_diagnostic_setting.workspace_id == null || var.loganalytics_diagnostic_setting.table == null ? 0 : 1
name = "diag-blob-${var.name}"
target_resource_id = "${azurerm_storage_account.storage_account.id}/fileServices/default/"
log_analytics_workspace_id = var.loganalytics_diagnostic_setting.workspace_id

dynamic "enabled_log" {
for_each = var.loganalytics_diagnostic_setting.file.categories == null ? data.azurerm_monitor_diagnostic_categories.file[0].log_category_types : var.loganalytics_diagnostic_setting.file.categories

content {
category = enabled_log.value
}
}

dynamic "metric" {
for_each = var.loganalytics_diagnostic_setting.file.metrics == null ? data.azurerm_monitor_diagnostic_categories.file[0].metrics : var.loganalytics_diagnostic_setting.file.metrics

content {
category = metric.value
enabled = true
}
}
}
31 changes: 30 additions & 1 deletion modules/azure/storage_account_public/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,33 @@ variable "delete_retention_policy_days" {
type = number
description = "Number of days to retain deleted blobs."
default = null
}
}

variable "loganalytics_diagnostic_setting" {
type = object({
workspace_id = string, // log analytics workspace ID
blob = optional(object({
categories = optional(list(string)), // null list,means send all categories
metrics = optional(list(string)), // null list means send all metrics
destination_type = optional(string), // AzureDiagnostics or Dedicated
})),
queue = optional(object({
categories = optional(list(string)), // null list,means send all categories
metrics = optional(list(string)), // null list means send all metrics
destination_type = optional(string), // AzureDiagnostics or Dedicated
})),
table = optional(object({
categories = optional(list(string)), // null list,means send all categories
metrics = optional(list(string)), // null list means send all metrics
destination_type = optional(string), // AzureDiagnostics or Dedicated
}))
file = optional(object({
categories = optional(list(string)), // null list,means send all categories
metrics = optional(list(string)), // null list means send all metrics
destination_type = optional(string), // AzureDiagnostics or Dedicated
}))
})
description = "Specifies the log categories that have to be sent to Log analytics."
default = null
}

0 comments on commit c7e74fe

Please sign in to comment.