if that is true, how do I create stuff that's outs...
# python
l
if that is true, how do I create stuff that's outside of pulumi from resource outputs?
i
You can use
output.apply
anywhere, as long as the promise that gets returned is awaited by something (a resource or by exporting using
pulumi.export
)
l
so, ugh, i'd need to explicitly export this resource output? else I wont be able to use it in my custom code?
i
what do you mean “custom code”? in another process, or within the same Pulumi process?
l
in the same process. but my own code, not pulumi resource
just call my own function, right now I'm just doing async\await
and it fails on the first time, because parent resource is not there
i
can you post your code?
l
Copy code
if not pulumi.runtime.is_dry_run():
            asyncio.ensure_future(create_sql_database_from_existing())
Copy code
async def create_sql_database_from_existing():
    sql_client = client_factory('SqlManagementClient')
    async_db_create = await sql_client.databases.create_or_update(
        gen_name(BRANCH),
        gen_name(BRANCH),
        'TslTest',
        {
            'location': LOCATION,
            'create_mode': 'Copy',
            'sourceDatabaseId': DBID
        }
    )
    async_db_create.result()
so i'd need to reference sql_server.name in this function
to await for sql server created in the same config (ideally)
i
you would, yeah; you can use
apply
for this. does that not work?
l
no, it says cannot find server <pulumi.Output.output at xxxxxx>
hm, let me try again
i
can I see your code that does the apply?
l
Copy code
sql_server.name.apply(lambda name: name),
replaced one of the gen_name(BRANCH) with it
here's the error:
Copy code
Can not perform requested operation on nested resource. Parent resource '<pulumi.output.Output object at 0x7f3aac273860>' not found.
i
right -
apply
returns something of type
Output
if you want to do something with the actual value of name, you need to do it inside the lambda
with the “name” parameter
l
ah, like
"{}".format(name)
?
i
or just
name
, it’s type is “string”
but yeah
you have to do your operations on
name
inside the lambda
l
ok, i dont seem to understand
that didnt work
sql_server.name.apply(lambda name: "{0}".format(name)),
i
you need to call sql_client.databases.create_or_update inside the lambda
l
oh, the whole thing?
i
yeah
that is the only time you are allowed to observe the actual value of
name
l
and it** can be not async?
i
it can be, if you want
l
but doesnt have to?
i
you can call
apply
with an async function
you can also call it with a non-async function
both work fine
l
or wait, it will block, so i still need async
or wait. will it work in parallel like all the other resources?
i
it will not
it will block the whole program
l
ok, i'll leave it as async
i
if you do it asynchronously, it will proceed in parallel with the rest of the program
l
let me try, thank you
ok, that works flawlessly, thanks
i
no problem!
l
Copy code
await sql_server.name.apply(lambda name: sql_client.databases.create_or_update(
            gen_name(BRANCH),
            name,
            'TslTest',
            {
                'location': LOCATION,
                'create_mode': 'Copy',
                'sourceDatabaseId': DBID
            }
        )
    )
p
ahhh! The light dawns… OK, so I can call one of my functions within the async apply 🙂
partypus 1
so I’m exploring this. My main has the following code: if name == “__main__“:
oops, try again: async def main(): # do pulumi stuff if name = “__main__“: loop = asyncio.get_event_loop() asyncio.ensure_future(main(),loop=loop)
but main() doesn’t appear to be called and there’s no error from pulumi and only the Stack is created
So I’m back to being confused 😄
l
i dont think you need async around pulumi stuff?
for me, i'm just calling a function using asyncio.ensure_future
without creating a loop before hand
and I'm using await inside the function i'm calling
p
hmm, will try that
hmm, my first await inside the function works (it’s a call to get_vpc), but subsequent awaits don’t and the processing just ends, I’m wondering whetrher I need to gather all the awaitables and wait for all of them 🙂
I guess I’m confused about how the ensure_future which is run inside the Stack just seems to stop executing at a particular point…
l
yeah, i'm not a python developer, have basically zero understanding about async in python
p
same 🙂