Skip to main content

Advanced use cases

Programmatically determining the environment

Sometimes you will want to adjust your app logic depending on the environment the app is running in. For example, you might want to write to DuckDB when the app is running locally, and to Snowflake when the app is running in the Tower cloud. If you were using Snowflake in both cases, you could have solved this by having different secrets values in your local and production environments. But when you want to use different databases, libraries, or connectors, you need to use conditional logic and define entirely different secrets.

Tower allows you to access the environment your code is running in through the special TOWER_ENVIRONMENT variable.

An app with this code

import os

tower_env = os.getenv("TOWER_ENVIRONMENT")

print("You are in {tower_env}!".format(tower_env=tower_env))

will output the following, when ran with --local parameter

tower run --local         
...
You are in local!

and will output the following, when run without the --local parameter

tower run        
✔ Scheduling run... Done!
Success! Run #2 for app `datafusion-transform` has been scheduled
tower apps logs datafusion-transform#2
✔ Fetching logs... Done!
You are in default!