Snowflake CLI Quickstart

The Snowflake CLI, released publicly on March 2024 and available on GitHub, provides convenient utilities for deploying Snowflake resources.

The Snowflake CLI can deploy Snowflake Native Apps, deploy Streamlit apps, push Docker images to Snowpark Container Services, connect a Git repository to Snowflake, and also manage various other Snowflake resources. This is a significantly different set of features than the SnowSQL CLI, which mainly provides a SQL console.

In this quickstart guide we'll discuss how to install the Snowflake CLI and how to authenticate with a Snowflake instance. We'll also give a short description of the snowflake.yml file, which is used to configure how the Snowflake CLI deploys the resources defined in a project. Finally, we'll demonstrate how to deploy a simple Snowpark "hello world" function.

All code discussed in this article is also available at the following repository:

https://www.github.com/corbettanalytics/snowflake-cli-quickstart

Installation

The Snowflake CLI is available from PyPI and can be installed with pip.

pip install snowflake-cli-labs

Note that you need Python >= 3.8.

To verify your installation you can use the following command to check the installed version.

snow --version

You can also install command completion support for the Snowflake CLI, which makes it easier to use the various options and configuration parameters.

snow --install-completion

Connections

The Snowflake CLI looks for connection parameters in a file ~/.snowflake/config.toml. (There are other locations the Snowflake CLI will search as well - for more details see their help page here).

An example configuration file is shown below. It includes two connections: one for connecting with a password, the other for using a private key file.

default_connection_name = "dev"

[connections.dev]
account = "corbett-analytics"
user = "[email protected]"
warehouse = "compute_xs"
database = "snowflake_cli_demo"
schema = "prod"
password = "..."

[connections.dev-ssh]
account = "corbett-analytics"
user = "[email protected]"
warehouse = "compute_xs"
database = "snowflake_cli_demo"
schema = "prod"
authenticator = "SNOWFLAKE_JWT"
private_key_path = "/Users/tom/.ssh/snowflake.p8"

To test your connection is working, use the test command.

snow connection test

You can also test a specific connection by providing its name.

snow connection test dev-ssh

The snowflake.yml file

The Snowflake CLI uses the concept of a project, which is just a directory containing a snowflake.yml file.

The Snowflake CLI includes a scaffold command for generating a simple project, including a snowflake.yml file, as well as some simple Snowpark functions.

snow snowpark init

Below is a minimalist version of the snowflake.yml file. You can also use this code as a simple template.

definition_version: 1
snowpark:
  project_name: snowflake_cli_demo
  stage_name: snowflake_cli_demo.prod.demo_stage
  src: app
  functions:
    - name: hello
      database: snowflake_cli_demo
      schema: prod
      handler: functions.hello
      signature:
        - name: name
          type: string
      returns: string

Note the following important parameters:

Deploying a Snowpark Function

After defining a snowflake.yml project file and writing the code for a simple function, we can use the Snowflake CLI to build and deploy the Snowpark Function.

First we need to build the project.

snow snowpark build

Notice that this will create two files: app.zip and requirements.snowflake.txt. We recommend adding these build artifacts to your .gitignore as well.

Now we can upload the Snowpark Function to the database and schema defined in the snowflake.yml file using the deploy command.

snow snowpark deploy

To validate that the function was deployed successfully, we can run a simple query that uses the function. You could also run this query through any other interface that can run SQL on Snowflake.

snow sql --query "select snowflake_cli_demo.prod.hello('world');"

Updating a Snowpark Function

After a Snowpark Function has been deployed for the first time, we can make changes by following the same process. However, we need to use the --replace flag during the deploy step:

snow snowpark build
snow snowpark deploy --replace
snow sql --query "select snowflake_cli_demo.prod.hello('world');"

Conclusion

This Quickstart has shown how you can install and authenticate with the Snowflake CLI. We also showed how to deploy a simple Snowpark Function.

For more documentation on the Snowflake CLI, you can see the detailed tutorials and references on the Snowflake documentation site.