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

main.tf#L61
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"]
big-query.tf#L24
resource "google_bigquery_table" "default2" {
  dataset_id          = google_bigquery_dataset.dataset.dataset_id
  table_id            = "agents_logs"
  deletion_protection = false
  labels = {
    env = "agents"
big-query.tf#L56
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"

Review your Terraform file for Google best practices

Shisho Cloud, our free checker to make sure your Terraform configuration follows best practices, is available (beta).

Parameters

Specifies column names to use for data clustering. Up to four top-level columns are allowed, and should be specified in descending priority order.

The time when this table was created, in milliseconds since the epoch.

The dataset ID to create the table in. Changing this forces a new resource to be created.

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.

The field description.

  • etag optional computed - string

A hash of the resource.

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.

A descriptive name for the table.

  • id optional computed - string
  • labels optional - map from string to string

A mapping of labels to assign to the resource.

The time when this table was last modified, in milliseconds since the epoch.

The geographic location where the table resides. This value is inherited from the dataset.

The geographic location where the table resides. This value is inherited from the dataset.

The number of bytes in the table that are considered "long-term storage".

The number of rows of data in this table, excluding any data in the streaming buffer.

The ID of the project in which the resource belongs.

  • schema optional computed - string

A JSON schema for the table.

The URI of the created resource.

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

    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

    Let BigQuery try to autodetect the schema and format of the table.

    The compression type of the data source. Valid values are "NONE" or "GZIP".

    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.

    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.

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

    A list of the fully-qualified URIs that point to your data in Google Cloud.

    • csv_options list block

      Indicates if BigQuery should accept rows that are missing trailing optional columns.

      Indicates if BigQuery should allow quoted data sections that contain newline characters in a CSV file. The default value is false.

      The character encoding of the data. The supported values are UTF-8 or ISO-8859-1.

      The separator for fields in a CSV file.

      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.

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

      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

      When set, what mode of hive partitioning to use when reading data.

      If set to true, queries over this table require a partition filter that can be used for partition elimination to be specified.

      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

    Specifies if BigQuery should automatically refresh materialized view when the base table is updated. The default is true.

    A query whose result is persisted.

    Specifies maximum frequency at which this materialized view will be refreshed. The default is 1800000

  • range_partitioning list block

    The field used to determine how to create a range-based partition.

    • range list block
      • end required - number

      End of the range partitioning, exclusive.

      The width of each range within the partition.

      Start of the range partitioning, inclusive.

  • time_partitioning list block

    Number of milliseconds for which to keep the storage for a partition.

    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.

    If set to true, queries over this table require a partition filter that can be used for partition elimination to be specified.

    The supported types are DAY, HOUR, MONTH, and YEAR, which will generate one partition per day, hour, month, and year, respectively.

  • view list block

    A query that BigQuery executes when the view is referenced.

    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 run terraform 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.

risk-label

google_bigquery_dataset

Ensure your BigQuery dataset blocks unwanted access

It is better to block unwanted access from users outside the organization.

Review your Google BigQuery settings

In addition to the above, there are other security points you should be aware of making sure that your .tf files are protected in Shisho Cloud.

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.

security-icon

Automate config file reviews on your commits

Fix issues in your infrastructure as code with auto-generated patches.