title: Terraform enable VM Insights
body: <p>Did someone managed to enable via terraforms Insights for a VM?</p>
  <p>i'm able to create a VM, enable logging, but not enable insights..</p>
  <p>i've seen this question: but don't find a clear answer..
  <a href="https://stackoverflow.com/questions/65428101/how-to-enable-azure-vm-application-insights-monitoring-agent-using-terraform">How to enable azure vm application insights monitoring agent using terraform</a></p>
  <p>Here is my full terraform script that i'm using for tests, i'm running it directly on the cloud shell from azure.</p>
  <pre><code>    # Configure the Azure provider
  provider &quot;azurerm&quot; {
      # The &quot;feature&quot; block is required for AzureRM provider 2.x.
      features {}
  }
  variable &quot;prefix&quot; {
    default = &quot;tfvmex&quot;
  }
  
  resource &quot;azurerm_resource_group&quot; &quot;main&quot; {
    name     = &quot;${var.prefix}-resources&quot;
    location = &quot;West Europe&quot;
  }
  
  resource &quot;azurerm_virtual_network&quot; &quot;main&quot; {
    name                = &quot;${var.prefix}-network&quot;
    address_space       = [&quot;10.0.0.0/16&quot;]
    location            = azurerm_resource_group.main.location
    resource_group_name = azurerm_resource_group.main.name
  }
  
  resource &quot;azurerm_subnet&quot; &quot;internal&quot; {
    name                 = &quot;internal&quot;
    resource_group_name  = azurerm_resource_group.main.name
    virtual_network_name = azurerm_virtual_network.main.name
    address_prefixes     = [&quot;10.0.2.0/24&quot;]
  }
  
  resource &quot;azurerm_network_interface&quot; &quot;main&quot; {
    name                = &quot;${var.prefix}-nic&quot;
    location            = azurerm_resource_group.main.location
    resource_group_name = azurerm_resource_group.main.name
  
    ip_configuration {
      name                          = &quot;testconfiguration1&quot;
      subnet_id                     = azurerm_subnet.internal.id
      private_ip_address_allocation = &quot;Dynamic&quot;
    }
  }
  
  resource &quot;azurerm_virtual_machine&quot; &quot;main&quot; {
    name                  = &quot;${var.prefix}-vm&quot;
    location              = azurerm_resource_group.main.location
    resource_group_name   = azurerm_resource_group.main.name
    network_interface_ids = [azurerm_network_interface.main.id]
    vm_size               = &quot;Standard_DS1_v2&quot;
  
    # Uncomment this line to delete the OS disk automatically when deleting the VM
    # delete_os_disk_on_termination = true
  
    # Uncomment this line to delete the data disks automatically when deleting the VM
    # delete_data_disks_on_termination = true
  
    storage_image_reference {
      publisher = &quot;Canonical&quot;
      offer     = &quot;UbuntuServer&quot;
      sku       = &quot;16.04-LTS&quot;
      version   = &quot;latest&quot;
    }
    storage_os_disk {
      name              = &quot;myosdisk1&quot;
      caching           = &quot;ReadWrite&quot;
      create_option     = &quot;FromImage&quot;
      managed_disk_type = &quot;Standard_LRS&quot;
    }
    os_profile {
      computer_name  = &quot;hostname&quot;
      admin_username = &quot;testadmin&quot;
      admin_password = &quot;Password1234!&quot;
    }
    os_profile_linux_config {
      disable_password_authentication = false
    }
    tags = {
      environment = &quot;staging&quot;
    }
  }
  
  resource &quot;azurerm_storage_account&quot; &quot;main&quot; {
    name                     = &quot;omstesttest22&quot;
    resource_group_name      = azurerm_resource_group.main.name
    location                 = &quot;westus&quot;
    account_tier             = &quot;Standard&quot;
    account_replication_type = &quot;GRS&quot;
  
    tags = {
      environment = &quot;staging&quot;
    }
  }
  
  resource &quot;azurerm_log_analytics_workspace&quot; &quot;law02&quot; {
    name                = &quot;${var.prefix}-logAnalytics&quot;
   location            = azurerm_resource_group.main.location
    resource_group_name = azurerm_resource_group.main.name
   sku                 = &quot;PerGB2018&quot;
    retention_in_days   = 30
  }
  
  
  
  resource &quot;azurerm_log_analytics_solution&quot; &quot;example&quot; {
    solution_name         = &quot;ContainerInsights&quot;
    location              = azurerm_resource_group.main.location
    resource_group_name   = azurerm_resource_group.main.name
    workspace_resource_id = azurerm_log_analytics_workspace.law02.id
    workspace_name        = azurerm_log_analytics_workspace.law02.name
  
    plan {
      publisher = &quot;Microsoft&quot;
      product   = &quot;OMSGallery/ContainerInsights&quot;
    }
  }
  
  #===================================================================
  # Set Monitoring and Log Analytics Workspace
  #===================================================================
  resource &quot;azurerm_virtual_machine_extension&quot; &quot;oms_mma02&quot; {
    name                       = &quot;test-OMSExtension&quot;
  virtual_machine_id         =  azurerm_virtual_machine.main.id
    publisher                  = &quot;Microsoft.EnterpriseCloud.Monitoring&quot;
    type                       = &quot;OmsAgentForLinux&quot;
    type_handler_version       = &quot;1.12&quot;
    auto_upgrade_minor_version = true
  
    settings = &lt;&lt;SETTINGS
      {
        &quot;workspaceId&quot; : &quot;${azurerm_log_analytics_workspace.law02.workspace_id}&quot;
      }
    SETTINGS
  
    protected_settings = &lt;&lt;PROTECTED_SETTINGS
      {
        &quot;workspaceKey&quot; : &quot;${azurerm_log_analytics_workspace.law02.primary_shared_key}&quot;
      }
    PROTECTED_SETTINGS
  }
  </code></pre>
  <p>Hope it was clear.
  Thanks!</p>