Review the New Project
Let’s review some of the generated project files:
Pulumi.yaml
defines the project.Pulumi.dev.yaml
contains configuration values for the stack you initialized.
Program.cs
with a simple entry point.
index.js
index.ts
__main__.py
main.go
MyStack.cs
Program.fs
MyStack.vb
import * as pulumi from "@pulumi/pulumi";
import * as resources from "@pulumi/azure-native/resources";
import * as storage from "@pulumi/azure-native/storage";
// Create an Azure Resource Group
const resourceGroup = new resources.ResourceGroup("resourceGroup");
// Create an Azure resource (Storage Account)
const storageAccount = new storage.StorageAccount("sa", {
resourceGroupName: resourceGroup.name,
sku: {
name: storage.SkuName.Standard_LRS,
},
kind: storage.Kind.StorageV2,
});
// Export the primary key of the Storage Account
const storageAccountKeys = pulumi.all([resourceGroup.name, storageAccount.name]).apply(([resourceGroupName, accountName]) =>
storage.listStorageAccountKeys({ resourceGroupName, accountName }));
export const primaryStorageKey = storageAccountKeys.keys[0].value;
import pulumi
from pulumi_azure_native import storage
from pulumi_azure_native import resources
# Create an Azure Resource Group
resource_group = resources.ResourceGroup("resource_group")
# Create an Azure resource (Storage Account)
account = storage.StorageAccount('sa',
resource_group_name=resource_group.name,
sku=storage.SkuArgs(
name=storage.SkuName.STANDARD_LRS,
),
kind=storage.Kind.STORAGE_V2)
# Export the primary key of the Storage Account
primary_key = pulumi.Output.all(resource_group.name, account.name) \
.apply(lambda args: storage.list_storage_account_keys(
resource_group_name=args[0],
account_name=args[1]
)).apply(lambda accountKeys: accountKeys.keys[0].value)
pulumi.export("primary_storage_key", primary_key)
package main
import (
"github.com/pulumi/pulumi-azure-native/sdk/go/azure/resources"
"github.com/pulumi/pulumi-azure-native/sdk/go/azure/storage"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)
func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
// Create an Azure Resource Group
resourceGroup, err := resources.NewResourceGroup(ctx, "resourceGroup", nil)
if err != nil {
return err
}
// Create an Azure resource (Storage Account)
account, err := storage.NewStorageAccount(ctx, "sa", &storage.StorageAccountArgs{
ResourceGroupName: resourceGroup.Name,
AccessTier: storage.AccessTierHot,
Sku: &storage.SkuArgs{
Name: storage.SkuName_Standard_LRS,
},
Kind: storage.KindStorageV2,
})
if err != nil {
return err
}
// Export the primary key of the Storage Account
ctx.Export("primaryStorageKey", pulumi.All(resourceGroup.Name, account.Name).ApplyT(
func(args []interface{}) (string, error) {
resourceGroupName := args[0].(string)
accountName := args[1].(string)
accountKeys, err := storage.ListStorageAccountKeys(ctx, &storage.ListStorageAccountKeysArgs{
ResourceGroupName: resourceGroupName,
AccountName: accountName,
})
if err != nil {
return "", err
}
return accountKeys.Keys[0].Value, nil
},
))
return nil
})
}
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;
}
}
This Pulumi program creates an Azure resource group and storage account and then exports the storage account’s primary key.
Note: In this program, the location of the resource group is set in the configuration setting azure-native:location
(check the Pulumi.dev.yaml
file). This is an easy way to set a global location for your program so you don’t have to specify the location for each resource manually. The location for the storage account is automatically derived from the location of the resource group. To override the location for a resource, simply set the location property to one of Azure’s supported locations.
Next, you’ll deploy your stack, which will provision a resource group and your storage account.