To enable Azure analytics (Azure Monitor/Log Analytics) on an Azure Windows VM using Terraform, you'll need to deploy the Azure Monitor Agent (AMA) or Log Analytics agent extension. Here's how to do it:
Option 1: Using Azure Monitor Agent (Recommended)
# Create a Log Analytics Workspace
resource "azurerm_log_analytics_workspace" "example" {
name = "example-workspace"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
sku = "PerGB2018"
retention_in_days = 30
}
# Create the Windows VM
resource "azurerm_windows_virtual_machine" "example" {
name = "example-vm"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
size = "Standard_DS2_v2"
admin_username = "adminuser"
admin_password = "P@ssword123!"
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 = "2022-Datacenter"
version = "latest"
}
identity {
type = "SystemAssigned"
}
}
# Install Azure Monitor Agent extension
resource "azurerm_virtual_machine_extension" "ama_windows" {
name = "AzureMonitorWindowsAgent"
virtual_machine_id = azurerm_windows_virtual_machine.example.id
publisher = "Microsoft.Azure.Monitor"
type = "AzureMonitorWindowsAgent"
type_handler_version = "1.0"
automatic_upgrade_enabled = true
}
# Create Data Collection Rule
resource "azurerm_monitor_data_collection_rule" "example" {
name = "example-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.example.id
name = "example-destination"
}
}
data_flow {
streams = ["Microsoft-InsightsMetrics", "Microsoft-Perf", "Microsoft-Event"]
destinations = ["example-destination"]
}
data_sources {
performance_counter {
streams = ["Microsoft-Perf", "Microsoft-InsightsMetrics"]
sampling_frequency_in_seconds = 60
counter_specifiers = [
"\\Processor Information(_Total)\\% Processor Time",
"\\Processor Information(_Total)\\% Privileged Time",
"\\Processor Information(_Total)\\% User Time",
"\\Processor Information(_Total)\\Processor Frequency",
"\\System\\Processes",
"\\Process(_Total)\\Thread Count",
"\\Memory\\Available Bytes",
"\\Memory\\Committed Bytes",
"\\Memory\\% Committed Bytes In Use",
"\\PhysicalDisk(_Total)\\Disk Transfers/sec",
"\\PhysicalDisk(_Total)\\Disk Reads/sec",
"\\PhysicalDisk(_Total)\\Disk Writes/sec"
]
name = "perfCounterDataSource"
}
windows_event_log {
streams = ["Microsoft-Event"]
x_path_queries = [
"Application!*[System[(Level=1 or Level=2 or Level=3)]]",
"System!*[System[(Level=1 or Level=2 or Level=3)]]"
]
name = "eventLogsDataSource"
}
}
}
# Associate the Data Collection Rule with the VM
resource "azurerm_monitor_data_collection_rule_association" "example" {
name = "example-dcra"
target_resource_id = azurerm_windows_virtual_machine.example.id
data_collection_rule_id = azurerm_monitor_data_collection_rule.example.id
}
Option 2: Using Log Analytics Agent (Legacy)
# Create Log Analytics Workspace
resource "azurerm_log_analytics_workspace" "example" {
name = "example-workspace"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
sku = "PerGB2018"
retention_in_days = 30
}
# Install MMA (Microsoft Monitoring Agent) extension
resource "azurerm_virtual_machine_extension" "mma" {
name = "MicrosoftMonitoringAgent"
virtual_machine_id = azurerm_windows_virtual_machine.example.id
publisher = "Microsoft.EnterpriseCloud.Monitoring"
type = "MicrosoftMonitoringAgent"
type_handler_version = "1.0"
settings = <<SETTINGS
{
"workspaceId": "${azurerm_log_analytics_workspace.example.workspace_id}"
}
SETTINGS
protected_settings = <<PROTECTED_SETTINGS
{
"workspaceKey": "${azurerm_log_analytics_workspace.example.primary_shared_key}"
}
PROTECTED_SETTINGS
}
Enabling VM Insights
To enable full VM Insights functionality, you'll also want to install the Dependency Agent:
# Install Dependency Agent for VM Insights
resource "azurerm_virtual_machine_extension" "da" {
name = "DependencyAgentWindows"
virtual_machine_id = azurerm_windows_virtual_machine.example.id
publisher = "Microsoft.Azure.Monitoring.DependencyAgent"
type = "DependencyAgentWindows"
type_handler_version = "9.0"
automatic_upgrade_enabled = true
settings = <<SETTINGS
{
"enableAMA": "true"
}
SETTINGS
}
The key components are:
- Log Analytics Workspace: Where your monitoring data will be stored
- Azure Monitor Agent (AMA): The modern agent that collects telemetry
- Data Collection Rule: Defines what data to collect and where to send it
- Data Collection Rule Association: Links the DCR to your VM
- Dependency Agent (optional): Provides additional insights for VM dependencies
After deployment, you can view your VM analytics in the Azure portal under Azure Monitor > Virtual Machines or in your Log Analytics workspace.