Is there a way to get an attribute from a resource...
# python
p
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
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
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
i think i’m just trying to understand why the order cannot be switched?
p
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
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
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
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.