Tutorial 3: Create a service and a job using the Snowflake Python APIs¶
Introduction¶
In Tutorial 1 and Tutorial 2, you use the SQL interface to create a Snowpark Container Services service and job. In this tutorial you use the Snowflake Python APIs to create the same service and job and thus explore using the Snowflake Python APIs to manage Snowpark Container Services resources.
The tutorial uses a Snowflake notebook to execute the Python code, but the code is independent of the notebook and you can execute the code in other environments.
1: Initial configuration¶
In this initial configure, you create a Snowflake notebook, import libraries you need, and define constants that are used by cells in the subsequent steps.
-
Create a Snowflake notebook.
- Create a notebook. For instructions, see Create a new notebook. Note that the Python environment you choose in the UI (Run on warehouse or Run on container) doesn’t matter.
- From the Packages drop-down menu, choose the “snowflake” package and install the latest version of the Snowflake Python APIs library.
- (Optional) Delete the cells provided in the notebook by default. As you follow the steps in this tutorial, you add Python cells to the notebook.
-
Create and run the cell to import Python libraries used by many cells in this tutorial.
-
Create and run the cell to define constants that you use in subsequent cells. The values provided below match Tutorials 1 and 2. You can optionally change these values.
2: Create Snowflake objects¶
Before you can create a service, you need Snowflake objects, such as a database, a user, a role, a compute pool, and an image repository. Some of these objects are account-scoped object that require administrative privileges to create them. The names of the objects created are defined in the preceding step.
2.1: Create account-scoped Snowflake objects¶
The following Python code creates these objects:
- Role (
test_role). You grant this role all the privileges required to create and use the service. In the code, you grant this role to the current user to enable the user to create and use the service. - Database (
tutorial_db). In the next step, you create a schema in this database. - Compute pool (
tutorial_compute_pool). Your service container executes in this compute pool. - Warehouse (
tutorial_warehouse). When the service connects to Snowflake and executes queries, this warehouse is used to execute the queries.
Create and run the cell to create these account-scoped objects using the ACCOUNTADMIN role. Note that the script creates resources only if they don’t exist. The comments in the code show the equivalent SQL statements.
As you create resources, the code also grants required privileges to the role (test_role) so the role can use these resources. Additionally, note that the echo service you create in this tutorial exposes one public endpoint. This public endpoint allows other users in your account to access the service from the public web (ingress). To create a service with a public endpoint, the role (test_role) must have the BIND SERVICE ENDPOINT privilege on the account.
2.2 Create schema-scoped objects¶
The Python code in this section uses the test_role role to create a schema and objects in that schema. You don’t need administrative privileges to create these resources.
- Schema (
data_schema). You create an image repository, service, and job in this schema. - Image repository (
tutorial_repository). You store your application image in this repository. - Stage (
tutorial_stage). The stage is created only for illustration. While not demonstrated in this tutorial, stages can be used to pass data into or collect data from your services.
Note that the script creates resources only if they don’t exist.
The Python code also prints out useful information about the repository (the repository URL) that you use when pushing your images to the repository.
3: Build an image and upload¶
You download locally the code as described in Tutorial 1, use Docker commands to build the image, and upload it to the image repository in your account.
-
Create and run the cell to obtain the hostname of your image registry and the URL to your image repository.
The Python code retrieves the image repository resource object (
repo), accesses the model object, and extracts the repository URL from it. -
Follow Tutorial 1 steps 1 and 2 to download the service code, build an image, and upload it to the repository.
-
Create and run the cell to verify the image is in the repository.
The code enumerates the images from the image repository resource (
repo) and prints theimage_pathfor each image.
4: Create a service¶
Create a service and a service function to communicate with the service.
-
Verify that the compute pool is ready. After you create a compute pool, it takes some time for Snowflake to provision all the nodes. Ensure that the compute pool is ready before creating a service, because service containers execute within the specified compute pool.
Create and run the cell to get the compute pool status:
The code fetches the compute pool model (
cpm) from the compute pool resource (cp) to retrieve the current compute pool state. If the compute pool is suspended, the code resumes the compute pool. The code loops, pausing for five seconds each time, until the compute pool is no longer in the STARTING or SUSPENDED state.The last line of output should be “IDLE” or “ACTIVE”, which indicates that the compute pool is ready to run your service. For more information, see Compute pool lifecycle. If the compute pool is not ready, your services can’t start.
-
Create and run the cell to create the echo service.
The code retrieves the repository URL, as done in the preceding step. The code then creates the
echo_serviceusing an inline specification and the image from the specified image repository.As you see from the Python code, it’s easy to parameterize the names of resources. The following is the equivalent SQL command that creates a service but doesn’t use parameters.
-
Run the cell to create a service function (
my_echo_function). A service function is one of the ways of using the service.The code calls the
createmethod on thefunctionscollection of theschemato create the service function (my_echo_function).
5: Use the service¶
In this section, you use the service as follows:
- Invoke the service function.
- Use a browser to interact with the service’s public endpoint.
-
Invoke the service function.
Snowflake sends a POST request to the service endpoint (
echoendpoint). Upon receiving the request, the service echoes the input string in the response.Output:
-
Access from a browser the public endpoint that the service exposes.
-
Get the URL of the public endpoint.
-
Paste the printed URL in a browser window. This causes the service to execute the
ui()function (seeecho_service.py).Note that the first time you access the endpoint URL, you will be asked to log in to Snowflake. For this test, use the same user that you used to create the service to ensure the user has the necessary privileges.
-
Enter the string “Hello” in the Input box, and press Return.
-
6: Create a job¶
In Tutorial 2, you use the SQL interface to create a Snowpark Container Services job. In this section, you create the same job using the Snowflake Python APIs.
-
Create and run the cell to obtain the hostname of your image registry and the URL to your image repository.
The Python code retrieves the image repository resource object (
repo), accesses the model object, and extracts the repository URL from it. -
Follow Tutorial 2 steps 1 and 2 to download the service code, build an image, and upload it to the repository.
-
Create and run the cell to verify the image is in the repository.
The code enumerates the images from the image repository resource (
repo) and prints theimage_pathfor each image. -
Create and run the cell to create the job.
The job runs the given query and stores the results in a table.
-
Run the following cell to review the result written to the table. This code uses Snowpark Python to query that table.
7: Clean up¶
-
Stop the service and drop it. After dropping the service, Snowflake by default automatically suspends the compute pool (assuming there are no other services and job services running). For more information, see compute pool lifecycle.
-
Drop the image repository to avoid paying for storage. Note that, if you have any other images stored in the repository they will be deleted.
-
Drop the schema. Dropping a schema also drops all objects in that schema. For this tutorial that includes the service, the function, the image repository, and the stage you created.
-
Instead of waiting for Snowflake to suspend your compute pool, you also can explicitly suspend the compute pool. In this case, Snowflake suspends any running services, and waits for any jobs running to finish, and then suspends the compute pool.
What’s next?¶
This tutorial demonstrates using Snowflake Python APIs to create and manage Snowpark Container Services services and jobs. For more information about the Snowflake Python APIs, see Snowflake Python APIs: Managing Snowflake objects with Python.