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




4 REPLIES 4

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

KartikeyaJain
New Contributor II

The official Databricks provider in Terraform only allows you to create SQL queries, not execute them. To actually run queries, you can either:

  1. Use the http provider to make API calls to the Databricks REST API to execute SQL queries.

  2. 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.

Join Us as a Local Community Builder!

Passionate about hosting events and connecting people? Help us grow a vibrant local community—sign up today to get started!

Sign Up Now