Skip to content

Instantly share code, notes, and snippets.

@lukehoban
Last active May 19, 2023 05:31
Show Gist options
  • Save lukehoban/b5ec07deb0cbfb1b6b020500dc6d5e05 to your computer and use it in GitHub Desktop.
Save lukehoban/b5ec07deb0cbfb1b6b020500dc6d5e05 to your computer and use it in GitHub Desktop.
Pulumi without variables
import pulumi
import pulumi_aws as aws
from typing import Optional, Dict, Tuple
import asyncio
all_resources: Dict[Tuple[str, str], pulumi.Resource] = {}
def transform(args: pulumi.ResourceTransformationArgs) -> Optional[pulumi.ResourceTransformationResult]:
all_resources[(args.name, args.type_)] = args.resource
return None # do not actually modify the resource
pulumi.runtime.register_stack_transformation(transform)
async def get_resource(type: str, name: str, prop: str):
while True:
res = all_resources.get((name, type))
if res is not None:
return res.__dict__[prop]
await asyncio.sleep(1000)
# User code
# -------------------------------------------------------------------------------
aws.s3.BucketObject("o", bucket=get_resource("aws:s3/bucket:Bucket", "b", "id"))
aws.s3.Bucket("b")
@lukehoban
Copy link
Author

Quite a few things that can be improved and cleaned up here. But this works as a PoC for using Pulumi without variables, and as a result, allowing out-of-order resource declarations.

@jf
Copy link

jf commented May 19, 2023

thank you for this! If I want to understand a bit more about what's going on under the hood there (specifically pulumi.runtime and what a transformation does and why we need def transform), where would I go?

I am struggling to find documentation on pulumi.runtime. I was able to find https://www.pulumi.com/docs/reference/pkg/nodejs/pulumi/pulumi/runtime/#registerStackTransformation ... but (a) this is a NodeJS-specific doc and (b) even the NodeJs doc is short on concepts and explanation.

@lukehoban
Copy link
Author

There are docs on transformations and specifically stack transformations used here at https://www.pulumi.com/docs/concepts/options/transformations/#stack-transformations.

This is a slight abuse of that feature, since we aren’t actually using it to transform the resource. But it’s a tool that is available today that can enable intercepting all resource construction calls.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment