Hi there! I am using Pulumi to create some custom ...
# getting-started
c
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?
Copy code
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
Are you saying that when you try to access, say,
new_account.sid
, it throws an error or something?
c
Yes
w
Can you share the error message?
c
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:
Copy code
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
Are you familiar with the
apply
stuff in Pulumit: https://www.pulumi.com/docs/intro/concepts/inputs-outputs/
c
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
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
That can't be it because my server has no incoming requests.
w
I’m referring to the create in the dynamic resource:
Copy code
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
I know that all of the subaccount stuff is defined, and when I check the stack export everything exists as it should
w
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
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
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
Ok, I'll give that a try
Everything checks out there too
w
And the error is due to this line, right?
Copy code
<http://requests.post|requests.post>("<http://localhost:5000>", {"f":new_account.sid.apply(lambda sid: f"{sid}")}),
If so, what if you do:
Copy code
new_account.sid.apply(lambda sid: <http://request.post|request.post>("<http://localhost:5000>", {"f":sid}))
c
Interesting, I'll test that
That seems to do the trick, Thanks so much for all of your help!
w
Good - sorry I didn’t pick up on that line earlier in the thread.
c
No problem!