Skip to content

Commit

Permalink
add azurerm_monitor_data_collection_rule example (#19223)
Browse files Browse the repository at this point in the history
Resolves #19156
  • Loading branch information
teowa authored Nov 10, 2022
1 parent cd0a13d commit e8e3b00
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 0 deletions.
3 changes: 3 additions & 0 deletions examples/azure-monitoring/data-collection-rule/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Example: Azure Monitor Data Collection Rule

This example provisions an Azure Monitor Data Collection Rule collecting data from Windows Virtual Machine. See more about [Collect data from virtual machines with Azure Monitor Agent](https://learn.microsoft.com/en-us/azure/azure-monitor/agents/data-collection-rule-azure-monitor-agent?tabs=portal) and [Troubleshooting guidance](https://learn.microsoft.com/en-us/azure/azure-monitor/agents/azure-monitor-agent-troubleshoot-windows-vm)
171 changes: 171 additions & 0 deletions examples/azure-monitoring/data-collection-rule/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "example" {
name = "${var.prefix}-resources"
location = var.location
}
resource "azurerm_log_analytics_workspace" "workspace" {
name = "${var.prefix}-law"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
sku = "PerGB2018"
retention_in_days = 30
}

resource "azurerm_log_analytics_solution" "vminsights" {
solution_name = "VMInsights"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
workspace_resource_id = azurerm_log_analytics_workspace.workspace.id
workspace_name = azurerm_log_analytics_workspace.workspace.name
plan {
publisher = "Microsoft"
product = "OMSGallery/VMInsights"
}
}

# Data Collection Rule
resource "azurerm_monitor_data_collection_rule" "rule" {
name = "${var.prefix}-dcr"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location

destinations {
log_analytics {
workspace_resource_id = azurerm_log_analytics_workspace.workspace.id
name = "destination-log"
}

azure_monitor_metrics {
name = "destination-metrics"
}
}

data_flow {
streams = ["Microsoft-InsightsMetrics"]
destinations = ["destination-metrics"]
}

data_flow {
streams = ["Microsoft-InsightsMetrics", "Microsoft-Syslog", "Microsoft-Perf", "Microsoft-WindowsEvent"]
destinations = ["destination-log"]
}

data_sources {
performance_counter {
streams = ["Microsoft-Perf", "Microsoft-InsightsMetrics"]
sampling_frequency_in_seconds = 60
counter_specifiers = ["\\VmInsights\\DetailedMetrics"]
name = "VMInsightsPerfCounters"
}

}
depends_on = [
azurerm_log_analytics_solution.vminsights
]
}
resource "azurerm_virtual_network" "example" {
name = "${var.prefix}-network"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
}

resource "azurerm_subnet" "example" {
name = "${var.prefix}-subnet"
resource_group_name = azurerm_resource_group.example.name
virtual_network_name = azurerm_virtual_network.example.name
address_prefixes = ["10.0.2.0/24"]
}

resource "azurerm_network_interface" "example" {
name = "${var.prefix}-nic"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name

ip_configuration {
name = "internal"
subnet_id = azurerm_subnet.example.id
private_ip_address_allocation = "Dynamic"
}
}

resource "azurerm_user_assigned_identity" "example" {
name = "${var.prefix}-id"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
}

#VM
resource "azurerm_windows_virtual_machine" "example" {
name = "${var.prefix}-machine"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
size = "Standard_F2"
admin_username = "adminuser"
admin_password = "P@$$w0rd1234!"
network_interface_ids = [
azurerm_network_interface.example.id,
]

os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}

source_image_reference {
publisher = "MicrosoftWindowsServer"
offer = "WindowsServer"
sku = "2016-Datacenter"
version = "latest"
}
identity {
type = "UserAssigned"
identity_ids = [azurerm_user_assigned_identity.example.id]
}
}

# Azure Monitor Extension
resource "azurerm_virtual_machine_extension" "azuremonitorwindowsagent" {
name = "AzureMonitorWindowsAgent"
publisher = "Microsoft.Azure.Monitor"
type = "AzureMonitorWindowsAgent"
type_handler_version = 1.8
automatic_upgrade_enabled = true
auto_upgrade_minor_version = "true"
virtual_machine_id = azurerm_windows_virtual_machine.example.id

settings = jsonencode({
workspaceId = azurerm_log_analytics_workspace.workspace.id
azureResourceId = azurerm_windows_virtual_machine.example.id
stopOnMultipleConnections = false

authentication = {
managedIdentity = {
identifier-name = "mi_res_id"
identifier-value = azurerm_user_assigned_identity.example.id
}
}
})
protected_settings = jsonencode({
"workspaceKey" = azurerm_log_analytics_workspace.workspace.primary_shared_key
})
}

resource "azurerm_virtual_machine_extension" "da" {
name = "DAExtension"
virtual_machine_id = azurerm_windows_virtual_machine.example.id
publisher = "Microsoft.Azure.Monitoring.DependencyAgent"
type = "DependencyAgentWindows"
type_handler_version = "9.10"
automatic_upgrade_enabled = true
auto_upgrade_minor_version = true
}

resource "azurerm_monitor_data_collection_rule_association" "example1" {
name = "${var.prefix}-dcra"
target_resource_id = azurerm_windows_virtual_machine.example.id
data_collection_rule_id = azurerm_monitor_data_collection_rule.rule.id
description = "example"
}
7 changes: 7 additions & 0 deletions examples/azure-monitoring/data-collection-rule/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
variable "prefix" {
description = "The prefix which should be used for all resources in this example"
}

variable "location" {
description = "The Azure Region in which all resources in this example should be created."
}

0 comments on commit e8e3b00

Please sign in to comment.