Hi just wonder if anyone can assist I have bq-table resource which works perfectly fine as it is. I provide the table schema as a json file within a subdirectory called bq-schema - this is picked up and the table and schema is created fine
When I call this as a module I get the following error
Error: Invalid function argument
│
│ on .terraform/modules/bigquery-tables/main.tf line 11, in resource "google_bigquery_table" "bqtable":
│ 11: schema = file("${path.module}/bq-schema/${var.bq_table[count.index]["schema_id"]}")
│ ├────────────────
│ │ count.index is 0
│ │ path.module is ".terraform/modules/bigquery-tables"
│ │ var.bq_table is list of object with 1 element
│
│ Invalid value for "path" parameter: failed to read file: read .terraform/modules/bigquery-tables/bq-schema: is a directory
On windows the error is - "Invalid value for path parameter - failed to read - the handle is invalid
My resource is as following
> resource "google_bigquery_table" "bqtable" {
> dataset_id = var.dataset_id
> count = length(var.bq_table)
> table_id = var.bq_table[count.index]["table_id"]
> labels = var.labels
> schema = file("${path.module}/bq-schema/${var.bq_table[count.index]["schema_id"]}")
> }
Variables.tf
variable “bq_table” {
default =
type = list(object({
table_id = string
schema_id = string
}))
}
variable “labels” {
type = map(string)
}
variable “dataset_id” {
type = string
}
Tfvars
bq_table = [
{
schema_id = “table-schema-01.json”
table_id = “table1”
}
]
labels = {
env = “development”
}
dataset_id = “testing”
Module/main.tf
module “bigquery-tables” {
source = “/xxx/Modules/bigquery-tables"
dataset_id = “testing”
bq_table = [
{
table_id = “”
schema_id = “”
}
]
labels = var.labels
}
Any ideas anyone ?, thanks
Hi @jayv1975,
This message suggests that you’ve specified the path to a directory rather than to a file.
Can you confirm that the final resolved path mentioned in the error message is a file or a directory? For example, on a Unix system you could try:
cat .terraform/modules/bigquery-tables/bq-schema
If this is indeed a file then it should show the contents of the file. If it’s a directory then I expect you will see a variation of the same error Terraform returned, which would suggest that you have an incorrect path in your configuration.
Hi
Thanks for your response, indeed did run that test and yes its a directory, but what I dont understand, if i run a terraform plan /apply in the directory where the actual resource is defined it work with no issues:
cat: .terraform/modules/bigquery-tables/bq-schema/: Is a directory
Also to confirm the json file also does exist : cat .terraform/modules/bigquery-tables/bq-schema/table-schema-01.json
[
{
"name": "permlinks",
"type": "STRING",
"mode": "NULLABLE"
}
]
Just puzzled as to why this is happening
In your example you show a value for bq_table
where the schema_id
attribute is set to the empty string ""
.
If you concatenate the empty string after ${path.module}/bq-schema/
then you will be left with a path to the whole directory rather than to a particular file inside that directory. Your module seems to require schema_id
to be a non-empty string in order to achieve correct behavior.
You could change your module to give better feedback about that usage error by adding a validation rule to the bq_table
variable, like this:
variable “bq_table” {
type = list(object({
table_id = string
schema_id = string
}))
validation {
condition = alltrue([
for obj in var.bq_table : obj.schema_id != ""
])
error_message = "Each table definition must have a non-empty schema_id attribute."
}
}
If you add that validation rule then it will hopefully cause your module to give better feedback about where the real problem is, and then you can correct your module call to provide a valid value for schema_id
.