Open ralbritton-utah opened 1 week ago
I have successfully reproduced the issue. To resolve it, you need to make two modifications to the google_bigquery_job resource definition:
location=<LOCATION>
parameter.project_id = google_bigquery_table.foo.project
parameter.I'm currently investigating why these parameters are required by the API but optional in the Terraform syntax. Here's an example of the working configuration:
"google_bigquery_dataset" "dataset" {
dataset_id = "example_dataset"
friendly_name = "test"
description = "This is a test description"
location = "US"
labels = {
env = "default"
}
}
resource "google_bigquery_table" "foo" {
deletion_protection = false
dataset_id = google_bigquery_dataset.dataset.dataset_id
table_id = "job_query_table"
}
resource "google_bigquery_job" "job" {
job_id = "job_query"
labels = {
"example-label" = "example-value"
}
query {
query = "SELECT state FROM [lookerdata:cdc.project_tycho_reports]"
destination_table {
project_id = google_bigquery_table.foo.project
dataset_id = google_bigquery_table.foo.dataset_id
table_id = google_bigquery_table.foo.table_id
}
write_disposition = "WRITE_TRUNCATE"
}
location = "US"
}
This configuration should resolve the "Required parameter is missing: project_reference" error you encountered. I'll update the ticket with any additional findings regarding the discrepancy between API requirements and Terraform syntax.
Confirmed issue!
According to terraform registry the query.destination_table.project_id
is not required, but it is not possible to create the resource without it.
I was able to work around the issue using the steps that @bindermuehle suggested. Thank you.
I haven't tried to repro the error myself, but according to the API reference doc for JobReference
and JobConfigurationQuery
-
location
is optionaldestination_table.project_id
and destination_table.dataset_id
should both be required, which we'll look into updating on the Terraform sideAfter further investigation, I'd like to provide some clarification based on the documentation and my findings:
The documentation indicates that project_id is indeed an optional field when the table is specified using a fully qualified ID. Specifically, it states: "table_id - (Required) The table. Can be specified {{table_id}} if project_id and dataset_id are also set, or of the form projects/{{project}}/datasets/{{dataset_id}}/tables/{{table_id}} if not." Based on this, both of the following configurations are valid ( I've validated this):
destination_table {
table_id = google_bigquery_table.foo.id
}
destination_table {
project_id = google_bigquery_table.foo.project
dataset_id = google_bigquery_table.foo.dataset_id
table_id = google_bigquery_table.foo.table_id
}
However, it appears that this validation cannot be performed during the planning phase. because the resolved string is not available. Regarding the possibility of making this error more user-friendly, I'm not immediately aware of an approach to achieve this. It seems to be a limitation in how Terraform interacts with the Google Cloud API during the planning stage. If you know of a way to improve this let me know and I'm happy to investigate
Community Note
Terraform Version & Provider Version(s)
Terraform vX.X.X on
Affected Resource(s)
google_bigquery_job
Terraform Configuration
Debug Output
google_bigquery_job.job: Creating... β· β Error: Error creating Job: googleapi: Error 400: Required parameter is missing: project_reference, required β β with google_bigquery_job.job, β on main.tf line 1, in resource "google_bigquery_job" "job": β 1: resource "google_bigquery_job" "job" { β
Expected Behavior
job should be created
Actual Behavior
Error received
Steps to reproduce
create main.tf:
Define Variables:
Created a tfvars file:
Important Factoids
project = "my-gcp-project" job_id="my_job_id"
query = { file_path ="query.txt" write_disposition="WRITE_TRUNCATE" }
destination_table = { dataset_id = "my_dataset" table_id = "my_table" }
job_labels = { env = "dev" }
run `terraform plan -var-file="my_var_file.tfvars"
The plan runs successfully. Then run `terraform apply -auto-approve -var-file="my_var_file.tfvars" It begins creating the job and fails with the error
There is no
project_reference
parameter in the terraform documentation. A project is provided.References
No response
b/373453207