Terraform provider that automates the creation and update of BigQuery functions/procedures/views by parsing CREATE SQL statements stored in SQL Scripts and connecting with the resources the hashicorp/google provider for object creation.
- Terraform >= 1.0 or
- OpenTofu >= 1.6
- hashicorp/google provider
- See
examples/folder for usage patterns (including table functions, views, and materialized views). - Check the provider documentation on the:
terraform {
required_providers {
bqutils = {
source = "juarezr/bqutils"
version = ">= 0.6"
}
}
}
provider "bqutils" {}No provider settings are required.
Parses CREATE FUNCTION / TABLE FUNCTION / PROCEDURE / AGGREGATE FUNCTION and fills attributes for google_bigquery_routine.
Parses CREATE VIEW / MATERIALIZED VIEW and supplies attributes for creating a view with the google_bigquery_table resource (including view / materialized_view blocks, partitioning, clustering, labels, etc.).
Using SQL from a file in the Terraform module folder:
CREATE OR REPLACE TABLE FUNCTION mydataset.list_tables
(
table_name_filter STRING,
max_results INT64
)
OPTIONS (
description = 'Used to show tables in another dataset.'
) AS (
SELECT
t.table_name,
t.table_type,
t.creation_time,
t.is_typed
FROM `mydataset`.INFORMATION_SCHEMA.TABLES AS t
WHERE t.table_name LIKE CONCAT('%', COALESCE(table_name_filter,''), '%')
QUALIFY ROW_NUMBER() OVER(ORDER BY t.table_name) <= COALESCE(max_results, 200)
);# Load the Table FUNCTION SQL from a file in the same folder as the Terraform code.
data "bqutils_routine_parser" "list_tables" {
sql = file("${path.module}/sql/list_tables.sql")
trim_body = true
}
# Gets the BigQuery dataset where the routine is created.
data "google_bigquery_dataset" "mydataset" {
dataset_id = "mydataset"
}
# Create the routine in BigQuery using the attributes parsed from the SQL file.
resource "google_bigquery_routine" "list_tables" {
dataset_id = google_bigquery_dataset.mydataset.dataset_id
routine_id = data.bqutils_routine_parser.list_tables.routine_id
routine_type = data.bqutils_routine_parser.list_tables.routine_type
language = data.bqutils_routine_parser.list_tables.language
description = data.bqutils_routine_parser.list_tables.description
definition_body = data.bqutils_routine_parser.list_tables.definition_body
dynamic "arguments" {
for_each = data.bqutils_routine_parser.list_tables.arguments
content {
name = arguments.value.name
data_type = arguments.value.data_type
argument_kind = arguments.value.argument_kind
}
}
}Check the guides for common tasks.
- Go >= 1.22 (for building from source)
- make
- Docker (for testing locally)
# Build
make build
# Unit tests (parser)
go test ./internal/sqlparse/ -v
# Acceptance tests (requires terraform on PATH)
TF_ACC=1 go test ./internal/provider/ -v -count=1
# Install docs tooling and regenerate provider docs
make tools
# Run all tests
make check# Install into local Terraform plugin directory
make install
# Setup dev overrides pointing to local Terraform plugin directory
make dev-override
# Test your terraform module
cd ~/src/terraform-module
terraform init && terraform plan
terraform apply -auto-approve && terraform destroy -auto-approve
cd -
# Remove dev overrides and binaries from the local Terraform plugin directory
make uninstallMPL-2.0 (see LICENSE).