Hey all Have a Pulumi Output array of objects (ro...
# python
f
Hey all Have a Pulumi Output array of objects (route53 hosted_zones) that looks like this:
Copy code
~ hosted_zones: [
      ~ [0]: {
                arn              : "arn:aws:route53:::hostedzone/Z06813069DDF4BVP5B49"
                comment          : "Hosted Zone for <http://env.example.com|env.example.com>"
                force_destroy    : false
                id               : "Z06813069DDF4BVP5B49"
                name             : "<http://env.example.com|env.example.com>"
                name_servers     : [
                    [0]: "<http://ns-1074.awsdns-09.org|ns-1074.awsdns-09.org>"
                    [1]: "<http://ns-1944.awsdns-52.co.uk|ns-1944.awsdns-52.co.uk>"
                    [2]: "<http://ns-475.awsdns-53.com|ns-475.awsdns-53.com>"
                    [3]: "<http://ns-948.awsdns-52.net|ns-948.awsdns-52.net>"
                ]
                tags_all         : {
                    provisioner: "pulumi"
                }
                urn              : "urn:pulumi:all::shared::aws:route53/zone:Zone::<http://env.example.com|env.example.com>"
                zone_id          : "Z06813069DDF4BVP5B49"
            }
      ~ [1]: {
                arn              : "arn:aws:route53:::hostedzone/Z05663573HLFBZ66YBFM2"
                comment          : "Hosted Zone for env.example.link"
                force_destroy    : false
                id               : "Z05663573HLFBZ66YBFM2"
                name             : "env.example.link"
                name_servers     : [
                    [0]: "<http://ns-1311.awsdns-33.org|ns-1311.awsdns-33.org>"
                    [1]: "<http://ns-1733.awsdns-22.co.uk|ns-1733.awsdns-22.co.uk>"
                    [2]: "<http://ns-191.awsdns-21.com|ns-191.awsdns-21.com>"
                    [3]: "<http://ns-786.awsdns-36.net|ns-786.awsdns-36.net>"
                ]
                tags_all         : {
                    provisioner: "pulumi"
                }
                urn              : "urn:pulumi:all::shared::aws:route53/zone:Zone::env.example.link"
                zone_id          : "Z05663573HLFBZ66YBFM2"
            }
    ]
I want to get, for example, the
zone_id
where
name
=
<http://env.example.com|env.example.com>
My thought was to search the array for the index, for example, where
name
=
<http://env.example.com|env.example.com>
, in this case
[0]
and then get the the value of
zone_id
at that index (similar to this https://stackoverflow.com/a/19868472) However, since this is a Pulumi Output object, I can’t iterate it like that:
Copy code
TypeError: 'Output' object is not iterable, consider iterating the underlying value inside an 'apply'
So I attempted to first convert the object to json, using a render function (which has worked for me when needing a Pulumi Output object as a string, like this https://stackoverflow.com/a/73558276/18101704) but that doesn’t work either:
Copy code
def get_shared_hosted_zones_to_string(shared_hosted_zones):
            return json.dumps(shared_hosted_zones)

        def get_zone_index(shared_hosted_zones_string, name):
            return next(index for index, element in enumerate(shared_hosted_zones_string)
                        if element["name"] == name)

        # For each domain, create Hosted Zone
        # i.e. (<http://uat.env.example.com|uat.env.example.com>, uat.env.example.link)
        for domain, name in domains.items():
            hosted_zone = route53.Zone(
                resource_name=f"{env_stack}.{domain_prefix}.{name}",
                name=f"{env_stack}.{domain_prefix}.{name}",
                comment=f"Hosted Zone for {env_stack}.{domain_prefix}.{name}",
                tags=TAGS,
                opts=ResourceOptions(protect=True)
            )

            shared_hosted_zones_string = Output.all(shared_hosted_zones).apply(
                lambda shared_hosted_zones: get_shared_hosted_zones_to_string(shared_hosted_zones))

            # select the hosted zone id where shared_hosted_zones.[].name == domain name
            # i.e. which object in the hosted_zones array has name : "<http://env.example.com|env.example.com>",
            #      because need to get that zone_id
            shared_hosted_zone = shared_hosted_zones_string[get_zone_index(
                shared_hosted_zones_string, domain_prefix + name)]
shared_hosted_zones_string
is of type Output though. I don’t fully understand how this works here, but not in this case? Any ideas?
b
you basically need to run the iteration inside an
apply
f
Like this?:
Copy code
shared_hosted_zone_ids = Output.all(shared_hosted_zones).apply(
                lambda shared_hosted_zones: zone["zone_id"] for zone in shared_hosted_zones if zone["name"] == f"{domain_prefix}.{name}")
but still get
Copy code
TypeError: 'Output' object is not iterable, consider iterating the underlying value inside an 'apply'
b
it’s hard to follow the code a little, but if you’re getting that error you’re trying to iterate over an async value
f
ohhhhh, oh, right. Don’t iterate over an async value, within an async value. Got it, thanks! Sorry for asking, my bad.
b
no need to apologise for asking questions, tat’s why we’re here!