This example demonstrates how to connect Snowpark to AWS Lambda locally using LocalStack. Snowpark allows you to query, process, and transform data in a variety of ways using Snowpark Python. In this example, we create a Lambda function that uses Snowpark to:
- Establish a connection to a local Snowflake database provided by LocalStack.
- Create a cursor object and execute a simple query to create a table.
- Insert rows and execute an insert query with
executemanyand a query to select data from the table. - Fetch the results and execute a query to get the current timestamp.
- A valid LocalStack for Snowflake license with access to the Snowflake Next preview. Your license provides a
LOCALSTACK_AUTH_TOKEN. - Docker with Docker Compose
- Python 3.12 and
pip awslocalandjq- Snowflake CLI (
snow), used bymake test
The sample runs two containers from docker-compose.yml:
localstack: LocalStack for AWS (localstack/localstack-pro) on port 4566. It runs the Lambda function.snowflake: the Snowflake emulator (localstack/snowflake-next) on host port 4567. It is Snowflake only, so it runs next to LocalStack for AWS. At startup it runsinit.sf.sql, which creates thetestdatabase and thetest_schemaschema the function connects to.
Both containers share the lambda-snowpark network, and LAMBDA_DOCKER_NETWORK puts the Lambda container on it too. The function reads SNOWFLAKE_HOST, SNOWFLAKE_PORT and SNOWFLAKE_PROTOCOL from its environment, which make create-function sets to snowflake, 4566 and http: the service name, over plain HTTP, since the emulator's TLS certificate is for snowflake.localhost.localstack.cloud. SF_HOSTNAMES tells the emulator to answer to that service name. Without these variables the function uses the usual snowflake.localhost.localstack.cloud over HTTPS.
Run the following command to install the dependencies:
make installStart LocalStack for AWS and the Snowflake emulator:
export LOCALSTACK_AUTH_TOKEN=<your-auth-token>
make start-localstackThis runs docker compose up -d --wait, which returns once both containers are healthy. make logs shows the logs of both, and make stop removes them.
You can create the Lambda function using the following command:
make zip
make create-functionInvoke the Lambda function using the following command:
make local-invokeOpen the output.txt file to see the results of the Lambda function.
{"statusCode": 200, "body": "Successfully connected to Snowflake and inserted rows!"}
The function logs the rows it read and the current timestamp from Snowflake:
awslocal logs filter-log-events --log-group-name /aws/lambda/localstack-snowflake-lambda-example \
--query 'events[].message' --output textTotal # of rows: 3
Row-1 => ('John', 'SQL')
Row-2 => ('Alex', 'Java')
Current timestamp from Snowflake: 2026-10-14 09:30:12.345678-07:00
You can read the table from the host as well, on port 4567:
snow sql -q "select * from test.test_schema.ability" --temporary-connection \
--account test --user test --password test \
--host snowflake.localhost.localstack.cloud --port 4567make test packages and deploys the function, invokes it, checks its response and logs, and reads the three rows back from the Snowflake emulator:
make testThis code is licensed under the Apache 2.0 License.