|
| 1 | +--- |
| 2 | +title: "Automate Your Infrastructure with Automation API and C#" |
| 3 | +date: 2021-03-08 |
| 4 | +meta_desc: "C# developers can programmatically build infrastructure (with out a CLI) using the Pulumi Automation API package. " |
| 5 | +meta_image: automation_api.png |
| 6 | +authors: |
| 7 | +- joshua-studt |
| 8 | +- sophia-parafina |
| 9 | +tags: |
| 10 | +- Automation API |
| 11 | +- C# |
| 12 | +- csharp |
| 13 | +- dotnet |
| 14 | +- .NET |
| 15 | +--- |
| 16 | + |
| 17 | +{{% notes type="info" %}} |
| 18 | +Joshua Studt is a Solutions Architect at Financial Independence Group and a Pulumi Community member who contributed the C# package for Automation API. |
| 19 | +{{% /notes %}} |
| 20 | + |
| 21 | +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]({{< relref "/blog/automation-api" >}}). |
| 22 | + |
| 23 | +## Using Automation API in .NET |
| 24 | + |
| 25 | +The `Pulumi.Automation` [NuGet package](https://www.nuget.org/packages/Pulumi.Automation) exposes a `LocalWorkspace` for creating and managing Pulumi [Stacks]({{< relref "/docs/intro/concepts/stack" >}}), 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: |
| 26 | + |
| 27 | +```csharp |
| 28 | +var program = PulumiFn.Create(() => |
| 29 | +{ |
| 30 | + var bucket = new Pulumi.Aws.S3.Bucket("s3-website-bucket"); |
| 31 | + |
| 32 | + return new Dictionary<string, object?> |
| 33 | + { |
| 34 | + ["bucket_name"] = bucket.BucketName, |
| 35 | + }; |
| 36 | +}); |
| 37 | + |
| 38 | +var stackArgs = new InlineProgramArgs("projectName", "stackName", program); |
| 39 | +using var stack = await LocalWorkspace.CreateOrSelectStackAsync(stackArgs); |
| 40 | + |
| 41 | +await stack.Workspace.InstallPluginAsync("aws", "v3.30.1"); |
| 42 | +var result = await stack.UpAsync(); |
| 43 | +var bucketName = result.Outputs["bucket_name"]; |
| 44 | +``` |
| 45 | + |
| 46 | +You can use your existing Pulumi projects and invoke them from the Automation API: |
| 47 | + |
| 48 | +```csharp |
| 49 | +var stackArgs = new LocalProgramArgs("stackName", "C:\path\to\pulumi\project\dir"); |
| 50 | +using var stack = await LocalWorkspace.CreateOrSelectStackAsync(stackArgs); |
| 51 | + |
| 52 | +await stack.UpAsync(); |
| 53 | +``` |
| 54 | + |
| 55 | +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: |
| 56 | + |
| 57 | +```csharp |
| 58 | +using var workspace = await LocalWorkspace.CreateAsync(new LocalWorkspaceOptions |
| 59 | +{ |
| 60 | + Program = PulumiFn.Create<ApplicationStack>(), // use your existing Pulumi.Stack implementation |
| 61 | + ProjectSettings = new ProjectSettings("projectName", ProjectRuntimeName.Dotnet), |
| 62 | +}); |
| 63 | + |
| 64 | +// your "blue" production code is already running |
| 65 | +
|
| 66 | +var green = await WorkspaceStack.SelectAsync("green", workspace); |
| 67 | +await green.UpAsync(); |
| 68 | + |
| 69 | +// do your cutover to "green" logic |
| 70 | +// and then teardown "blue" |
| 71 | +
|
| 72 | +var blue = await WorkspaceStack.SelectAsync("blue", workspace); |
| 73 | +await blue.DestroyAsync(); |
| 74 | +``` |
| 75 | + |
| 76 | +See full Automation API examples in [C#](https://github.com/pulumi/automation-api-examples/tree/main/dotnet). Let us know if you're using Automation API on [Twitter](https://twitter.com/PulumiCorp) or in our [Community Slack](https://slack.pulumi.com/). |
0 commit comments