Skip to content

Instantly share code, notes, and snippets.

Last active November 9, 2021 02:04
Show Gist options
  • Save komalali/8b90ab0726e49067989d5219c438e01b to your computer and use it in GitHub Desktop.
Save komalali/8b90ab0726e49067989d5219c438e01b to your computer and use it in GitHub Desktop.
Structured Output from Automation API
import sys
from pulumi import automation as auto
import pulumi_random as random
# This is the pulumi program in "inline function" form
def pulumi_program():
random.RandomString("my_rando_string", length=16, special=True)
def print_id_on_create(evt: auto.EngineEvent):
# Check if it's a ResOutputsEvent. This event is emitted after an operation on a resource has completed.
if evt.res_outputs_event:
meta = evt.res_outputs_event.metadata
# Only print when a resource of type "random:index/randomString:RandomString" is created.
if meta["type"] == "random:index/randomString:RandomString" \
and meta["op"] == auto.OpType.CREATE:
print(f"{meta['type']} created")
# contains the new state for the resource.
# We can get whatever output we want from `outputs`.
print(f"resource_id: {meta['new']['outputs']['id']}")
print(f"urn: {meta['new']['urn']}")
# To destroy our program, we can run python destroy
destroy = False
args = sys.argv[1:]
if len(args) > 0:
if args[0] == "destroy":
destroy = True
project_name = "print_on_create"
stack_name = "dev"
# create or select a stack matching the specified name and project.
# this will set up a workspace with everything necessary to run our inline program (pulumi_program)
stack = auto.create_or_select_stack(stack_name=stack_name,
print("stack initialized")
print("installing plugins... ", end="")
stack.workspace.install_plugin("random", "v4.3.1")
print("refreshing stack... ", end="")
if destroy:
print("destroying stack... ", end="")
print("updating stack...")
up_res = stack.up(on_event=print_id_on_create)
print("stack update complete")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment