I have a Python code I am trying to inject Terraform variables into via the templatefile()
. So basically I have a file foo.tftpl
(template file):
async def lambda_handler(event, context):
"""The entry point of the lambda functions
"""
foo = "${bar}"
print(foo)
And then in terraform, I create a new file with the injected variables:
variable "demo"
{
value = "demo"
}
resource "local_file" "bar"
{
filename = "${path.module}/bar.py"
content = templatefile("${path.module}/foo.tfpl", {bar=var.demo.value})
}
and so, the result is a file named bar.py
that has the following content:
foo = "demo"
print(foo)
So you could say I am creating this file "on the fly". It will only be created during the terraform apply
command.
Now, I want to pass this file to a lambda function:
resource "aws_lambda_function" "demo-lambda-function"
{
filename = "${path.module}/bar.py"
runtime = "python3.8"
depends_on = [ local_file.bar ]
handler = "bar.lambda_handler"
}
But when I try to run terraform apply
, I get an error:
Error: unable to load [lambda function resource path]: read [lambda function resource path]: The handle is invalid.
How do I use a dynamically created file in the lambda function?