创建Pulumi.AzureNative.OperationalInsights.Workspace后如何获取WorkspaceKey?

How to get the WorkspaceKey after creating a Pulumi.AzureNative.OperationalInsights.Workspace?

我用以下内容创建了一个 Pulumi.AzureNative.OperationalInsights.Workspace:

    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,
        }
      );

稍后我想将我的 ContainerGroup 连接到此工作区并需要 WorkspaceIdWorkspaceKey

我在哪里可以获得这些值?

WorkspaceId 派生自 workspace.CustomerId

WorkspaceKey 不是由工作区直接返回的,您需要使用 GetSharedKeys

检索它

这是为我成功预配的完整工作示例

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,
        });
    }
}