Apple AirPods 4 Wireless Earbuds, Bluetooth Headphones, Personalized Spatial Audio, Sweat and Water Resistant, USB-C Charging Case, H2 Chip, Up to 30 Hours of Battery Life, Effortless Setup for iPhone
7% OffSony PlayStation DualSense wireless controller – 30th Anniversary Limited Edition (Renewed Premium)
$168.95 (as of December 13, 2024 21:07 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)AWS Lambda provides a serverless computing service, allowing you to run code without provisioning servers. It supports Python as one of its runtimes. To connect your Lambda functions to Snowflake for data warehousing, you need to import the Snowflake connector and SnowSQL libraries.
This poses a challenge because the Lambda environment contains only the Python standard library by default. So we need to package any other dependencies along with our deployment bundle.
In this guide, we’ll cover the necessary steps to import and bundle the Snowflake Python connector (Snowflake-Connector-Python) and SnowSQL (SnowSQL) libraries for use in Lambda:
Overview:
- Set up a Python virtual environment
- Install Snowflake connector and SnowSQL
- Bundle as deployment package
- Upload deployment package to Lambda
- Import libraries and connect to Snowflake
Prerequisites:
- AWS Lambda console access
- Basic Lambda & Python knowledge
- Snowflake account credentials
Set Up a Virtual Environment and Install Dependencies
First, create a dedicated directory for your Lambda project. We’ll use a Python 3.7 virtual environment to mimic the Lambda runtime environment locally.
Steps:
Create project folder
$ mkdir snowflakeLambda
$ cd snowflakeLambda
JavaScriptCreate & activate Python 3.7 virtual env
$ python3.7 -m venv .venv
$ source .venv/bin/activate
JavaScriptUpgrade pip and setuptools
(.venv) $ pip install --upgrade pip setuptools
JavaScriptInstall Snowflake connector (latest)
(.venv) $ pip install snowflake-connector-python
JavaScriptInstall SnowSQL Python library
(.venv) $ pip install snowsql
JavaScriptThis installs the Snowflake libraries we want and their dependencies into our local virtual environment.
Bundle Dependencies into Deployment Package
AWS Lambda requires all function dependencies to be bundled together in a deployment package zip file. We need to create a bundle containing our function code and the Snowflake libraries.
Steps:
Write basic Lambda function code (e.g. lambda_function.py
)
import snowflake.connector
def lambda_handler(event, context):
...
JavaScript- In the project directory, create a folder
python
Copy virtual environment site-packages intopython
folder
(.venv) $ cp -R .venv/lib/python3.7/site-packages/* python
JavaScript- Zip the
python
folder to create a package
$ zip -r9 deployment.zip python/*
JavaScriptThis bundles all our libraries so Lambda can access them at runtime. We only include the contents under site-packages
, not the entire virtual environment.
Upload and Test Lambda Deployment Package
With our deployment bundle zipped, we can now upload it to Lambda and test importing the Snowflake modules.
Steps:
In Lambda console, create a new function
For runtime, select Python 3.7
Skip creating functions code from scratch
Under function package select “Upload a .zip file”
Upload the deployment.zip
bundle we created
Save function
In function code editor, add test code:
import snowflake.connector
import snowsql
print(snowflake.connector.__version__)
print(snowsql.__version__)
JavaScriptRun a test execution
Check logs to verify Snowflake connector versions print
This validates that Lambda now loads those libraries at runtime. We can begin coding our Snowflake integration!
Connecting to Snowflake from Lambda
With the foundations in place, we are ready to connect a Lambda function to our Snowflake account using the imported libraries.
Steps:
Import connector
import snowflake.connector
JavaScriptDefine a Snowflake connection function
def get_conn():
return snowflake.connector.connect(
user='<your_user>',
account='<your_account>',
private_key='<private_key>',
role='<role>',
warehouse='<warehouse>'
)
JavaScriptUtilize connection to execute query
def run_query():
conn = get_conn()
cs = conn.cursor()
cs.execute("SELECT current_version()")
one = cs.fetchone()
print(one[0])
JavaScriptBy bundling the libraries into our deployment package, we enabled Python code running on Lambda servers to interact directly with Snowflake!
There are many further possibilities for querying data, loading files, triggering notifications, and more by connecting Lambda serverless with Snowflake’s data warehouse services in this manner.
This cross-cloud platform integration grants efficient scaling, automation capabilities, and streamlined devops pipelines around your analytics stack.