Home > Mobile >  Getting StorageAccount-AccessKey in Pulumi Azure Native
Getting StorageAccount-AccessKey in Pulumi Azure Native

Time:09-29

I am trying to switch from "classic" Azure to Azure Native in Pulumi. One of my requirements is to retrieve the Connectionstring and AccessKey to my newly created StorageAccount.

On classic Azure I received those fields by

var connectionString=ClassicStorageAccount.PrimaryConnectionString;
var accessKey=ClassicStorageAccount.PrimaryAccessKey;

where ClassicStorageAccount is of type Pulumi.Azure.Storage.Account

Now after creating a storage account with Azure Native:

var account=new Pulumi.AzureNative.Storage.StorageAccount("myMagicAccount", new StorageAccountArgs{...});

I am struggling to retrieve the AccessKey.

I am able to retrieve the connectionstring using

var connectionstring=account.StorageAccount.PrimaryEndpoints.Apply(q=>q.Web);

but none of the properties from PrimaryEndpoints or PrivateEndpointConnections seem to contain my required AccessKey.

The docs for StorageAccount on Azure Native did not help me on this approach

CodePudding user response:

There's a listStorageAccountKeys method you can use.

using System.Threading.Tasks;
using Pulumi;
using Pulumi.AzureNative.Resources;
using Pulumi.AzureNative.Storage;
using Pulumi.AzureNative.Storage.Inputs;

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

        // Create an Azure resource (Storage Account)
        var storageAccount = new StorageAccount("sa", new StorageAccountArgs
        {
            ResourceGroupName = resourceGroup.Name,
            Sku = new SkuArgs
            {
                Name = SkuName.Standard_LRS
            },
            Kind = Kind.StorageV2
        });

        // Export the primary key of the Storage Account
        this.PrimaryStorageKey = Output.Tuple(resourceGroup.Name, storageAccount.Name).Apply(names =>
            Output.CreateSecret(GetStorageAccountPrimaryKey(names.Item1, names.Item2)));
    }

    [Output]
    public Output<string> PrimaryStorageKey { get; set; }

    private static async Task<string> GetStorageAccountPrimaryKey(string resourceGroupName, string accountName)
    {
        var accountKeys = await ListStorageAccountKeys.InvokeAsync(new ListStorageAccountKeysArgs
        {
            ResourceGroupName = resourceGroupName,
            AccountName = accountName
        });
        return accountKeys.Keys[0].Value;
    }
}

Code above is from the template Pulumi uses when you run pulumi new azure-csharp and can be found in the templates repository

  • Related