Home > Software design >  How to get the WorkspaceKey after creating a Pulumi.AzureNative.OperationalInsights.Workspace?
How to get the WorkspaceKey after creating a Pulumi.AzureNative.OperationalInsights.Workspace?

Time:03-02

I create a Pulumi.AzureNative.OperationalInsights.Workspace with the following:

    name = "workspace";
    var workspace = 
      new Workspace
      (
        name, 
        new WorkspaceArgs
        {
          Location = resourceGroup.Location,
          ResourceGroupName = resourceGroup.Name,
          RetentionInDays = 30,
          Sku = new WorkspaceSkuArgs
          {
            Name = WorkspaceSkuNameEnum.PerGB2018,
          },
          Tags =
                {
                    { "tag1", "val1" },
                },
          WorkspaceName = name,
        }
      );

Later I want to connect my ContainerGroup to this workspace and need the WorkspaceId and WorkspaceKey.

Where can I get those values?

CodePudding user response:

The WorkspaceId is derived from the workspace.CustomerId

The WorkspaceKey isn't returned directly by the workspace, you need to retrieve it using GetSharedKeys

Here's a full working example that provisions successfully for me

using Pulumi;
using Pulumi.AzureNative.ContainerInstance;
using Pulumi.AzureNative.ContainerInstance.Inputs;
using Pulumi.AzureNative.Resources;
using Pulumi.AzureNative.OperationalInsights;
using Pulumi.AzureNative.OperationalInsights.Inputs;

class MyStack : Stack
{
    public MyStack()
    {
        // Create an Azure Resource Group
        var resourceGroup = new ResourceGroup("example");

        // create the operational insights workspace
        var workspace =
          new Workspace
          (
            "example",
            new WorkspaceArgs
            {
                Location = resourceGroup.Location,
                ResourceGroupName = resourceGroup.Name,
                RetentionInDays = 30,
                Sku = new WorkspaceSkuArgs
                {
                    Name = WorkspaceSkuNameEnum.PerGB2018,
                },
                WorkspaceName = "example",
            }
          );

        // retrieve the workspace shared key
        // you may want to make this a secret
        var workspaceSharedKeys = Output.Tuple(resourceGroup.Name, workspace.Name).Apply(items =>
            GetSharedKeys.InvokeAsync(new GetSharedKeysArgs
            {
                ResourceGroupName = items.Item1,
                WorkspaceName = items.Item2,
            }));

        // provision the containergroup
        var containerGroup = new ContainerGroup("example", new ContainerGroupArgs
        {
            ContainerGroupName = "example",
            Containers =
            {
                new ContainerArgs
                {
                    Image = "nginx",
                    Name = "example",
                    Ports =
                    {
                        new ContainerPortArgs
                        {
                            Port = 80,
                        },
                    },
                    Resources = new ResourceRequirementsArgs
                    {
                        Requests = new ResourceRequestsArgs
                        {
                            Cpu = 1,
                            MemoryInGB = 1.5,
                        },
                    },
                },
            },
            Diagnostics = new ContainerGroupDiagnosticsArgs
            {
                LogAnalytics = new LogAnalyticsArgs
                {
                    LogType = "ContainerInsights",

                    WorkspaceId = workspace.CustomerId,
                    WorkspaceKey = workspaceSharedKeys.Apply(key => key.PrimarySharedKey!),
                },
            },
            Location = "WestUS",
            OsType = "Linux",
            ResourceGroupName = resourceGroup.Name,
        });
    }
}

  • Related