https://prefect.io logo
p

Peyton Runyan

01/31/2021, 10:58 PM
Is there a good way to parameterize an environment variable in a flow? Below is my user config. I'd love to be able to pass
mode
as a parameter instead of messing with it in the config and re-registering my flow.
Copy code
mode = "hard_coded_mode"
[sql_server]
    server = server
    driver = driver
    dsn = "MYMSSQL"
    user = "${sql_server.${mode}.user}"
    database = "${sql_server.${mode}.database}"

    [sql_server.dev]
        user =  dev user
        database = "dev db

    [sql_server.hub]
        user = app user
        database = app db

    [sql_server.prod]
        user = prod user
        database = prod db
Worst comes to worst I guess I could just kill the agent and restart it with
mode
as an environment variable?
k

Kyle Moon-Wright

02/01/2021, 5:06 PM
Hey @Peyton Runyan, With your setup so far, it might work to set the environment variable in your python script with something like this (not sure if these work):
Copy code
mode = Parameter("Environment Mode", default="dev")
os.environ["PREFECT__CONFIG__MODE"] = mode
Otherwise, setting the values directly in your script might be best:
Copy code
mode = Parameter("Mode", default="dev")

prefect.config.sql_server.user = prefect.config.sql_server.${mode}.user

prefect.config.sql_server.database = prefect.config.sql_server.${mode}.database
You’ll likely need to adjust for your use case, but I definitely think you can accomplish something like this. More information on switching configurations can be found here.
🙏 1