Terraform lambda with dependencies (required includes) without using layers

I am new to terraform but I am following some documentation for an app I am trying to support and I am having a little trouble understanding how this works.

Example TF for Lambdas:

data "archive_file" "handler_lambda_archive_file" {
  type        = "zip"
  source_dir  = "../lambda/js/submit-handler"
  output_path = "${path.module}/submit-handler.zip"
}


resource "aws_lambda_function" "handler_lambda_function" {
  function_name = "app-${var.app_lifecycle}-submit-handler-lambda"

  filename         = data.archive_file.handler_lambda_archive_file.output_path
  source_code_hash = filebase64sha256(data.archive_file.handler_lambda_archive_file.output_path)

  handler = "main.handler"
  runtime = "nodejs14.x"
  timeout = 240

  role = aws_iam_role.handler_lambda_iam_role.arn

  environment {
    variables = {
      app_lifecycle                     = var.app_lifecycle
    }
  }

  vpc_config {
    subnet_ids         = var.subnets_for_lambda
    security_group_ids = [aws_security_group.handler_lambda_sg.id]
  }

  tags = var.tags
}

resource "aws_lambda_function" "handler_lambda_function2" {
    function_name = "zoom-${var.app_lifecycle}-submit-handler2-lambda"
  
    filename         = data.archive_file.handler_lambda_archive_file.output_path
    source_code_hash = filebase64sha256(data.archive_file.handler_lambda_archive_file.output_path)
  
    handler = "main.handler"
    runtime = "nodejs14.x"
    timeout = 240
  
    role = aws_iam_role.handler_lambda_iam_role.arn
  
    environment {
      variables = {
        app_lifecycle                     = var.app_lifecycle
      }
    }
  
    vpc_config {
      subnet_ids         = var.subnets_for_lambda
      security_group_ids = [aws_security_group.handler_lambda_sg.id]
    }
  
    tags = var.tags
  }

In the source_dir folder, there are the following files:

main.js, auth.js, shared.js.

In both the main and auth JS files, there is a line at the top requiring the include: const someSharedFunction = require("./shared.js");

If I understand, there will be two lambda functions created with the following names:

app-sandbox-submit-handler-lambda and app-sandbox-submit-handler2-lambda

My question is regarding that shared.js file. The archive file contains all 3 js files in the zip and will create the terraform functions using that.

Does it detect that there is a require in those files and automatically put a copy of the shared.js file into each one of the functions so that it can be used?

It isn't using layers, so I'm just not sure how the requires work in this case.

Question:

Does the terraform detect require when creating the functions and include any referenced files with the lambda?


Solution 1:

Nothing is looking at your JavaScript code and detecting that a require is pulling in some dependency. Terraform isn't looking at your Lambda function's code at all.

What is happening is the entire directory is being zipped, because you are giving a source_dir to the archive_file resource.