From e8e3b00c854fd5f598bcf8bb985ee875488655c1 Mon Sep 17 00:00:00 2001 From: Tao <104055472+teowa@users.noreply.github.com> Date: Thu, 10 Nov 2022 10:44:06 -0800 Subject: [PATCH] add `azurerm_monitor_data_collection_rule` example (#19223) Resolves https://github.com/hashicorp/terraform-provider-azurerm/issues/19156 --- .../data-collection-rule/README.md | 3 + .../data-collection-rule/main.tf | 171 ++++++++++++++++++ .../data-collection-rule/variables.tf | 7 + 3 files changed, 181 insertions(+) create mode 100644 examples/azure-monitoring/data-collection-rule/README.md create mode 100644 examples/azure-monitoring/data-collection-rule/main.tf create mode 100644 examples/azure-monitoring/data-collection-rule/variables.tf diff --git a/examples/azure-monitoring/data-collection-rule/README.md b/examples/azure-monitoring/data-collection-rule/README.md new file mode 100644 index 000000000000..ad8e03ebc730 --- /dev/null +++ b/examples/azure-monitoring/data-collection-rule/README.md @@ -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) diff --git a/examples/azure-monitoring/data-collection-rule/main.tf b/examples/azure-monitoring/data-collection-rule/main.tf new file mode 100644 index 000000000000..a50767173782 --- /dev/null +++ b/examples/azure-monitoring/data-collection-rule/main.tf @@ -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" +} diff --git a/examples/azure-monitoring/data-collection-rule/variables.tf b/examples/azure-monitoring/data-collection-rule/variables.tf new file mode 100644 index 000000000000..68babb00b91a --- /dev/null +++ b/examples/azure-monitoring/data-collection-rule/variables.tf @@ -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." +}