|
| 1 | +# Azure Log Analytics Workspace Construct |
| 2 | + |
| 3 | +This module provides a high-level TypeScript construct for managing Azure Log Analytics Workspaces using the Terraform CDK and Azure AZAPI provider. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Multiple API Version Support**: Supports 2023-09-01 (latest) and 2022-10-01 (for backward compatibility) |
| 8 | +- **Automatic Version Resolution**: Uses the latest API version by default |
| 9 | +- **Version Pinning**: Lock to a specific API version for stability |
| 10 | +- **Type-Safe**: Full TypeScript support with comprehensive interfaces |
| 11 | +- **JSII Compatible**: Can be used from TypeScript, Python, Java, and C# |
| 12 | +- **Terraform Outputs**: Automatic creation of outputs for id, name, workspaceId, and customerId |
| 13 | +- **Tag Management**: Built-in methods for managing resource tags |
| 14 | + |
| 15 | +## Supported API Versions |
| 16 | + |
| 17 | +| Version | Status | Release Date | Notes | |
| 18 | +|---------|--------|--------------|-------| |
| 19 | +| 2023-09-01 | Active (Latest) | 2023-09-01 | Recommended for new deployments, includes Data Collection Rule support | |
| 20 | +| 2022-10-01 | Active | 2022-10-01 | Stable release for backward compatibility | |
| 21 | + |
| 22 | +## Installation |
| 23 | + |
| 24 | +This module is part of the `@microsoft/terraform-cdk-constructs` package. |
| 25 | + |
| 26 | +```bash |
| 27 | +npm install @microsoft/terraform-cdk-constructs |
| 28 | +``` |
| 29 | + |
| 30 | +## Basic Usage |
| 31 | + |
| 32 | +### Simple Log Analytics Workspace |
| 33 | + |
| 34 | +```typescript |
| 35 | +import { App, TerraformStack } from "cdktn"; |
| 36 | +import { AzapiProvider } from "@microsoft/terraform-cdk-constructs/core-azure"; |
| 37 | +import { ResourceGroup } from "@microsoft/terraform-cdk-constructs/azure-resourcegroup"; |
| 38 | +import { LogAnalyticsWorkspace } from "@microsoft/terraform-cdk-constructs/azure-loganalyticsworkspace"; |
| 39 | + |
| 40 | +const app = new App(); |
| 41 | +const stack = new TerraformStack(app, "log-analytics-stack"); |
| 42 | + |
| 43 | +// Configure the Azure provider |
| 44 | +new AzapiProvider(stack, "azapi", {}); |
| 45 | + |
| 46 | +// Create a resource group |
| 47 | +const resourceGroup = new ResourceGroup(stack, "rg", { |
| 48 | + name: "my-resource-group", |
| 49 | + location: "eastus", |
| 50 | +}); |
| 51 | + |
| 52 | +// Create a Log Analytics Workspace |
| 53 | +const workspace = new LogAnalyticsWorkspace(stack, "workspace", { |
| 54 | + name: "my-log-analytics", |
| 55 | + location: "eastus", |
| 56 | + resourceGroupId: resourceGroup.id, |
| 57 | + tags: { |
| 58 | + environment: "production", |
| 59 | + project: "monitoring", |
| 60 | + }, |
| 61 | +}); |
| 62 | + |
| 63 | +app.synth(); |
| 64 | +``` |
| 65 | + |
| 66 | +## Advanced Usage |
| 67 | + |
| 68 | +### Custom Retention Period |
| 69 | + |
| 70 | +```typescript |
| 71 | +const workspace = new LogAnalyticsWorkspace(stack, "workspace-retention", { |
| 72 | + name: "workspace-with-retention", |
| 73 | + location: "eastus", |
| 74 | + resourceGroupId: resourceGroup.id, |
| 75 | + retentionInDays: 90, // Keep data for 90 days (range: 30-730) |
| 76 | +}); |
| 77 | +``` |
| 78 | + |
| 79 | +### PerGB2018 Pricing (Pay-as-you-go) |
| 80 | + |
| 81 | +```typescript |
| 82 | +const workspace = new LogAnalyticsWorkspace(stack, "workspace-pergb", { |
| 83 | + name: "workspace-pergb", |
| 84 | + location: "eastus", |
| 85 | + resourceGroupId: resourceGroup.id, |
| 86 | + sku: { |
| 87 | + name: "PerGB2018", |
| 88 | + }, |
| 89 | + retentionInDays: 60, |
| 90 | +}); |
| 91 | +``` |
| 92 | + |
| 93 | +### Capacity Reservation Pricing |
| 94 | + |
| 95 | +```typescript |
| 96 | +const workspace = new LogAnalyticsWorkspace(stack, "workspace-capacity", { |
| 97 | + name: "workspace-capacity-reservation", |
| 98 | + location: "eastus", |
| 99 | + resourceGroupId: resourceGroup.id, |
| 100 | + sku: { |
| 101 | + name: "CapacityReservation", |
| 102 | + capacityReservationLevel: 500, // Valid values: 100, 200, 300, 400, 500, 1000, 2000, 5000 |
| 103 | + }, |
| 104 | + retentionInDays: 365, |
| 105 | +}); |
| 106 | +``` |
| 107 | + |
| 108 | +### Daily Volume Cap (Workspace Capping) |
| 109 | + |
| 110 | +```typescript |
| 111 | +const workspace = new LogAnalyticsWorkspace(stack, "workspace-capped", { |
| 112 | + name: "workspace-with-cap", |
| 113 | + location: "eastus", |
| 114 | + resourceGroupId: resourceGroup.id, |
| 115 | + workspaceCapping: { |
| 116 | + dailyQuotaGb: 100, // Stop ingestion after 100GB per day |
| 117 | + }, |
| 118 | +}); |
| 119 | +``` |
| 120 | + |
| 121 | +### Private Network Access |
| 122 | + |
| 123 | +```typescript |
| 124 | +const workspace = new LogAnalyticsWorkspace(stack, "workspace-private", { |
| 125 | + name: "workspace-private", |
| 126 | + location: "eastus", |
| 127 | + resourceGroupId: resourceGroup.id, |
| 128 | + publicNetworkAccessForIngestion: "Disabled", |
| 129 | + publicNetworkAccessForQuery: "Disabled", |
| 130 | +}); |
| 131 | +``` |
| 132 | + |
| 133 | +### Workspace Features |
| 134 | + |
| 135 | +```typescript |
| 136 | +const workspace = new LogAnalyticsWorkspace(stack, "workspace-features", { |
| 137 | + name: "workspace-with-features", |
| 138 | + location: "eastus", |
| 139 | + resourceGroupId: resourceGroup.id, |
| 140 | + features: { |
| 141 | + enableDataExport: true, |
| 142 | + immediatePurgeDataOn30Days: false, |
| 143 | + disableLocalAuth: true, |
| 144 | + enableLogAccessUsingOnlyResourcePermissions: true, |
| 145 | + }, |
| 146 | +}); |
| 147 | +``` |
| 148 | + |
| 149 | +### Managed Identity |
| 150 | + |
| 151 | +```typescript |
| 152 | +const workspace = new LogAnalyticsWorkspace(stack, "workspace-identity", { |
| 153 | + name: "workspace-with-identity", |
| 154 | + location: "eastus", |
| 155 | + resourceGroupId: resourceGroup.id, |
| 156 | + identity: { |
| 157 | + type: "SystemAssigned", |
| 158 | + }, |
| 159 | +}); |
| 160 | +``` |
| 161 | + |
| 162 | +### Pinning to a Specific API Version |
| 163 | + |
| 164 | +```typescript |
| 165 | +const workspace = new LogAnalyticsWorkspace(stack, "workspace-pinned", { |
| 166 | + name: "workspace-stable", |
| 167 | + location: "eastus", |
| 168 | + resourceGroupId: resourceGroup.id, |
| 169 | + apiVersion: "2022-10-01", // Pin to specific version |
| 170 | +}); |
| 171 | +``` |
| 172 | + |
| 173 | +### Using Outputs |
| 174 | + |
| 175 | +```typescript |
| 176 | +const workspace = new LogAnalyticsWorkspace(stack, "workspace", { |
| 177 | + name: "my-workspace", |
| 178 | + location: "eastus", |
| 179 | + resourceGroupId: resourceGroup.id, |
| 180 | +}); |
| 181 | + |
| 182 | +// Access workspace ID for use in other resources (e.g., Diagnostic Settings) |
| 183 | +console.log(workspace.id); // Terraform interpolation string |
| 184 | + |
| 185 | +// Use outputs for cross-stack references |
| 186 | +new TerraformOutput(stack, "workspace-id", { |
| 187 | + value: workspace.idOutput, |
| 188 | +}); |
| 189 | + |
| 190 | +new TerraformOutput(stack, "workspace-customer-id", { |
| 191 | + value: workspace.customerIdOutput, |
| 192 | +}); |
| 193 | +``` |
| 194 | + |
| 195 | +## API Reference |
| 196 | + |
| 197 | +### LogAnalyticsWorkspaceProps |
| 198 | + |
| 199 | +Configuration properties for the Log Analytics Workspace construct. |
| 200 | + |
| 201 | +| Property | Type | Required | Description | |
| 202 | +|----------|------|----------|-------------| |
| 203 | +| `name` | `string` | No* | Name of the workspace. If not provided, uses the construct ID. Must be 4-63 characters, alphanumeric and hyphens. | |
| 204 | +| `location` | `string` | Yes | Azure region where the workspace will be created. | |
| 205 | +| `resourceGroupId` | `string` | Yes | Resource group ID where the workspace will be created. | |
| 206 | +| `retentionInDays` | `number` | No | Data retention in days (30-730). Default: 30. | |
| 207 | +| `sku` | `LogAnalyticsWorkspaceSku` | No | SKU configuration. Default: { name: "PerGB2018" }. | |
| 208 | +| `workspaceCapping` | `LogAnalyticsWorkspaceCapping` | No | Daily volume cap configuration. | |
| 209 | +| `publicNetworkAccessForIngestion` | `"Enabled" \| "Disabled"` | No | Network access for data ingestion. Default: "Enabled". | |
| 210 | +| `publicNetworkAccessForQuery` | `"Enabled" \| "Disabled"` | No | Network access for queries. Default: "Enabled". | |
| 211 | +| `features` | `LogAnalyticsWorkspaceFeatures` | No | Workspace feature flags. | |
| 212 | +| `forceCmkForQuery` | `boolean` | No | Require customer-managed keys for saved searches/alerts. | |
| 213 | +| `defaultDataCollectionRuleResourceId` | `string` | No | Default DCR resource ID (API 2023-09-01+). | |
| 214 | +| `identity` | `LogAnalyticsWorkspaceIdentity` | No | Managed identity configuration. | |
| 215 | +| `tags` | `{ [key: string]: string }` | No | Resource tags. | |
| 216 | +| `apiVersion` | `string` | No | Pin to a specific API version. | |
| 217 | + |
| 218 | +### LogAnalyticsWorkspaceSku |
| 219 | + |
| 220 | +| Property | Type | Required | Description | |
| 221 | +|----------|------|----------|-------------| |
| 222 | +| `name` | `string` | Yes | SKU name: "Free", "Standard", "Premium", "PerNode", "PerGB2018", "Standalone", "CapacityReservation" | |
| 223 | +| `capacityReservationLevel` | `number` | No | Capacity in GB/day for CapacityReservation SKU. Valid: 100, 200, 300, 400, 500, 1000, 2000, 5000. | |
| 224 | + |
| 225 | +### LogAnalyticsWorkspaceCapping |
| 226 | + |
| 227 | +| Property | Type | Required | Description | |
| 228 | +|----------|------|----------|-------------| |
| 229 | +| `dailyQuotaGb` | `number` | Yes | Daily volume cap in GB. Use -1 for no cap. | |
| 230 | + |
| 231 | +### LogAnalyticsWorkspaceFeatures |
| 232 | + |
| 233 | +| Property | Type | Required | Description | |
| 234 | +|----------|------|----------|-------------| |
| 235 | +| `enableDataExport` | `boolean` | No | Enable data export capability. | |
| 236 | +| `immediatePurgeDataOn30Days` | `boolean` | No | Purge data immediately after 30 days. | |
| 237 | +| `disableLocalAuth` | `boolean` | No | Disable local authentication. | |
| 238 | +| `enableLogAccessUsingOnlyResourcePermissions` | `boolean` | No | Enable resource-based access only. | |
| 239 | + |
| 240 | +### LogAnalyticsWorkspaceIdentity |
| 241 | + |
| 242 | +| Property | Type | Required | Description | |
| 243 | +|----------|------|----------|-------------| |
| 244 | +| `type` | `string` | Yes | Identity type: "SystemAssigned", "UserAssigned", "SystemAssigned,UserAssigned", "None" | |
| 245 | +| `userAssignedIdentities` | `object` | No | User-assigned identity resource IDs. | |
| 246 | + |
| 247 | +### Public Properties |
| 248 | + |
| 249 | +| Property | Type | Description | |
| 250 | +|----------|------|-------------| |
| 251 | +| `id` | `string` | The Azure resource ID of the workspace. | |
| 252 | +| `props` | `LogAnalyticsWorkspaceProps` | The original input properties. | |
| 253 | +| `resolvedApiVersion` | `string` | The API version being used. | |
| 254 | + |
| 255 | +### Outputs |
| 256 | + |
| 257 | +The construct automatically creates Terraform outputs: |
| 258 | + |
| 259 | +- `id`: The workspace resource ID |
| 260 | +- `name`: The workspace name |
| 261 | +- `workspace_id`: The workspace unique identifier (GUID) |
| 262 | +- `customer_id`: The workspace customer ID (same as workspace_id) |
| 263 | + |
| 264 | +### Methods |
| 265 | + |
| 266 | +| Method | Parameters | Description | |
| 267 | +|--------|-----------|-------------| |
| 268 | +| `addTag` | `key: string, value: string` | Add a tag to the workspace. | |
| 269 | +| `addAccess` | `objectId: string, roleDefinitionName: string` | Add RBAC role assignment. | |
| 270 | + |
| 271 | +## Best Practices |
| 272 | + |
| 273 | +### 1. Use PerGB2018 for Most Workloads |
| 274 | + |
| 275 | +Unless you have specific requirements, the pay-as-you-go model is most cost-effective: |
| 276 | + |
| 277 | +```typescript |
| 278 | +const workspace = new LogAnalyticsWorkspace(stack, "workspace", { |
| 279 | + name: "my-workspace", |
| 280 | + location: "eastus", |
| 281 | + resourceGroupId: resourceGroup.id, |
| 282 | + sku: { name: "PerGB2018" }, |
| 283 | +}); |
| 284 | +``` |
| 285 | + |
| 286 | +### 2. Set Appropriate Retention |
| 287 | + |
| 288 | +Balance between compliance requirements and cost: |
| 289 | + |
| 290 | +```typescript |
| 291 | +// Production: longer retention for compliance |
| 292 | +const prodWorkspace = new LogAnalyticsWorkspace(stack, "prod-workspace", { |
| 293 | + name: "prod-logs", |
| 294 | + location: "eastus", |
| 295 | + resourceGroupId: resourceGroup.id, |
| 296 | + retentionInDays: 365, |
| 297 | +}); |
| 298 | + |
| 299 | +// Development: shorter retention to save costs |
| 300 | +const devWorkspace = new LogAnalyticsWorkspace(stack, "dev-workspace", { |
| 301 | + name: "dev-logs", |
| 302 | + location: "eastus", |
| 303 | + resourceGroupId: resourceGroup.id, |
| 304 | + retentionInDays: 30, |
| 305 | +}); |
| 306 | +``` |
| 307 | + |
| 308 | +### 3. Use Daily Cap for Cost Control |
| 309 | + |
| 310 | +Prevent unexpected charges from log spikes: |
| 311 | + |
| 312 | +```typescript |
| 313 | +const workspace = new LogAnalyticsWorkspace(stack, "workspace", { |
| 314 | + name: "cost-controlled", |
| 315 | + location: "eastus", |
| 316 | + resourceGroupId: resourceGroup.id, |
| 317 | + workspaceCapping: { |
| 318 | + dailyQuotaGb: 50, // Stop at 50GB/day |
| 319 | + }, |
| 320 | +}); |
| 321 | +``` |
| 322 | + |
| 323 | +### 4. Apply Consistent Tags |
| 324 | + |
| 325 | +```typescript |
| 326 | +const workspace = new LogAnalyticsWorkspace(stack, "workspace", { |
| 327 | + name: "my-workspace", |
| 328 | + location: "eastus", |
| 329 | + resourceGroupId: resourceGroup.id, |
| 330 | + tags: { |
| 331 | + environment: "production", |
| 332 | + "cost-center": "platform-team", |
| 333 | + "managed-by": "terraform", |
| 334 | + }, |
| 335 | +}); |
| 336 | +``` |
| 337 | + |
| 338 | +## Troubleshooting |
| 339 | + |
| 340 | +### Common Issues |
| 341 | + |
| 342 | +1. **Retention Out of Range**: Ensure `retentionInDays` is between 30 and 730. |
| 343 | + |
| 344 | +2. **Invalid Capacity Reservation Level**: Valid values are 100, 200, 300, 400, 500, 1000, 2000, 5000. |
| 345 | + |
| 346 | +3. **Name Validation**: Workspace names must be 4-63 characters, start and end with alphanumeric, and contain only letters, numbers, and hyphens. |
| 347 | + |
| 348 | +4. **API Version Errors**: Ensure the version is 2023-09-01 or 2022-10-01. |
| 349 | + |
| 350 | +## Related Constructs |
| 351 | + |
| 352 | +- [`ResourceGroup`](../azure-resourcegroup/README.md) - Azure Resource Groups |
| 353 | +- [`DiagnosticSettings`](../azure-diagnosticsettings/README.md) - Send logs to this workspace |
| 354 | +- [`MetricAlert`](../azure-metricalert/README.md) - Create alerts based on workspace metrics |
| 355 | + |
| 356 | +## Additional Resources |
| 357 | + |
| 358 | +- [Azure Log Analytics Documentation](https://learn.microsoft.com/en-us/azure/azure-monitor/logs/log-analytics-overview) |
| 359 | +- [Log Analytics Pricing](https://azure.microsoft.com/en-us/pricing/details/monitor/) |
| 360 | +- [Azure REST API Reference](https://learn.microsoft.com/en-us/rest/api/loganalytics/) |
| 361 | +- [Terraform CDK Documentation](https://developer.hashicorp.com/terraform/cdktf) |
| 362 | + |
| 363 | +## Contributing |
| 364 | + |
| 365 | +Contributions are welcome! Please see the [Contributing Guide](../../CONTRIBUTING.md) for details. |
| 366 | + |
| 367 | +## License |
| 368 | + |
| 369 | +This project is licensed under the MIT License - see the [LICENSE](../../LICENSE) file for details. |
0 commit comments