To associate a storage account with an Azure Log Analytics Workspace, you need to use the Azure native provider in Pulumi. Specifically, we'll leverage the LinkedStorageAccount
resource, which represents a storage account linked to a Log Analytics workspace for logs. Also, we will assume you have already created a Storage Account and a Log Analytics Workspace. If not, we can also create them using the StorageAccount
and Workspace
resources respectively.
Here's how you do it in Pulumi with TypeScript:
- First, you'll need to import the necessary packages from Pulumi and set up the Azure provider.
- Then, you'll create a
LinkedStorageAccount
resource, specifying the workspace name, resource group, data source type, and storage account IDs you want to associate.
Below is a Pulumi program that demonstrates these steps:
import * as pulumi from "@pulumi/pulumi";import * as azure_native from "@pulumi/azure-native";// Creating a storage account (if it does not exist)const storageAccount = new azure_native.storage.StorageAccount("myStorageAccount", { // Required properties resourceGroupName: "myResourceGroup", sku: { name: azure_native.storage.SkuName.Standard_LRS, }, kind: azure_native.storage.Kind.StorageV2, location: "West US", // Replace with your desired location});// Creating a Log Analytics Workspace (if it does not exist)const logAnalyticsWorkspace = new azure_native.operationalinsights.Workspace("myLogAnalyticsWorkspace", { // Required properties resourceGroupName: "myResourceGroup", location: "West US", // Replace with your desired location sku: { name: "PerGB2018", // Choose an appropriate SKU },});// Associating the storage account with the log analytics workspace for logsconst linkedStorageAccount = new azure_native.operationalinsights.LinkedStorageAccount("myLinkedStorageAccount", { // Required properties resourceGroupName: "myResourceGroup", workspaceName: logAnalyticsWorkspace.name, dataSourceType: "CustomLogs", // The type of data that the storage account will hold for the workspace storageAccountIds: [storageAccount.id], // The array of storage account resource IDs to associate});// Exporting the IDs of the created resources. Replace with your desired outputs.export const storageAccountId = storageAccount.id;export const logAnalyticsWorkspaceId = logAnalyticsWorkspace.id;export const linkedStorageAccountId = linkedStorageAccount.id;
Explanation:
Imports:
- We import the Pulumi SDK and the Azure Native Pulumi package to interact with Azure resources.
Storage Account:
- We create an Azure storage account using the
StorageAccount
class, which will be associated with the Log Analytics Workspace. TheresourceGroupName
,sku
,kind
, andlocation
are required parameters.
- We create an Azure storage account using the
Log Analytics Workspace:
- We create a Log Analytics Workspace resource using the
Workspace
class. We specify itsresourceGroupName
,location
, andsku
.
- We create a Log Analytics Workspace resource using the
Linked Storage Account:
- We then create the
LinkedStorageAccount
resource, associating the storage account we created with the Log Analytics Workspace. TheworkspaceName
,dataSourceType
, andstorageAccountIds
are provided to link the resources.
- We then create the
Exports:
- Finally, we export the IDs of the created resources. These outputs can be helpful for cross-reference or in CI/CD pipelines.
Make sure the resourceGroupName
and location
are correct for your use case, and adjust them as necessary for your Azure environment. The storageAccountIds
property is an array because you can associate multiple storage accounts with a single workspace.
After you write this code in a .ts
file, you can deploy your infrastructure using Pulumi's CLI by running pulumi up
. This will prompt you for confirmation detailing the resources that will be created or modified.
Important to Note:
- Ensure you have the Azure provider configured in your Pulumi project.
- Be cautious about the data you store in the Storage Account as it can incur costs based on data ingestion and retention policies.
- The SKU for the Log Analytics workspace and Storage Account should be chosen based on your needs and budget considerations.
- You would need appropriate permissions to create these resources in the Azure subscription.
Remember that this code is meant to run in a TypeScript environment where Pulumi is installed and configured to communicate with your Azure subscription.