pulumi / pulumi

Pulumi - Infrastructure as Code in any programming language 🚀
https://www.pulumi.com
Apache License 2.0
21.83k stars 1.12k forks source link

Create exports within an `Output.apply` lambda function #9545

Open christophermaier opened 2 years ago

christophermaier commented 2 years ago

Hello!

Issue details

I'd like to conditionally export data based on the value of a pulumi.Output value. Unfortunately, it does not appear to be possible at the moment.

Ideally, this is the kind of code I would like to write:

def _export_if_triggered(trigger_mode: str) -> None:
    if trigger_mode != "none":
        pulumi.export(name, {"webhook-url": pipeline.webhook_url})
    else:
        pulumi.log.info(f"Sorry, {repo_name} is not triggered")

pipeline.provider_settings.trigger_mode.apply(_export_if_triggered)

(For context, I'm creating a CI/CD pipeline, and if it is configured to be triggered in response to code changes in Github, I would like to export the pipeline's webhook URL so I can wire it up properly in another Pulumi project.)

At the moment (using Pulumi 3.31.0) this does not work. Using the code above, I see the pulumi.log output, but nothing is exported.

Affected area/feature

SDKs, generally speaking.

mikhailshilkov commented 2 years ago

Would you be opposed to exporting an empty value in case when you don't need to export the URL? If this case, you should return a value or None from you apply and export that result unconditionally. That's the approach we recommend currently instead of side-effectful apply callbacks.

christophermaier commented 2 years ago

@mikhailshilkov For the time being, I've moved my logic elsewhere to export things conditionally. I think I can work around this; I was just a bit surprised that this didn't seem to work. Generally speaking, I also like to avoid side-effects in apply callbacks (and I tell everyone on my team to not try and do things like create resources within callbacks). That being said, I've also seen Pulumi engineers say that it's actually OK to create resources inside callbacks (with the caveat that those resources won't show up in a preview, which is understandable). I assumed that if I can create a resource from inside a callback, then certainly I could export a value. So it's not totally clear where the dividing line is.

Logging is arguably a side effect, too, and that works (though I understand that's a bit different internally, given the planning/execution distinction with resources). I raise this not to be pedantic, but just to further illustrate that the distinction between what is and is not allowed in a callback is not always very clear.

If possible, it might be useful to emit some kind of warning message whenever an export (or any other "bad" operation) is done from inside an apply callback, just for visibility and clarity.

Thanks for your reply and suggestion; much appreciated 🙏