Google BigQuery Table
This page shows how to write Terraform for BigQuery Table and write them securely.
google_bigquery_table (Terraform)
The Table in BigQuery can be configured in Terraform with the resource name google_bigquery_table
. The following sections describe 3 examples of how to use the resource and its parameters.
Example Usage from GitHub
resource "google_bigquery_table" "table" {
for_each = local.tables
dataset_id = google_bigquery_dataset.dataset.dataset_id
friendly_name = each.key
table_id = each.key
labels = each.value["labels"]
resource "google_bigquery_table" "default2" {
dataset_id = google_bigquery_dataset.dataset.dataset_id
table_id = "agents_logs"
deletion_protection = false
labels = {
env = "agents"
resource "google_bigquery_table" "be_logs_table" {
dataset_id = google_bigquery_dataset.dataset.dataset_id
table_id = "bar"
deletion_protection = false
time_partitioning {
type = "MONTH"
Parameters
-
clustering
optional - list of string
Specifies column names to use for data clustering. Up to four top-level columns are allowed, and should be specified in descending priority order.
-
creation_time
optional computed - number
The time when this table was created, in milliseconds since the epoch.
-
dataset_id
required - string
The dataset ID to create the table in. Changing this forces a new resource to be created.
-
deletion_protection
optional - bool
Whether or not to allow Terraform to destroy the instance. Unless this field is set to false in Terraform state, a terraform destroy or terraform apply that would delete the instance will fail.
-
description
optional - string
The field description.
-
etag
optional computed - string
A hash of the resource.
-
expiration_time
optional computed - number
The time when this table expires, in milliseconds since the epoch. If not present, the table will persist indefinitely. Expired tables will be deleted and their storage reclaimed.
-
friendly_name
optional - string
A descriptive name for the table.
A mapping of labels to assign to the resource.
-
last_modified_time
optional computed - number
The time when this table was last modified, in milliseconds since the epoch.
-
location
optional computed - string
The geographic location where the table resides. This value is inherited from the dataset.
-
num_bytes
optional computed - number
The geographic location where the table resides. This value is inherited from the dataset.
-
num_long_term_bytes
optional computed - number
The number of bytes in the table that are considered "long-term storage".
-
num_rows
optional computed - number
The number of rows of data in this table, excluding any data in the streaming buffer.
-
project
optional computed - string
The ID of the project in which the resource belongs.
-
schema
optional computed - string
A JSON schema for the table.
-
self_link
optional computed - string
The URI of the created resource.
-
table_id
required - string
A unique ID for the resource. Changing this forces a new resource to be created.
-
type
optional computed - string
Describes the table type.
-
encryption_configuration
list block-
kms_key_name
required - string
The self link or full name of a key which should be used to encrypt this table. Note that the default bigquery service account will need to have encrypt/decrypt permissions on this key - you may want to see the google_bigquery_default_service_account datasource and the google_kms_crypto_key_iam_binding resource.
-
-
external_data_configuration
list block-
autodetect
required - bool
Let BigQuery try to autodetect the schema and format of the table.
-
compression
optional - string
The compression type of the data source. Valid values are "NONE" or "GZIP".
-
ignore_unknown_values
optional - bool
Indicates if BigQuery should allow extra values that are not represented in the table schema. If true, the extra values are ignored. If false, records with extra columns are treated as bad records, and if there are too many bad records, an invalid error is returned in the job result. The default value is false.
-
max_bad_records
optional - number
The maximum number of bad records that BigQuery can ignore when reading data.
-
schema
optional computed - string
A JSON schema for the external table. Schema is required for CSV and JSON formats and is disallowed for Google Cloud Bigtable, Cloud Datastore backups, and Avro formats when using external tables.
-
source_format
required - string
The data format. Supported values are: "CSV", "GOOGLE_SHEETS", "NEWLINE_DELIMITED_JSON", "AVRO", "PARQUET", "ORC" and "DATASTORE_BACKUP". To use "GOOGLE_SHEETS" the scopes must include "googleapis.com/auth/drive.readonly".
-
source_uris
required - list of string
A list of the fully-qualified URIs that point to your data in Google Cloud.
-
csv_options
list block-
allow_jagged_rows
optional - bool
Indicates if BigQuery should accept rows that are missing trailing optional columns.
-
allow_quoted_newlines
optional - bool
Indicates if BigQuery should allow quoted data sections that contain newline characters in a CSV file. The default value is false.
-
encoding
optional - string
The character encoding of the data. The supported values are UTF-8 or ISO-8859-1.
-
field_delimiter
optional - string
The separator for fields in a CSV file.
-
quote
required - string
The value that is used to quote data sections in a CSV file. If your data does not contain quoted sections, set the property value to an empty string. If your data contains quoted newline characters, you must also set the allow_quoted_newlines property to true. The API-side default is ", specified in Terraform escaped as ". Due to limitations with Terraform default values, this value is required to be explicitly set.
-
skip_leading_rows
optional - number
The number of rows at the top of a CSV file that BigQuery will skip when reading the data.
-
-
google_sheets_options
list block-
range
optional - string
Range of a sheet to query from. Only used when non-empty. At least one of range or skip_leading_rows must be set. Typical format: "sheet_name!top_left_cell_id:bottom_right_cell_id" For example: "sheet1!A1:B20"
-
skip_leading_rows
optional - number
The number of rows at the top of the sheet that BigQuery will skip when reading the data. At least one of range or skip_leading_rows must be set.
-
-
hive_partitioning_options
list block-
mode
optional - string
When set, what mode of hive partitioning to use when reading data.
-
require_partition_filter
optional - bool
If set to true, queries over this table require a partition filter that can be used for partition elimination to be specified.
-
source_uri_prefix
optional - string
When hive partition detection is requested, a common for all source uris must be required. The prefix must end immediately before the partition key encoding begins.
-
-
-
materialized_view
list block-
enable_refresh
optional - bool
Specifies if BigQuery should automatically refresh materialized view when the base table is updated. The default is true.
-
query
required - string
A query whose result is persisted.
-
refresh_interval_ms
optional - number
Specifies maximum frequency at which this materialized view will be refreshed. The default is 1800000
-
-
range_partitioning
list block-
field
required - string
The field used to determine how to create a range-based partition.
-
-
time_partitioning
list block-
expiration_ms
optional computed - number
Number of milliseconds for which to keep the storage for a partition.
-
field
optional - string
The field used to determine how to create a time-based partition. If time-based partitioning is enabled without this value, the table is partitioned based on the load time.
-
require_partition_filter
optional - bool
If set to true, queries over this table require a partition filter that can be used for partition elimination to be specified.
-
type
required - string
The supported types are DAY, HOUR, MONTH, and YEAR, which will generate one partition per day, hour, month, and year, respectively.
-
-
view
list block-
query
required - string
A query that BigQuery executes when the view is referenced.
-
use_legacy_sql
optional - bool
Specifies whether to use BigQuery's legacy SQL for this view. The default value is true. If set to false, the view will use BigQuery's standard SQL
-
Explanation in Terraform Registry
Creates a table resource in a dataset for Google BigQuery. For more information see the official documentation and API. -> Note: On newer versions of the provider, you must explicitly set
deletion_protection=false
(and runterraform apply
to write the field to state) in order to destroy an instance. It is recommended to not set this field (or set it to true) until you're ready to destroy.
Tips: Best Practices for The Other Google BigQuery Resources
In addition to the google_bigquery_dataset, Google BigQuery has the other resources that should be configured for security reasons. Please check some examples of those resources and precautions.
google_bigquery_dataset
Ensure your BigQuery dataset blocks unwanted access
It is better to block unwanted access from users outside the organization.
Frequently asked questions
What is Google BigQuery Table?
Google BigQuery Table is a resource for BigQuery of Google Cloud Platform. Settings can be wrote in Terraform.
Where can I find the example code for the Google BigQuery Table?
For Terraform, the Shriyut/samestate-terraform, yurikrupnik/mussia8 and yurikrupnik/mussia8 source code examples are useful. See the Terraform Example section for further details.