How to execute SQL statement using terraform
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2024 10:56 AM
Is there a way to execute SQL statements using Terraform
I can see it can be possible using API as bellow,
https://docs.databricks.com/api/workspace/statementexecution/executestatement
but I want to know is a strength way to run like bellow code
- Labels:
-
Delta Lake
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2024 07:19 AM
I am not an expert here but some quick research it appears that you can execute SQL via Terraform but you need to utilize the Databricks Terraform provider and configure resources that support SQL execution. Terraform itself does not directly support SQL; however, it can provision resources like clusters or sql warehouses to run the sql.
You can leverage the SQL Perm resource:
resource "databricks_sql_permissions" "example_table" { table = "example_table" privilege_assignments { principal = "user@example.com" privileges = ["SELECT", "MODIFY"] } }
You can also schedule the SQL via a job, SDK, or API.
Cheers, Louis.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2024 07:55 AM
I was having the same question a while ago, and I couldn't find a way to automatically execute the query using terraform. What you can do though, is to set a schedule if the query needs to be executed rather regularly, or simply execute it manually in the UI if you need to execute it only once ...
Let me know if you find another solution 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2024 01:34 AM
I have used the bellow provider to run the query
https://registry.terraform.io/providers/hashicorp/http/latest
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 weeks ago
The official Databricks provider in Terraform only allows you to create SQL queries, not execute them. To actually run queries, you can either:
Use the http provider to make API calls to the Databricks REST API to execute SQL queries.
Alternatively, if you're using a Service Principal with a Client Secret, you can configure another http provider to obtain an access token via OIDC, which can then be used in place of a PAT token for authentication.

