Manage Pulumi ESC resources in your automation workflows
Pulumi Automation API includes methods for interacting with Pulumi ESC Environments programmatically. This enables you to seamlessly integrate environment management into your automated workflows and build sophisticated custom tooling.
Automation API methods for ESC include:
addEnvironments(...): Add environments in order to your Pulumi stacks' import list.listEnvironments(): Retrieve a list of environments currently imported into your stack.removeEnvironment(environment): Remove a specific environment from your stack's import list.
These methods provide the building blocks for advanced automation scenarios such as dynamically configuring applications based on environments, managing environment dependencies, and integrating ESC into CI/CD pipelines.
If you haven't used Automation API before, get started here.
Full Pulumi ESC Automation API support — adding, listing, and removing environments — is available for TypeScript/JavaScript, Python, and Go. The .NET SDK and the Java SDK both support adding and removing environments but do not provide a method to list them. Here are some examples:
import * as pulumi from "@pulumi/pulumi";
import * as automation from "@pulumi/pulumi/automation";
async function manageEnvironments() {
const projectName = "myProject";
const stackName = "dev";
// Set up your stack options. Here we're using an empty program
// since we're only manipulating environments, not deploying resources.
const stackArgs: automation.InlineProgramArgs = {
stackName,
projectName,
program: async () => { },
};
// Create or select an existing stack.
const stack = await automation.LocalWorkspace.createOrSelectStack(stackArgs);
// Add environments to your stack's configuration.
// This is like adding them to the `imports` section of an ESC environment.
await stack.addEnvironments("env1", "env2");
// The stack's configuration now includes `env1` and `env2`.
// List the environments currently configured for the stack.
const environments = await stack.listEnvironments();
console.log("Current environments:", environments); // Output: ["env1", "env2"]
// Remove an environment from the stack's configuration.
await stack.removeEnvironment("env1");
// The stack's configuration now only includes `env2`.
// List environments again to confirm the removal.
const updatedEnvironments = await stack.listEnvironments();
console.log("Updated environments:", updatedEnvironments); // Output: ["env2"]
}
// Call the async function to execute.
manageEnvironments().catch(console.error);