0

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?

Stack Overflow
  • 377
  • 4
  • 16
  • What happens if you set `${path.root}`? – Marko E Mar 31 '22 at 12:38
  • @MarkoE doesn't change the error – Stack Overflow Mar 31 '22 at 12:48
  • Can you add the directory structure as well to the question? – Marko E Mar 31 '22 at 13:07
  • It feels like you are approaching your issue from a sub-optimal angle. Typically, if you want to "inject" variables into your Lambda code you use environment variables, SSM parameter store variables or even simple files you store along your Python file. This way, there is no need for such a complicated "replace this in the file and then use the file as Lambda code". Is there any reason, why you are not doing it like this? This reason might help us to answer your question better. – Jens Mar 31 '22 at 13:08
  • Ah, ok, I wasn't reading the error as I should have: you have to have a `lambda_handler` function in your Python script. – Marko E Mar 31 '22 at 13:08
  • @Jens when using files alongside the code, we encounter the exact same problem: we need to add to the lambda a dynamically created file. About SSM, I saw this solution but I think it's a bit overkill for me. – Stack Overflow Mar 31 '22 at 13:10
  • I don't think you can use `async` before the `lambda_handler` function definition because the way function is invoked depends on the integration you have, e.g., it's different for API Gateway + Lambda and S3 + Lambda: https://docs.aws.amazon.com/lambda/latest/dg/invocation-async.html. – Marko E Mar 31 '22 at 13:22
  • 1
    https://stackoverflow.com/questions/60455830/can-you-have-an-async-handler-in-lambda-python-3-6 Python Lambda functions don't support async handlers. NodeJS Lambda functions do support them, which makes it confusing. – Mark B Mar 31 '22 at 14:33

0 Answers0