cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to execute SQL statement using terraform

chethankumar
New Contributor III

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 

provider "databricks" {
host = var.databricks_host
token = var.databricks_token
}

resource "databricks_sql_query" "example" {
name = "example-query"
data_source_id = var.data_source_id
query = "SELECT * FROM your_table"
}

resource "local_file" "query_result" {
content = jsonencode(databricks_sql_query.example.output)
filename = "${path.module}/query_result.json"
}




3 REPLIES 3

BigRoux
Databricks Employee
Databricks Employee

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.

Nes_Hdr
New Contributor III

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 🙂 

chethankumar
New Contributor III

I have used the bellow provider to run the query 
https://registry.terraform.io/providers/hashicorp/http/latest

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group