1

I am trying to deploy BigQuery scheduled query using terraform but I do not know how I can use list and for_each syntax.

I have this template file.

SELECT *
FROM mydataset.mytable_${suffix}

Then generate a list of query using template_file module. var.suffix_list here is a list of string.

data "template_file" "query" {
  for_each = var.suffix_list # list(string)
  template = "file/path/to/template/file"
  vars = {
    suffix    = each.value
  }
}

From this module I want to generate a list of query.

output "query" {
  value = # a list of rendered query
}

This output will be an input of google_bigquery_data_transfer_config resource.

resource "google_bigquery_data_transfer_config" "query_config" {
  for_each                 = # a list of rendered query
  data_refresh_window_days = var.data_refresh_window_days
  data_source_id           = "scheduled_query"
  display_name             = var.display_name
  disabled                 = var.disabled
  destination_dataset_id   = var.destination_dataset_id
  location                 = var.query_location
  project                  = var.gcp_project
  schedule                 = var.query_schedule
  params = {
    destination_table_name_template = var.destination_table_name_template
    query                           = each.value
  }
}

How can I output definition and an input for google_bigquery_data_transfer_config? Thanks in advance.

1 Answer 1

4

A couple of things are necessary to make this work.

Your argument to for_each should be a set, so you'll need to use

  for_each = toset(var.suffix_list)

In your template itself the variable is named suffix, however, you're passing the variable schema. These ought to match.

The template argument actually needs to be the text of the template. So you'll need to read the file in using the file function, e.g.

  template = file("path/to/template/file")

Then, finally, to write your actual output, you can use a list comprehension

output "query" {
  value = [for query in data.template_file.query : query.rendered]
}
Sign up to request clarification or add additional context in comments.

1 Comment

Would you also advise me how I can use the output "query" in the resource "google_bigquery_data_transfer_config"?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.