How to Connect AWS Lambda to a Databricks SQL Warehouse
AWS Lambda can query a Databricks SQL warehouse through the Python connector or the Statement Execution API. Here is how each method works and what to set up.
On this page
Short answer
You connect AWS Lambda to a Databricks SQL warehouse with the Databricks SQL Connector for Python, which opens a connection using the warehouse server hostname, HTTP path and an access token, or with the SQL Statement Execution API, which runs queries over plain HTTPS. The connector suits short interactive queries inside a function, while the API suits longer queries because Lambda can submit a query and poll for results.
What does connecting Lambda to a Databricks SQL warehouse mean?
Connecting AWS Lambda to a Databricks SQL warehouse means letting a small serverless function send SQL queries to Databricks and use the results, so data work can run on an event instead of on a server that is always on. Lambda runs your code when something happens, such as a file landing in storage or a timer firing, and the SQL warehouse runs the query against the tables in your lakehouse.
The two services do not share a network or a driver by default, so the connection is made inside the function code. The function opens a connection with the warehouse address, a path that identifies the warehouse, and credentials, then sends a query and reads the rows that come back. Nothing is installed on the warehouse side, and no Databricks cluster needs to run all day for the function to work.
This pattern fits jobs that are short and event-driven: nightly rollups, checks that run when new data arrives, small extracts that feed a dashboard, or alerts based on a query result. It is a poor fit for long-running analytics, because a Lambda function stops after 15 minutes at most. For longer work, a Databricks job or workflow is the better tool, and the same serverless thinking applies to other AWS serverless architecture designs.
What do you need before you start?
Before you write any code, gather the connection details and the permissions the function will need. A missing detail is the most common reason a first connection fails, so it is worth collecting everything in one place first. A Databricks SQL warehouse is the query engine of a lakehouse, so knowing data warehouse concepts helps when you choose which tables the function reads.
- A SQL warehouse and its connection details. In Databricks, open the SQL warehouse and copy the server hostname, the port (443), and the HTTP path from the Connection details tab. You also need the warehouse id, which appears in the same place.
- Credentials with the right permissions. A personal access token, or an OAuth service principal, that has permission to use the warehouse and read the tables the function will query.
- A Lambda function with a Python runtime. The Databricks SQL Connector for Python must be in the deployment package or in a Lambda layer, since it is not part of the standard runtime.
- An IAM role for the function. It needs permission to read the secret that holds the credentials and to write logs, and nothing else.
- Network access. If the workspace uses private connectivity, the function must run in the same VPC with routes that reach the warehouse.
Test the credentials from your own machine first with a short query. If the query fails there, it will fail in Lambda too, and fixing it locally is faster than debugging inside a function.
Which connection method should you choose?
There are three ways to connect, and the right one depends on how long your queries run and how much packaging you want to do. Most teams pick one of the first two.
| Method | How it connects | Best for | Watch out for |
|---|---|---|---|
| Python connector | Driver inside the function, direct connection over HTTPS | Short queries with rows returned to the function | Driver adds size to the package; connection setup takes time |
| Statement Execution API | Plain HTTPS requests: submit a query, then poll for results | Longer queries and simple deployments | Results arrive in pages; polling adds code |
| JDBC or ODBC driver | Standard database driver in a Lambda layer | Teams that already use JDBC tooling | Large layer; harder to package and keep current |
Start with the Python connector if your queries finish in seconds and you want rows back in the function. Choose the Statement Execution API if queries may run for minutes, because the function can submit the query, do other work, and check back later instead of holding a connection open. The JDBC route is worth it only when your team already has JDBC code to reuse.
How do you connect with the Python connector?
The Python connector is the most direct route: the function imports a library, opens a connection, runs a query and reads the rows. The steps below assume the connector is packaged in a layer.
- Package the connector. Install the databricks-sql-connector package into a Lambda layer, and pin the version so a later release cannot change behaviour without a test.
- Store the credentials. Put the server hostname, HTTP path and token in AWS Secrets Manager, and let the function's IAM role read only that one secret.
- Open the connection. In the function, read the secret and call the connector with the server hostname, HTTP path and access token.
- Run the query. Open a cursor, execute the SQL, and fetch the rows you need. Fetch in batches for large results rather than loading everything at once.
- Close everything. Close the cursor and the connection in a finally block, so a failed query cannot leave a session open on the warehouse.
- Test with a short query. Run a query such as SELECT 1 first, then move on to the real query once the connection works.
Keep the connection setup in one small helper that every function in the project can reuse. When the hostname or token changes, you then have one place to update instead of a copy in each function.
How do you connect with the Statement Execution API?
The Statement Execution API needs no driver, which keeps the deployment package small. The function talks to Databricks over plain HTTPS, submits a query, and checks back until the result is ready.
- Prepare the credentials. Store a token or service principal secret in Secrets Manager, as with the connector method.
- Submit the query. Send a POST request to the statements endpoint with the warehouse id and the SQL text, and read the statement id from the response.
- Poll for completion. Ask for the statement status until it reports SUCCEEDED. Wait a few seconds between checks so you do not flood the API.
- Fetch the results. Read the result pages in order and combine them. Each page holds a chunk of the rows, so loop until no more pages remain.
- Handle failure states. If the status reports FAILED or CANCELED, log the error message and raise an alert instead of treating the run as a success.
- Set a sensible timeout. Stop polling after a limit you choose, and let the function fail clearly rather than running until Lambda stops it.
This method fits queries that run for minutes, because the function is not holding a database connection open while it waits. It also fits teams that prefer plain HTTP calls to a packaged driver, and it works from any language that can send a web request.
How do you keep credentials safe?
A token that can query your warehouse is a valuable secret, so treat it like one. Never put tokens in function code, in plain environment variables, or in a repository. Use AWS Secrets Manager or Systems Manager Parameter Store, and let the function's IAM role read only the one secret it needs.
Prefer an OAuth service principal over a personal access token where your workspace supports it. A service principal belongs to the application rather than a person, so its secret can be rotated without touching a user's account, and access ends when the principal is removed. Give it only the permissions it needs: use the warehouse and read the tables it queries, nothing more.
Rotate secrets on a schedule, and keep audit logs switched on so a leaked credential can be traced. CloudTrail records who read the secret, and Databricks audit logs record which queries ran, so an incident review has a clear trail to follow. The same discipline applies to any pipeline built with data engineering services, where a single shared credential is a common weak point.
Example (hypothetical): a nightly sales rollup
Example (hypothetical): a retail company keeps daily sales in a Databricks table and wants a summary ready each morning. A Lambda function runs on a timer at 02:00, queries yesterday's orders, and writes one row per store into a summary table.
Yesterday's table holds 8,400 orders across 12 stores. The query groups them by store and returns 12 rows with the order count and total for each store. The function checks that the 12 rows add up to 8,400 orders, then writes the summary. If the counts do not match, it logs an error and sends an alert instead of writing bad data.
The function runs 30 times a month and each run takes about 40 seconds, which is 1,200 seconds, or 20 minutes of compute a month. With a serverless warehouse, compute starts for the query and stops afterwards, so the team does not pay to keep a warehouse running all night. For exact rates, see the vendor site.
What are the common mistakes?
The most common mistakes are about credentials, connections and time limits, and all of them are easy to avoid once you know to look for them.
- Tokens in code or plain environment variables. A secret in the deployment package can be read by anyone with access to the function. Keep it in Secrets Manager.
- A new connection per query inside a loop. Opening a connection for every row or every query is slow and can hit warehouse limits. Open once per invocation.
- Queries longer than the function timeout. A query that runs past 15 minutes is cut off. Use the Statement Execution API, or move the work to a Databricks job.
- Forgetting to close cursors and connections. Open sessions linger on the warehouse and can block other work.
- Over-wide permissions. A service principal that can read every table is a risk. Scope it to the warehouse and tables the function actually uses.
- Testing only with small data. A query that is fast on a thousand rows may time out on the real tables. Test with a realistic slice before going live.
Key takeaways
- Lambda reaches a Databricks SQL warehouse through the Python connector or the Statement Execution API
- The connector fits short queries; the API fits longer ones and keeps the package small
- Connection details are the server hostname, HTTP path and warehouse id, plus a token or service principal
- Keep credentials in Secrets Manager and give the function the minimum permissions it needs
- Close connections in a finally block and design for the 15-minute function limit
Frequently asked questions
Can Lambda connect directly to Databricks?
Yes. A Lambda function can connect to a Databricks SQL warehouse with the Python connector or the Statement Execution API. Both use the warehouse server hostname, HTTP path and a token or service principal, and neither needs anything installed on the warehouse side.
Do I need a VPC for the connection?
Only if the Databricks workspace uses private connectivity. For a standard workspace, the function connects over the public internet with HTTPS. When private links are in use, run the function in the same VPC with routes that reach the warehouse.
Which method is faster for short queries?
The Python connector is faster for short queries because it holds one connection and returns rows directly. The Statement Execution API adds polling overhead, so it is better suited to queries that run for minutes rather than seconds.
What permissions does the token need?
The token or service principal needs permission to use the SQL warehouse and read access to the tables the function queries. Grant only those permissions, and keep write access out unless the function writes results back to a table.
How do I handle queries that run longer than 15 minutes?
Do not hold the query inside the function. Submit it through the Statement Execution API and poll, or move the work to a Databricks job or workflow, which has no 15-minute limit. Lambda can then check the result later.
Does the warehouse need to be running?
A serverless SQL warehouse starts on demand when a query arrives, so no action is needed. A classic warehouse must be running, or the first query will wait while it starts, which can add minutes to the run.