Path problem with lambda module for lambda functions

Hi guys,

I’m working on task to create lambda module and use it for multiple lambda functions. Everything seems to work but I can’t sort out what to use for data.archive_file. My point is to use the module for the lambdas and create payload.zip on each update in the lambda function folder. I have the blocks below:

resource "null_resource" "lambda_dependencies" {
  triggers = {
    index = "${base64sha256(file("./index.js"))}"
  }

  provisioner "local-exec" {
    command = "mkdir -p ./lambda && npm install --legacy-peer-deps"
  }
}

data "archive_file" "payload_zip" {
  type        = "zip"
  source_dir  = "${path.module}/lambda"
  output_path = "${path.module}/payload.zip"
  depends_on  = [null_resource.lambda_dependencies]
}

resource "aws_lambda_function" "payload" {
  function_name = var.function_name
  filename         = "${data.archive_file.payload_zip.output_path}"
  role             = aws_iam_role.payload.arn
  handler          = var.lambda_handler
  runtime          = var.compatible_runtimes
  depends_on       = [aws_iam_role_policy_attachment.attach_iam_policy_to_iam_role]
  source_code_hash = data.archive_file.payload_zip.output_base64sha256
  publish          = true
}

First part is to test the triggers for changes in the index.js file from the function folder.
Then it checks if the lambda subfolder exists or not and then runs npm install .
After that my problem is with data.archive_file. I want the updated files from in my lambda folder to be checked and then added to the payload archive in the subfolder. If I use {path.module}, {path.root} or {path.cwd} Terraform is checking the module folder not the lambda folder.

Example:

./modules/lambda/  <- where my module is 
./sub1/  etc.  <- where my function is 
./sub1/lambda <- where I want the payload.zip to go

I saw in internet how to use basename(abspath(path.module)) and basename(path.cwd) for source_dir but then Terraform creates subfolder with the same name in the folder.

I created this public GitHub repository for testing purpose if anyone want to help me: GitHub - IvayloB84/terraform-with-sub-projects There might be some differences in the code but this because I’m trying to find solution.

My Terraform version is:
Terraform v1.4.2
on linux_amd64

Regards,
Ivo