Is there a canonical way of using a `PrefectSecret...
# ask-community
s
Is there a canonical way of using a
PrefectSecret
to add a new item to the
prefect.context
? I see that you can add to the global context by doing something like:
Copy code
with prefect.context(key='abc'):
    flow.run()
but it's not clear to me how to actually supply this value to the runtime context when my agents are deploying the flows and I'm not explicitly calling
flow.run()
. For context, I'm reading through the documentation on adding Slack handlers and I see that all you need to do is set a
SLACK_WEBHOOK_URL
secret in order for the buiilt-in
slack_notifier
to be able to post to your channel.
k
Hey @Sean Talia, you can pass it through spinning up the agent as an environment variable. Have you seen this on how to load the secret?
s
I'm not sure what you mean by "spinning up the agent as an environment variable" but I suppose it would be possible to set the env var
PREFECT__CONTEXT__SECRETS__SLACK_WEBHOOK_URL
at runtime via a task?
or is it not even possible to modify the context on the fly like that and the context is only read once when the flow is launched
k
oh sorry i meant “with” an environment variable like
prefect local agent start --env var1
If you want to set it on runtime, you would pass it through the RunConfig.
Copy code
DockerRun(env={"SOME_VAR": "value"})
Actually let me double check if you can use a secret with runconfig
s
oh yeah yeah I've done that before locally, I was hoping to avoid doing that in production 1) because I didn't want to have
var1
stashed on the host where I'm running the agent but also 2) I'd like for different teams at my org to notify different channels as they need and I was hoping that they could make those adjustments in their flow code rather than my having to configure a pattern for agents/labels to determine that
Oh yes if that could work I'd love to be able to pass that in (although similar to the question I posted the other day I'm wondering if this would result in the secret url getting loaded at registration-time and then supplied to the cloud in the flow metadata as plaintext)
k
Ok my bad. We can’t pass secrets to the runconfig…
😭 1
Yes this is tough. I believe you mentioned the only way to modify context at runtime already. Sounds like we can’t have it stashed on the host, especially cuz it’s dynamic. We can pull it with a Prefect secret…but how to we pass it to the slack_notifier? Let me think lol
👍 1
s
okay well at the very least I'm encouraged by the fact that the solution isn't obvious so my understanding of how prefect works can't be that terrible
k
Maybe you can pass the PrefectSecret directly to the
slack_notifier
. Check
slack_notifier
in the docs . It takes in a
webhook_secret
s
alright I will take a look at this!
oh yeah wow this totally seems like it should work...
alright i'll try and report back!
(thanks, I should have looked at the source for this to start with)
k
Nah man all good. Happy to help 🙂
s
okay, that worked like a charm! I might have to refactor my code a little bit if I want to turn this into a parameter but that was super easy, thanks for your help
👍 1