Skip to content

Snowflake-Labs/apj-build-2024-slack-demo

Repository files navigation

Cortex Analyst with Slack

A simple demo showcasing the integration of Snowflake Cortex Analyst with Slack.

The demo also covers ChatOps aspects using the Snowflake Python API for setting up Snowflake resources such as generating RSA Keypair, setting user public keys, creating config, databases, tables, stages, upload semantic models, etc. All containerized for an effective DevOps.

Prerequisites

Note

Check out direnv for a neat and clean way to manage your environment variables.

Setup Environment

Snowflake Config Environment

The following variables are used to generate RSA KeyPair and set th Snowflake user SNOWFLAKE_USER public key and finally generate the Snowflake config.toml to be used connecting with Snowflake.

The config docker compose service will use .env

# application log level
APP_LOG_LEVEL=DEBUG
# Snowflake Account to use 
SNOWFLAKE_ACCOUNT=your snowflake account id
# Snowflake User
SNOWFLAKE_USER=your snowflake user name
# Snowflake User Password
SNOWFLAKE_PASSWORD=your snowflake user password
# Snowflake Role - should be able to create DB, schema and objects under them, alter users, 
SNOWFLAKE_ROLE=ACCOUNTADMIN

Slack Bot Environment

cp $DEMO_HOME/.env.example $DEMO/.env

The slack-bot docker compose service will use .env.bot:

# application log level
APP_LOG_LEVEL=DEBUG
# Snowflake connection name to use
SNOWFLAKE_DEFAULT_CONNECTION_NAME="default"
# Slack API Bot Token
SLACK_BOT_TOKEN=your slack bot token
# Slack API App Token
SLACK_APP_TOKEN=your slack app token
# The private key path (defaulted to the one within container)
PRIVATE_KEY_FILE_PATH=/home/me/.snowflake/snowflake_user.p8
cp $DEMO_HOME/.env.bot.example $DEMO/.env.bot

Important

Update the $DEMO/.env and $DEMO/.env.bot to match your settings.

Setup(Easy Way)

With your environment files ready fire up docker

cd $DEMO_HOME
docker-compose up -d

Docker Compose Service::config

  • Config will setup Snowflake RSA KeyPair
  • Creates /home/me/.snowflake/config.toml to use RSA KeyPair

Docker Compose Service::slack-bot

  • Will start the slack-bot app
  • Uses the Snowflake Connection from /home/me/.snowflake/config.toml

Slack Commands

  • setup
  • cortalyst (Cortex Analyst :D)
  • cleanup

App Demo

Demo Video

Setup(Hard Way)

[!IMPORTANT]

  • The .env and .env.bot needs to be sourced before you start the bot
  • Setup RSA KeyPair Auth with Snowflake for your account, simple trick
# not use daemon mode so that you can watch the logs, kill the container after that
docker compose up config
# once you see it successful settings check for logs for message ` User keys configured and working`
./scripts/bin/docker-copy.sh

./scripts/bin/docker-copy.sh will copy the /home/me/.snowflake to $PWD/.snowflake. Set SNOWFLAKE_HOME to and all set.

Create Python Virtual Environment

Important

All code and packages have been tested with Python 3.11

python -m venv $DEMO_HOME/.venv

Install the required packages:

pip install -r requirements.txt

Slack App Manifest

The Slack App configuration and its manifest are available in manifest.json. You can use this while setting up your Slack App. It includes all permissions, commands, and other details needed to interact with Snowflake using the bot.

Start the Bot

Note

Ensure you have installed the Slack App in your workspace using the manifest

python app.py

References

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages