How to query a stack resource using an automation API?
TypeScriptTo query a Pulumi stack resource using the Pulumi Automation API, you can leverage the
listStackOutputs
method to retrieve the outputs of the stack.This method will provide you with a list of all resources in the stack and their current state. You can use this information to examine specific resources and their properties.
Here is a TypeScript program illustrating the process:
import { LocalWorkspace } from "@pulumi/pulumi/x/automation"; async function main() { // Create an instance of the Pulumi Automation LocalWorkspace const ws = LocalWorkspace.create({ workDir: "./project" }) // Use the instance to select a particular stack const stack = await ws.selectStack("my-stack"); // Query the outputs of the stack const outputs = await stack.outputs(); // Outputs are a map of output keys to values for (const [key, value] of Object.entries(outputs)) { console.log(`Output: ${key}, Value: ${value.value}`); } } main().catch(err => console.log(err));
In this program:
- An instance of
LocalWorkspace
is created pointing to a directory that contains the Pulumi project (./project
). - The
selectStack
method is used to select a specific stack (my-stack
). - The
outputs
method is used to retrieve the outputs of the stack, storing each output key and its corresponding value.
Please note, you need to replace
"./project"
and"my-stack"
with your Pulumi project directory and your stack name respectively. The project directory is the directory that contains your Pulumi.yaml file.- An instance of