Is it possible to export an `Output` wrapped value...
# getting-started
p
Is it possible to export an
Output
wrapped value? I can't do
pulumi.export(name='export-name', value=x)
because
x
is of type
Output
, but when I rewrite that to use
x.apply(fn)
where
fn
calls
pulumi.export
the name & value don't appear with the other exported values. Tossing a
print
statement in there reveals in the diagnostics that it is being run, and the correct name/value pairs get through, but despite that they don't seem to get exported.
m
Definitely. You’ll probably want to do something like this, though, with
export
outside of the apply: https://github.com/pulumi/templates/blob/master/serverless-azure-python/__main__.py#L139-L144
e
export of outputs should work, is should be fine to pass it directly to the
export
method no need for apply
p
I've now solved the issue I was having. Thanks for the help. The issue was because I was trying to export attributes from a resource object created by
pulumi_awsx
rather than
pulumi_aws
. With the latter I would get a non-
Output
object like
TaskDefinition
which would have exportable attributes such as its ARN. With
pulumi_awsx
I would get e.g. a
FargateTaskDefinition
that has a
Output
wrapped
task_definition
attribute, which has the ARN and other exportable attributes. So exporting
task_definition.task_definition.arn
works. The problem occurred because the code I was using was trying to automatically export several common attributes, such as
arn
,
name
,
id
, etc. When the attribute didn't exist for a given object (e.g.
TaskDefinition
has no
url
), it would suppress the
AttributeError
and continue, but because the
Output
wrapped objects are evaluated within callbacks the error suppression did not work. To work around that I moved the error suppression into a callback that I would then apply to the
Output
object:
Copy code
def _get_exportable(
    obj: Any,  # noqa: ANN401
) -> Iterable[tuple[str, None | str | pulumi.Output[str]]]:
    is_output = isinstance(obj, pulumi.Output)
    for key in STANDARD_EXPORT_KEYS:

        def _get_attr(
            x: Any,  # noqa: ANN401
            k: str = key,
        ) -> None | str | pulumi.Output[str]:
            with suppress(AttributeError):
                return getattr(x, k)  # type: ignore
            return None

        yield key, obj.apply(_get_attr) if is_output else _get_attr(obj)
The results from this would then be exported like so:
Copy code
for key, value in _get_exportable(obj):
        pulumi.export(name=f"{prefix}{key}", value=value)