Introduction to Pulumi: Infrastructure as Real Code

While Terraform uses a proprietary DSL (HCL), Pulumi allows you to define infrastructure using general-purpose programming languages like C#, TypeScript, Python, and Go. This brings the full power of your IDE, testing frameworks, and package managers to infrastructure.

Infrastructure in C#

Using .NET Core to define an Azure Resource Group and Storage Account.

using Pulumi;
using Pulumi.AzureNextGen.Resources.Latest;
using Pulumi.AzureNextGen.Storage.Latest;
using Pulumi.AzureNextGen.Storage.Latest.Inputs;

class MyStack : Stack
{
    public MyStack()
    {
        var resourceGroup = new ResourceGroup("resourceGroup", new ResourceGroupArgs
        {
            ResourceGroupName = "my-pulumi-rg",
            Location = "WestUS"
        });

        var storageAccount = new StorageAccount("sa", new StorageAccountArgs
        {
            ResourceGroupName = resourceGroup.Name,
            Sku = new SkuArgs { Name = SkuName.Standard_LRS },
            Kind = Kind.StorageV2
        });

        this.ConnectionString = Output.Format($"DefaultEndpointsProtocol=https;...;AccountName={storageAccount.Name}...");
    }

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

Benefits of Real Code

  • Loops & Conditionals: Use standard foreach, if, and LINQ immediately.
  • Abstraction: Create classes and methods to encapsulate infrastructure patterns.
  • Testing: Use NUnit or xUnit to unit test your infrastructure logic.

Key Takeaways

  • Pulumi treats infrastructure as software.
  • No new syntax to learn if you know .NET/TS.
  • Shared state is managed via the Pulumi Service (SaaS) or generic cloud storage.

Discover more from C4: Container, Code, Cloud & Context

Subscribe to get the latest posts sent to your email.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.