Using pulumi python sdk to retrieve resource's attributes

49 Views Asked by At

I feel stupid. I've been trying to figure this out for a few hours - totally clueless.

I've set up several services through pulumi on aws in a successful manner.

Each of these services has an id (s3 buckets), or an endpoint (rds database) that other applications will require.

While I can manually set those, I would like to fetch those through the python sdk.

I would assume that is possible - but I just cant seem to do it. I can retrieve 'em by running:

    result = subprocess.run(["pulumi", "stack", "output", "--json", "--stack", stack_name], capture_output=True, text=True)

But not by using the pulumi python library.

Can anyone provide a simple functional example of how to do it?

ty

1

There are 1 best solutions below

0
epattaro On

Took me a while.

Need to have the Pulumi.yaml file in the project repository.

Than,

import pulumi
from pulumi.automation import LocalWorkspace


def get_property(property):
    organization = ...
    project = ...
    stack = ...

    stack_name = f"{organization}/{project}/{stack}"

    # Create a workspace with a specified working directory
    workspace = LocalWorkspace(work_dir=os.getcwd())
    stack = pulumi.automation.Stack.select(stack_name=stack_name, workspace=workspace)

    # Get the stack outputs
    outputs = stack.outputs()

    # Fetch the specific resource
    resource_property = outputs.get(property)
    return resource_property.value