https://pulumi.com logo
#python
Title
# python
p

polite-iron-99873

03/10/2020, 7:46 PM
Is there a way to get an attribute from a resource using its resource name? For example, I create a bucket after a reference to that bucket.
Copy code
some_other_resource = foo.resource("foores", s3_bucket_id = bucket1.id, ...)
...
bucket1 = s3.bucket("bucket1", ...args)
This would be likened to Interpolation in terraform. Or tropospheres ability to use Ref('<resource_name>').
f

faint-table-42725

03/10/2020, 7:54 PM
Could you describe your use-case a bit more? I’d like to better understand why you can’t:
Copy code
bucket1 = s3.bucket("bucket1", ...args)
...
some_other_resource = foo.resource("foores", s3_bucket_id = bucket1.id, ...)
p

polite-iron-99873

03/10/2020, 8:08 PM
What I want to do is this:
Copy code
some_other_resource = foo.resource("foores", s3_bucket_id = bucket1.id, ...)
...
bucket1 = s3.bucket("bucket1", ...args)
I am trying to convert Troposphere code to be used with pulumi and Tropospere allows for cases like what I showed, Terraform uses interpolation to tackle this problem as well
f

faint-table-42725

03/10/2020, 8:57 PM
i think i’m just trying to understand why the order cannot be switched?
p

polite-iron-99873

03/10/2020, 9:11 PM
The order cannot be switched because of python.
Copy code
UnboundLocalError: local variable 'bucket1' referenced before assignment
    error: an unhandled error occurred: Program exited with non-zero exit code: 1
Is there a way to utilize the resource name? As in:
Copy code
some_other_resource = foo.resource("foores", s3_bucket_id = "bucketResourceName".id, ...)
...
bucket1 = s3.bucket("bucketResourceName", ...args)
f

faint-table-42725

03/10/2020, 9:54 PM
no, i meant the order in terms of bucket1 comes first
Copy code
bucket1 = s3.bucket("bucketResourceName", ...args)
...
some_other_resource = foo.resource("foores", s3_bucket_id = "bucketResourceName".id, ...)
sorry if i am totally misunderstanding
but i don’t see why some_other_resource cannot come afterward? bucket1 has no dependency on some_other_resource
p

polite-iron-99873

03/11/2020, 12:33 PM
It could come afterward in this situation, but in certain conditionals in my code, I can't always reverse the order. I mainly just want to know pre-referencing is possible, or if I could reference a resource based on the resource_name somehow. Its fine if the answer is no, but I can't find anything online that says if I have access to the resource_names.
f

faint-table-42725

03/11/2020, 5:20 PM
Unfortunately, you can’t do it via referencing the resource_name. I think if you really need to do this kind of thing, your best bet would be to not use autonaming and force the name in the subsequent statement when you create the bucket.
3 Views