Error: Invalid function argument

Hi there, looks like there is some glitch in Terraform, I am continuously getting below error, I tried with …/… as well as it works on local system but in terraform apply it’s giving error.

Error: Invalid function argument
629
630 on modules/rule-target/main.tf line 4, in resource "aws_cloudwatch_event_rule" "event_rule":
631 4: event_pattern = file("../event_patterns/${var.rule_name}.json")
632 ----------------
633 var.rule_name is ""
634
635 Invalid value for "path" parameter: no file exists at
636 ../event_patterns/s3.json; this function works only with files
637 that are distributed as part of the configuration source code, so if this file
638 will be created by a resource in this configuration you must instead obtain
639 this result from an attribute of that resource.

I’d suggest using ${path.module} or ${path.root} (References to Values - Configuration Language - Terraform by HashiCorp) rather than relying on relative paths.

Hi @stuart-c thanks for replying. Will it be something as below?

old path: event_pattern = file("../event_pattern/${var.rule_name}.json")

new path: event_pattern = file($"{path.root/event_patterns}/${var.rule_name}.json")

Something like event_pattern = file("${path.root}/event_patterns/${var.rule_name}.json")

That is assuming the file lives in a event_patterns directory in your root module. If that code is in a module and the file is too, you’d want ${path.module} instead of ${path.root}.

1 Like

@stuart-c same error. Really not sure whats broke, struggling to fix this error from couple of days now. :sneezing_face:

Error: Invalid function argument
195
196 on modules/rule-target/main.tf line 4, in resource "aws_cloudwatch_event_rule" "event_rule":
197 4: event_pattern = file("${path.root}/event_patterns}/${var.rule_name}.json")
198 ----------------
199 path.root is "."
200 var.rule_name is "eventTrigger"
201
202 Invalid value for "path" parameter: no file exists at
203 event_patterns}/eventTrigger.json; this function works only with files that
204 are distributed as part of the configuration source code, so if this file will
205 be created by a resource in this configuration you must instead obtain this
206 result from an attribute of that resource.

Could you paste the exact code, as the error event_patterns}/eventTrigger.json suggests you have something wrong (that } shouldn’t appear there)?

Could you reformat your post using backticks as otherwise it is very hard to follow.

It looks like you have file("./event_patterns}/${var.rule_name}.json"). You watn file("${part.root}/event_patterns/${var.rule_name}.json")

You still have the extra } that needs removing after event_patterns.

@stuart-c Yes, I tried removing } as well, same error.

Could you paste the code & error with the extra } removed?

@stuart-c sure

resource "aws_cloudwatch_event_rule" "event_rule" {
  
  name = "${var.rule_name}"
  event_pattern = file("./event_patterns/${var.rule_name}.json")
  
}
Error: Invalid function argument
195
196 on modules/rule-target/main.tf line 4, in resource "aws_cloudwatch_event_rule" "event_rule":
197 4: event_pattern = file("./event_patterns/${var.rule_name}.json")
198 ----------------
199 var.rule_name is "eventTrigger"
200
201 Invalid value for "path" parameter: no file exists at
202 event_patterns/eventTrigger.json; this function works only with files that are
203 distributed as part of the configuration source code, so if this file will be
204 created by a resource in this configuration you must instead obtain this
205 result from an attribute of that resource.

Have you tried "${path.root}/event_patterns/${var.rule_name}.json" as suggested?

1 Like

Thank you so much @stuart-c it worked. :hugs:

2 posts were split to a new topic: File not found with “file” function