Deploying Streamlit Applications using Snowpark Container Services¶
In the previous chapter we learnt how to deploy Streamlit application in Snowflake with SiS. In this chapter we will explore how to containerize the application and deploy it using Snowpark Container Services(SPCS).
In this chapter, we will:
- Add a Dockerfile file to containerize the application
- Create Compute Pool
- Create Image Repository
- Build and Deploy the application
Prerequisites¶
- You have completed the previous chapter on Deploying to SiS
- Snow CLI is installed and configured
- Docker is installed and configured
- Notebook has been imported into your Snowflake Account and ready to use.
Preparing for SPCS Deployment¶
To be able to deploy our Penguins ML app as a container onto SPCS we need to
- Create compute pool
- Create an image repository
- Build and Push the container image to the image repository
- Deploy service
Create Compute Pool¶
All SPCS containers run using the required compute size, check the documentation for more details on available compute pool sizes.
Let us create one for this tutorial:
export ST_ML_APP_COMPUTE_POOL=st_ml_app_xs
snow spcs compute-pool create $ST_ML_APP_COMPUTE_POOL \
--family CPU_X64_XS
Let us describe the created compute pool:
The compute pool is another Snowflake resource, it has all the usual operations like list, describe, etc., check the cheatsheet for more information.
Creating SPCS Objects¶
Important
- SPCS containers cant be run as
ACCOUNTADMIN
hence we need to create a new role and use that role to run the containers.
Let us switch to our notebook that we used earlier and run the following cells under the section Snowpark Container Services(SPCS),
sql_current_user_role
sql_current_database
sql_current_user
spcs_variables
spcs_objects
After successful execution of the cells you would have created,
- A Role named
st_ml_app
to create Snowpark Container Services - A Schema named
images
on DBST_ML_APP
to hold the image repository. - A Warehouse
st_ml_app_spcs_wh_s
which will be used to run query from services.
Building and Pushing the Image¶
Get image registry URL,
Create Image Repository¶
Create an image repository if not exist,
snow spcs image-repository create st_ml_apps \
--database='st_ml_app' \
--schema='images' \
--role='st_ml_app' \
--if-not-exists
Get the image repository st_ml_apps
and store it the environment variable $IMAGE_REPO
,
export IMAGE_REPO=$(snow spcs image-repository url st_ml_apps \
--database='st_ml_app' \
--schema='images' \
--role='st_ml_app')
Update the App¶
Edit and update the $TUTORIAL_HOME/sis/streamlit_app.py
with,
streamlit_app.py | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
|
Building the Application Container Image¶
Important
This uses the docker
daemon, if you have not installed this yet, please install Docker before proceeding further.
Let us login to the SPCS image registry,
Let us build, tag and push the $IMAGE_REPO/penguins_app
to the $IMAGE_REGISTRY_URL
,
Let us list all images in repository st_ml_apps
,
snow spcs image-repository list-images st_ml_apps \
--database='st_ml_app' \
--schema='images' \
--role='st_ml_app'
Let us export the image FQN to a variable for easy reference and substitution,
Deploy Service¶
Create a SPCS service specification file,
Note
Replace $IMAGE_REPO_NAME
with actual value. If you are on Linux/macOS you can use envsubst like,
Create a new directory named work
and create the following file in it,
service-spec.yaml | |
---|---|
Create a Service,
snow spcs service create st_ml_app \
--compute-pool=$ST_ML_APP_COMPUTE_POOL \
--spec-path=work/service-spec.yaml \
--if-not-exists \
--database='st_ml_app' \
--schema='images' \
--role='st_ml_app'
Service Status¶
Check service status,
Note
It will take a few minutes for the service to be in RUNNING
status:
snow spcs service describe st_ml_app \
--database='st_ml_app' \
--schema='images' \
--role='st_ml_app'\
--format json | jq '.[0].status'
Service Endpoints¶
List the service endpoint for the service st_ml_app
,
Note
It will take few minutes for the Endpoint URL to be ready.
snow spcs service list-endpoints st_ml_app \
--database='st_ml_app' \
--schema='images' \
--role='st_ml_app' --format=json | jq '.[0].ingress_url'
Open the application by navigating to the URL provided in the previous command, then authenticate to access the Penguins ML application.
Cleanup¶
To clean up the services created as part of this demo run the cell spcs_cleanup
on the notebook.
References¶
Quickstarts¶
- Snowflake Developers::Quickstart
- Snowflake Developers::Getting Started With Snowflake CLI
- Intro to Snowpark Container Services
- Build a Data App and run it on Snowpark Container Services
Documentation¶
- Snowflake CLI
- Execute Immediate Jinja Templating
- Snowpark Container Services
- https://docs.snowflake.com/en/sql-reference/sql/create-compute-pool
- https://docs.snowflake.com/en/developer-guide/snowpark-container-services/specification-reference