More struggle with `Output` :sweat_smile: ``` d...
# python
r
More struggle with
Output
😅
Copy code
def _setup_alert_manager_config(
        self, sns_topics: list[SnsTopic]
    ) -> AlertManagerDefinition:
        ......
        return pulumi.Output.json_dumps(alert_manager_config_dict).apply(
            lambda config_json_str: self._create_alert_manager_definition(
                config_json_str
            )
        )

    def _create_alert_manager_definition(
        self, alert_manager_config_json_str: str
    ) -> AlertManagerDefinition:
        config = {}
        ......
Towards the end of the stack, we call
_setup_alert_manager_config
. When running
pulumi preview
, it doesn't reach the
lambda
in
pulumi.Output.json_dumps
. Any idea? Is there a way for me to force waiting for the
pulumi.Output.json_dump
to finish?
If I add
run_with_unknowns=True
to the
apply
, then it runs and fails with this:
Copy code
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/__init__.py", line 339, in loads
        raise TypeError(f'the JSON object must be str, bytes or bytearray, '
    TypeError: the JSON object must be str, bytes or bytearray, not Unknown
So looks like
config_json_str
is
Unknown
. But why?
alert_manager_config_dict
's definition includes
Output
as one of the values in the dict. But that should be fine?
d
You should avoid creating resources within applies, as they're not guaranteed to run. An apply generally only runs when used
r
An apply generally only runs when used
What is the definition of
used
?
d
Used as an input to a resource or export
r
In our case, we need to serialize the JSON (includes
Output
), then deserialize it as YAML, which would be used as an input to create resource. Any suggestion how to do that?
Oh never mind - I guess I just need to move the resource creation out of the
apply
d
it'd be something like:
Copy code
AlertManagerDefinition("name", config=pulumi.Output.json_dumps(alert_manager_config_dict))
Remember that json is valid yaml if it's expecting a yaml string
r
Cool that seems to work. Thank you!
Yeah json does work. But yaml format looks much better in alert manager console 🙂
d
true. In that case
alert_manager_config_dict.apply(yaml.dumps)
👍 1
r
Still seems to have issue:
Copy code
alert_manager_defintion = pulumi.Output.json_dumps(alert_manager_config_dict).apply(
            lambda config_json_str: self._create_alert_manager_definition(
                config_json_str
            )
        )

        return AlertManagerDefinition(
            f"amp-rule-group-{self._stack}",
            workspace_id=self._amp_workspace.id,
            definition=alert_manager_defintion,
            opts=ResourceOptions(provider=self._provider),
        )
It ended up with empty string in
AlertManagerDefinition
.
_create_alert_manager_definition
returns the serialized yaml. I added logging in
_create_alert_manager_definition
, but nothing prints
Part of
alert_manager_config_dict
looks like this:
Copy code
"sns_configs": [
                    {
                        "topic_arn": topic.arn,
For some reason,
topic.arn.is_known()
shows false
Copy code
<Task finished name='Task-633' coro=<Output.__init__.<locals>.is_value_known() done, defined at /Users/dennis.pan.117/code/iac/observability/venv/lib/python3.11/site-packages/pulumi/output.py:121> result=False>
I think that causes the
apply
to not do anything
The same
topic.arn
is used by other resources creation, and those were all fine
d
It's supposed to recurse through a dictionary + list. Does
.apply(json.dumps)
work?
f
I would create custom
componentResource
to wrap this functionality and make this new custom resource a dependency for that the other resource.