LATEST UPDATES

How to Deploy Snowpark Python with Snowflake CoCo – Step by Step Guide

Why Snowpark Python and Snowflake CoCo are a Game Changer

Data engineers and analysts are constantly looking for ways to run Python code directly where the data lives. Snowpark Python brings the power of Python libraries to Snowflake’s cloud data platform, eliminating data movement and reducing latency. However, deploying Snowpark workloads can be tricky without a reliable CI/CD pipeline.

Enter Snowflake CoCo (Continuous Collaboration) – Snowflake’s native, Git‑centric deployment framework. CoCo automates code versioning, testing, and promotion across environments, giving teams confidence that their Snowpark Python jobs run exactly as intended.

Prerequisites Before You Start

  • Snowflake account with the ACCOUNTADMIN role (or a role with CREATE COMPUTE POOL privilege).
  • Python 3.9+ installed locally.
  • Git repository hosted on GitHub, GitLab, or Bitbucket.
  • Snowflake CLI (snowsql) configured for authentication.
  • Basic knowledge of Docker (optional but recommended for local testing).

Having these in place ensures a smooth setup and avoids common roadblocks.

Step 1: Set Up a Snowpark Python Project

Start by creating a new directory for your project and initializing a Python virtual environment.

mkdir snowpark-demo
cd snowpark-demo
python -m venv venv
source venv/bin/activate
pip install "snowflake-snowpark-python[default]"

Next, scaffold a minimal main.py that reads a table, transforms data, and writes results back to Snowflake.

from snowflake.snowpark import Session

def get_session():
    connection_parameters = {
        "account": "your_account",
        "user": "your_user",
        "password": "your_password",
        "role": "your_role",
        "warehouse": "your_warehouse",
        "database": "your_db",
        "schema": "your_schema"
    }
    return Session.builder.configs(connection_parameters).create()

def run():
    session = get_session()
    df = session.table("RAW_DATA")
    result = df.filter(df["value"] > 10).select("id", "value")
    result.write.save_as_table("PROCESSED_DATA", mode="overwrite")
    session.close()

if __name__ == "__main__":
    run()

This script will serve as the core of your Snowpark Python job.

Step 2: Initialize Snowflake CoCo in Your Repo

Snowflake CoCo relies on a .coco configuration file placed at the root of the repository. Create the file and define the environments you need – typically dev, test, and prod.

{
  "project": "snowpark-demo",
  "environments": {
    "dev": {
      "role": "DEV_ROLE",
      "warehouse": "DEV_WH",
      "database": "DEV_DB",
      "schema": "PUBLIC"
    },
    "test": {
      "role": "TEST_ROLE",
      "warehouse": "TEST_WH",
      "database": "TEST_DB",
      "schema": "PUBLIC"
    },
    "prod": {
      "role": "PROD_ROLE",
      "warehouse": "PROD_WH",
      "database": "PROD_DB",
      "schema": "PUBLIC"
    }
  }
}

Commit the file and push it to your remote repository. CoCo will read this metadata to determine where to deploy the code.

Step 3: Create a Snowflake Stage for Artifacts

A stage acts as a landing zone for packaged Python files. Run the following SQL commands in Snowflake to set up a private internal stage.

CREATE OR REPLACE STAGE my_coco_stage;
GRANT USAGE ON STAGE my_coco_stage TO ROLE DEV_ROLE;
GRANT USAGE ON STAGE my_coco_stage TO ROLE TEST_ROLE;
GRANT USAGE ON STAGE my_coco_stage TO ROLE PROD_ROLE;

Make sure each environment’s role has the proper permissions.

Step 4: Package the Python Code

CoCo expects a zipped package that includes main.py and a requirements.txt. Create the requirements file:

snowflake-snowpark-python[default]==1.7.0

Then zip everything:

zip -r snowpark_demo.zip main.py requirements.txt

Upload the zip to the stage using snowsql:

snowsql -q "PUT file://snowpark_demo.zip @my_coco_stage AUTO_COMPRESS=FALSE"

Step 5: Define the CoCo Deployment Pipeline

In your repository, add a .github/workflows/deploy.yml (or the equivalent for GitLab CI). Below is a GitHub Actions example that triggers on pushes to main and on pull‑request merges.

name: Deploy Snowpark Python via CoCo

on:
  push:
    branches: [ main ]
  pull_request:
    types: [ closed ]
    branches: [ main ]

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.9'
      - name: Install Snowflake CLI
        run: pip install snowflake-cli
      - name: Authenticate to Snowflake
        env:
          SNOWFLAKE_ACCOUNT: ${{ secrets.SNOW_ACCOUNT }}
          SNOWFLAKE_USER: ${{ secrets.SNOW_USER }}
          SNOWFLAKE_PASSWORD: ${{ secrets.SNOW_PASSWORD }}
        run: snowsql -a $SNOWFLAKE_ACCOUNT -u $SNOWFLAKE_USER -p $SNOWFLAKE_PASSWORD -q "SELECT CURRENT_VERSION();"
      - name: Run CoCo Deploy
        run: |
          coco deploy --env prod --stage my_coco_stage --package snowpark_demo.zip

This pipeline logs into Snowflake, verifies connectivity, and then calls the coco deploy command to push the package to the production environment.

Step 6: Validate the Deployment

After the pipeline finishes, open Snowflake Worksheet and run a simple query to ensure the stored procedure (or task) created by CoCo exists.

SHOW PROCEDURES LIKE 'SNOWPARK_DEMO%';

If the procedure appears, trigger it manually or schedule it as a Snowflake task:

CREATE OR REPLACE TASK run_snowpark_demo
  WAREHOUSE = PROD_WH
  SCHEDULE = 'USING CRON 0 * * * * UTC'
AS CALL SNOWPARK_DEMO();

Monitor the task history to confirm successful runs.

Best Practices for Ongoing Maintenance

  • Version your packages. Include a semantic version number in the zip filename (e.g., snowpark_demo_v1.2.0.zip) and update the .coco file accordingly.
  • Use environment‑specific secrets. Store Snowflake credentials in GitHub Secrets, Azure Key Vault, or AWS Secrets Manager, never in plain text.
  • Automated testing. Add a unit‑test stage that runs pytest against a local Snowpark session before the deploy step.
  • Monitoring. Enable Snowflake’s Resource Monitors to catch runaway compute usage caused by a mis‑behaving Python job.
  • Rollback strategy. Keep the previous package in the stage and tag the corresponding Git commit. CoCo can redeploy an older version with a single command.

Conclusion – Start Deploying Snowpark Python Today

Deploying Snowpark Python with Snowflake CoCo removes manual steps, enforces version control, and accelerates data‑driven innovation. By following the six steps above, you’ll have a repeatable, automated pipeline that scales from development to production.

Ready to supercharge your Snowflake workloads? Try the workflow now and share your results in the comments. Need help customizing the pipeline for your organization? Contact our experts for a free consultation.

Leave a Reply

Your email address will not be published. Required fields are marked *