https://pulumi.com logo
Title
c

cool-belgium-78445

08/16/2021, 2:55 PM
Hi there! I am using Pulumi to create some custom infrastructure and am having trouble accessing the outputs of my resources. On the docs (https://www.pulumi.com/docs/intro/concepts/resources/#dynamic-resource-outputs) it says that what I am doing should be correct, but there is also little mention of how to actually access the outputs, so I could be wrong. This is what my resource and provider look like; am I doing anything wrong?
class AccountProvider(ResourceProvider):
    def __init__(self, props):
        self.parent_client = Client(props["sid"], props["auth_token"])
    def create(self, props):
        subaccount = self.parent_client.api.accounts.create(friendly_name="{}-{}".format(props["name"], stack))
        return CreateResult(id_=subaccount.sid, outs={'sid':subaccount.sid, 'auth_token':subaccount.auth_token})
    def delete(self, id, props):
        self.parent_client.api.accounts(id).update(status="closed")

class Account(Resource):
    sid: Output[str]
    auth_token: Output[str]
    def __init__(self, name: str, props,  opts: Optional[ResourceOptions] = None):
        super().__init__(AccountProvider(props), name, {"name":name, 'sid':None, 'auth_token':None}, opts)

new_account = Account("new_account1", props=AccountCredentials(sid=account_sid, auth_token=auth_token))
w

witty-candle-66007

08/16/2021, 3:29 PM
Are you saying that when you try to access, say,
new_account.sid
, it throws an error or something?
c

cool-belgium-78445

08/16/2021, 3:31 PM
Yes
w

witty-candle-66007

08/16/2021, 3:31 PM
Can you share the error message?
c

cool-belgium-78445

08/16/2021, 3:34 PM
Yes. When I access it just with
new_account.sid
everything works fine but when I pass that into another resource I get the following:
TypeError: 'Output' object is not callable
 error: an unhandled error occurred: Program exited with non-zero exit code: 1
1
Whenever I pass the value into anything that actually consumes the value I get
TypeError: 'Output' object is not iterable, consider iterating the underlying value inside an 'apply'
w

witty-candle-66007

08/16/2021, 4:08 PM
Are you familiar with the
apply
stuff in Pulumit: https://www.pulumi.com/docs/intro/concepts/inputs-outputs/
c

cool-belgium-78445

08/16/2021, 4:09 PM
I've been taking a look at it. I tried using it and got the same error though
I setup a basic http server just to look at all of the outputs, and I tried posting to it with this
<http://requests.post|requests.post>("<http://localhost:5000>", {"f":new_account.sid.apply(lambda sid: f"{sid}")})
, and that did not work
TypeError: 'Output' object is not iterable, consider iterating the underlying value inside an 'apply'
same error
w

witty-candle-66007

08/16/2021, 5:09 PM
I’m not sure what’s happening. FWIW, I hacked out a python dynamic resource example (https://github.com/MitchellGerdisch/pulumi_work/tree/main/py-dynamic-resource) that uses an output from the dynamic resource (acl) as an input to creating another resource (S3 bucket) and it works as expected. I’m wondering if the output from the API call you are making and returning is not as expected?
c

cool-belgium-78445

08/16/2021, 5:15 PM
That can't be it because my server has no incoming requests.
w

witty-candle-66007

08/16/2021, 5:27 PM
I’m referring to the create in the dynamic resource:
def create(self, props):
        subaccount = self.parent_client.api.accounts.create(friendly_name="{}-{}".format(props["name"], stack))
Are you sure
subaccount
and more importantly
subaccount.sid
is defined? or that
new_account.sid
is defined in the calling code?
c

cool-belgium-78445

08/16/2021, 5:29 PM
I know that all of the subaccount stuff is defined, and when I check the stack export everything exists as it should
w

witty-candle-66007

08/16/2021, 5:32 PM
Meaning you have a
pulumi.export("new_account", new_account)
after the
new_account
declaration, and see that new_account has the various properties?
c

cool-belgium-78445

08/16/2021, 5:32 PM
I don't, I believe it does that automatically? when I run
pulumi stack export
I everything outputs like it should
I'm not sure if you have to call pulumi.export with a custom resource though
w

witty-candle-66007

08/16/2021, 5:35 PM
That should be a valid representation of things. But I would suggest throwing that
pulumi.export
in your code and see what it shows - just to see what outputs the code sees.
c

cool-belgium-78445

08/16/2021, 6:44 PM
Ok, I'll give that a try
Everything checks out there too
w

witty-candle-66007

08/16/2021, 7:16 PM
And the error is due to this line, right?
<http://requests.post|requests.post>("<http://localhost:5000>", {"f":new_account.sid.apply(lambda sid: f"{sid}")}),
If so, what if you do:
new_account.sid.apply(lambda sid: <http://request.post|request.post>("<http://localhost:5000>", {"f":sid}))
c

cool-belgium-78445

08/16/2021, 7:37 PM
Interesting, I'll test that
That seems to do the trick, Thanks so much for all of your help!
w

witty-candle-66007

08/16/2021, 7:43 PM
Good - sorry I didn’t pick up on that line earlier in the thread.
c

cool-belgium-78445

08/16/2021, 7:49 PM
No problem!