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"
}




2 REPLIES 2

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

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 ๐Ÿ™‚ 

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