Using azure monitoring with loganalyticsworkspace
TypeScriptAzure Monitoring and Log Analytics Workspace are critical services offered on Microsoft Azure to monitor and analyze the health, performance, and availability of your applications and infrastructure. Log Analytics Workspace is a unique environment for Azure Monitor Logs data. Each workspace has its own data repository and configuration, and data sources and solutions are configured to store their data in particular workspaces.
To create an Azure Monitoring setup with a Log Analytics Workspace using Pulumi, you need to follow these steps:
- Define the Log Analytics Workspace: This is where the data from your monitoring will be collected and analyzed.
- Configure Monitoring Solutions: These are solutions that can be added to Log Analytics Workspace to monitor specific services or aspects of your infrastructure.
- Set Up Diagnostic Settings: This is required if you want to send logs and metrics to the Log Analytics Workspace for resources that support Azure Monitor Diagnostic settings.
Below is a Pulumi program that creates an Azure Log Analytics Workspace using azure-native.operationalinsights.Workspace and sets up Azure Monitoring for it using azure-native.monitor.DiagnosticSetting, along with the relevant commentary to guide you through the code.
import * as pulumi from "@pulumi/pulumi"; import * as azure_native from "@pulumi/azure-native"; // Create an Azure Resource Group to host your Log Analytics Workspace const resourceGroup = new azure_native.resources.ResourceGroup("myResourceGroup"); // Create an Azure Log Analytics Workspace in the Resource Group const workspace = new azure_native.operationalinsights.Workspace("myWorkspace", { // Specify the desired location for your workspace location: resourceGroup.location, // Provide the name of the resource group where your workspace will reside resourceGroupName: resourceGroup.name, // Define the Sku of the workspace, PerGB2018 is common for pay-as-you-go sku: { name: "PerGB2018", }, // Workspace properties can be configured such as retention time // Here we set the retention period to 30 days (optional) retentionInDays: 30, }); // Setting up monitoring for an example resource, e.g., an Azure Virtual Network // You would set up a similar diagnostic setting for resources you wish to monitor const exampleVirtualNetwork = new azure_native.network.VirtualNetwork("myVnet", { // Provide details of the Virtual Network location: resourceGroup.location, resourceGroupName: resourceGroup.name, addressSpace: { addressPrefixes: ["10.0.0.0/16"], }, }); const networkDiagnosticSetting = new azure_native.monitor.DiagnosticSetting("myNetworkDiagnosticSetting", { // Specify the resource ID to monitor; here it's the Virtual Network targetResourceId: exampleVirtualNetwork.id, // The logs and metrics that should be sent to the Log Analytics Workspace logs: [ { category: "NetworkSecurityGroupEvent", enabled: true, retentionPolicy: { enabled: false, days: 0, }, }, // Add other log categories as needed ], workspaceId: workspace.id, resourceGroupName: resourceGroup.name, }); // Export the IDs of the resources created export const resourceGroupId = resourceGroup.id; export const workspaceId = workspace.id; export const networkDiagnosticSettingId = networkDiagnosticSetting.id;
In the program above, we start by creating a new Resource Group which is a logical container for Azure resources. Then, a Log Analytics Workspace is created inside the Resource Group. We define parameters such as location (same as the Resource Group), SKU (for pricing), and optional retention period for logs.
Next, we demonstrate setting up monitoring for a resource by taking an Azure Virtual Network as an example. We configure the Diagnostic Settings to collect specific log categories (
NetworkSecurityGroupEvent
in our example) and send the logs to our earlier created Log Analytics Workspace.Finally, we export the IDs of created resources, so they can be easily referenced or used in Pulumi stacks or outputs.
Make sure to replace
"myResourceGroup"
,"myWorkspace"
,"myVnet"
, and"myNetworkDiagnosticSetting"
with your desired resource names, and you may need to add resources that are specific to your use case to the monitoring setup.Run this code by saving it in a
.ts
file and using Pulumi CLI commandspulumi up
to deploy the infrastructure. Make sure you've logged in to your Azure account withaz login
and set up the Pulumi Azure Native provider.