Automate Your Infrastructure with Automation API and C#
Posted on
Currently available in public preview, Pulumi’s Automation API enables you to provision your infrastructure programmatically using the Pulumi engine. Today, we are excited to announce C# support for Automation API, enabling .NET developers to automate infrastructure deployments, create complex orchestration workflows, build custom ops tooling, and build cloud frameworks. Read more about the Automation API here.
Using Automation API in .NET
The Pulumi.Automation
NuGet package exposes a LocalWorkspace
for creating and managing Pulumi Stacks, and a WorkspaceStack
that is a programmatic representation of a Stack for updating, refreshing, previewing, and destroying cloud resources. The Automation API makes it trivial to run Pulumi programs inline:
var program = PulumiFn.Create(() =>
{
var bucket = new Pulumi.Aws.S3.Bucket("s3-website-bucket");
return new Dictionary<string, object?>
{
["bucket_name"] = bucket.BucketName,
};
});
var stackArgs = new InlineProgramArgs("projectName", "stackName", program);
using var stack = await LocalWorkspace.CreateOrSelectStackAsync(stackArgs);
await stack.Workspace.InstallPluginAsync("aws", "v3.30.1");
var result = await stack.UpAsync();
var bucketName = result.Outputs["bucket_name"];
You can use your existing Pulumi projects and invoke them from the Automation API:
var stackArgs = new LocalProgramArgs("stackName", "C:\path\to\pulumi\project\dir");
using var stack = await LocalWorkspace.CreateOrSelectStackAsync(stackArgs);
await stack.UpAsync();
Since the Automation API can be invoked and debugged like any other code, it enables you to put together complex deployment workflows such as a blue-green deployment model:
using var workspace = await LocalWorkspace.CreateAsync(new LocalWorkspaceOptions
{
Program = PulumiFn.Create<ApplicationStack>(), // use your existing Pulumi.Stack implementation
ProjectSettings = new ProjectSettings("projectName", ProjectRuntimeName.Dotnet),
});
// your "blue" production code is already running
var green = await WorkspaceStack.SelectAsync("green", workspace);
await green.UpAsync();
// do your cutover to "green" logic
// and then teardown "blue"
var blue = await WorkspaceStack.SelectAsync("blue", workspace);
await blue.DestroyAsync();
See full Automation API examples in C#. Let us know if you’re using Automation API on Twitter or in our Community Slack.