Hi everyone!
I have a weird behavior between a module and a resource. I don’t know if it’s a bug or normal, but let’s consider the following code:
module "my_module" {
source = "./my_module"
path = local_file.foo.filename
}
resource "local_file" "foo" {
filename = "foo.json"
content = jsonencode({ foo : "bar" })
}
And the module:
variable "path" {
type = string
}
data "local_file" "this" {
filename = var.path
}
output "test" {
value = data.local_file.this.content
}
What the code does is create a file, pass its path to the module and with a data source, read its content. You’d think a terraform apply would work, but I get the following error:
Terraform planned the following actions, but then encountered a problem:
# local_file.foo will be created
+ resource "local_file" "foo" {
+ content = jsonencode(
{
+ foo = "bar"
}
)
+ content_base64sha256 = (known after apply)
+ content_base64sha512 = (known after apply)
+ content_md5 = (known after apply)
+ content_sha1 = (known after apply)
+ content_sha256 = (known after apply)
+ content_sha512 = (known after apply)
+ directory_permission = "0777"
+ file_permission = "0777"
+ filename = "foo.json"
+ id = (known after apply)
}
Plan: 1 to add, 0 to change, 0 to destroy.
╷
│ Error: Read local file data source error
│
│ with module.my_module.data.local_file.this,
│ on my_module/main.tf line 3, in data "local_file" "this":
│ 3: data "local_file" "this" {
│
│ The file at given path cannot be read.
│
│ +Original Error: open foo.json: no such file or directory
╵
Basically, Terraform tries to read the file before the resource is created, even if the dependency on the field path is explicit.
If I add a depends_on
on the module, no issue; I get the expected behavior:
Terraform will perform the following actions:
# local_file.foo will be created
+ resource "local_file" "foo" {
+ content = jsonencode(
{
+ foo = "bar"
}
)
+ content_base64sha256 = (known after apply)
+ content_base64sha512 = (known after apply)
+ content_md5 = (known after apply)
+ content_sha1 = (known after apply)
+ content_sha256 = (known after apply)
+ content_sha512 = (known after apply)
+ directory_permission = "0777"
+ file_permission = "0777"
+ filename = "foo.json"
+ id = (known after apply)
}
# module.my_module.data.local_file.this will be read during apply
# (depends on a resource or a module with changes pending)
<= data "local_file" "this" {
+ content = (known after apply)
+ content_base64 = (known after apply)
+ content_base64sha256 = (known after apply)
+ content_base64sha512 = (known after apply)
+ content_md5 = (known after apply)
+ content_sha1 = (known after apply)
+ content_sha256 = (known after apply)
+ content_sha512 = (known after apply)
+ filename = "foo.json"
+ id = (known after apply)
}
Plan: 1 to add, 0 to change, 0 to destroy.
I know the use case is kinda weird (I just simplified it to share something simple with you), but I feel Terraform should recognize the dependency without the explicit depends_on
. What do you think?
Cheers,
Antoine Rouaze
PS: My Terraform and providers versions are:
Terraform v1.6.4
on linux_amd64
+ provider registry.terraform.io/hashicorp/local v2.4.0
+ provider registry.terraform.io/hashicorp/null v3.2.1
+ provider registry.terraform.io/hashicorp/random v3.5.1