Managing Snowflake virtual warehouses with Python

You can use Python to manage Snowflake virtual warehouses, which are clusters of compute resources in Snowflake. For an overview of warehouses, see Virtual Warehouses.

The Snowflake Python API represents warehouses with two separate types:

  • Warehouse: Exposes a warehouse’s properties such as its name, size, type, and auto-resume and auto-suspend settings.

  • WarehouseResource: Exposes methods you can use to fetch a corresponding Warehouse object, suspend and resume the warehouse, and delete the warehouse.

Prerequisites

The examples in this topic assume that you’ve added code to connect with Snowflake and to create a Root object from which to use the Snowflake Python API.

For example, the following code uses connection parameters defined in a configuration file to create a connection to Snowflake:

from snowflake.core import Root
from snowflake.snowpark import Session

session = Session.builder.config("connection_name", "myconnection").create()
root = Root(session)
Copy

Using the resulting Session object, the code creates a Root object to use the API’s types and methods. For more information, see Connect to Snowflake with the Snowflake Python API.

Creating a warehouse

To create a warehouse, first create a Warehouse object, and then create a WarehouseCollection object from the API Root object. Using WarehouseCollection.create, add the new warehouse to Snowflake.

Code in the following example creates a Warehouse object that represents a warehouse called my_wh:

from snowflake.core import Root
from snowflake.core.warehouse import Warehouse

my_wh = Warehouse(
  name="my_wh",
  warehouse_size="SMALL",
  auto_suspend=600,
)
warehouses = root.warehouses
warehouses.create(my_wh)
Copy

The code creates a WarehouseCollection variable warehouses and uses WarehouseCollection.create to create a new warehouse in Snowflake.

Getting warehouse details

You can get information about a warehouse by calling the WarehouseResource.fetch method, which returns a Warehouse object.

Code in the following example gets information about a warehouse called my_wh:

from snowflake.core import Root
from snowflake.core.warehouse import Warehouse

my_wh = root.warehouses["my_wh"].fetch()
print(my_wh.to_dict())
Copy

Creating or updating a warehouse

You can update characteristics of an existing warehouse by setting properties of a Warehouse object that represents the warehouse, and then passing the object to Snowflake with the WarehouseResource.create_or_update method.

You can also pass a Warehouse object that describes a new warehouse when you want to create the warehouse.

Code in the following example sets the name, size, and auto-suspend setting of a warehouse and then updates the warehouse on Snowflake, creating the warehouse if it doesn’t yet exist:

from snowflake.core import Root
from snowflake.core.warehouse import Warehouse

my_wh = Warehouse(
  name="my_wh",
  warehouse_size="LARGE",
  auto_suspend=1800,
)
my_wh_res = root.warehouses["my_wh"]
my_wh_res.create_or_update(my_wh)
Copy

In this case, it changes the my_wh warehouse’s size to LARGE and its auto-suspend setting to 1800 if you previously created it with different properties.

Listing warehouses

You can list warehouses using the WarehouseCollection.iter method, which returns a PagedIter iterator of Warehouse objects.

Code in the following example lists warehouses whose name includes the text my and prints the name of each:

from snowflake.core import Root
from snowflake.core.warehouse import Warehouse, WarehouseCollection

warehouses: WarehouseCollection = root.warehouses
wh_iter = warehouses.iter(like="my%")  # returns a PagedIter[Warehouse]
for wh_obj in wh_iter:
  print(wh_obj.name)
Copy

Performing warehouse operations

You can perform common warehouse operations—such as suspending and resuming warehouses and aborting all queries on warehouses—with a WarehouseResource object.

Code in the following example suspends and resumes the my_wh warehouse, aborts all running or queued queries on the warehouse, and then deletes the warehouse:

from snowflake.core import Root
from snowflake.core.warehouse import Warehouse

my_wh_res = root.warehouses["my_wh"]

my_wh_res.suspend()
my_wh_res.resume()
my_wh_res.abort_all_queries()
my_wh_res.delete()
Copy