0

I have a simple lambda consisting of index.js and node_modules. When I zip these two files (either by zipping both directly or by zipping the parent directory), it places them inside a folder which is then zipped.

I then upload this zip to my S3 bucket which works fine.

My Lambda however, can't find the right files because it seems to have nested the index.js handler inside an extra directory (I assume this happens from the zipping)

enter image description here

I am using Terraform to create my Lambda function which references the S3 bucket:

resource "aws_lambda_function" "Lambda" {
  s3_bucket     = var.bucket_id
  s3_key        = "on-sign-in-trigger.zip"
  handler       = "index.handler"
  runtime       = "nodejs18.x"
  role          = aws_iam_role.lambda_iam_role.arn
  function_name = "on-sign-in-trigger-${var.env_name}"
}

Do I need to configure Lambda so that it knows to go a folder deeper to get the handler function or is there another way I should be zipping / uploading my code to S3?

Mark B
  • 183,023
  • 24
  • 297
  • 295
Stretch0
  • 8,362
  • 13
  • 71
  • 133
  • I believe the handler has to be in the root of the zip file, not in a folder. You seem unclear on what is actually happening in your zip file. At a minimum you should open the zip file and look at the contents to verify how it is structured. That is a minimum level of debugging you should do before anyone can really help you. – Mark B Apr 24 '23 at 16:46
  • This answer is what you are looking for:) [Creating a lambda function in AWS from zip file](https://stackoverflow.com/a/38623471/12326605) – Arpit Jain Apr 24 '23 at 16:50
  • Thanks @MarkB, as I mentioned in my post, when I zip the files, it places them inside a folder. My question was is there another way to zip them to avoid it getting nested or if I can update my lamdba config. Looks like I've found the answer though. Cheers – Stretch0 Apr 24 '23 at 17:01
  • You said "I assume this happens from the zipping" which made it unclear what you were actually sure of. – Mark B Apr 24 '23 at 17:01

1 Answers1

1

Turns out I needed to add the folder path to my handler value:

resource "aws_lambda_function" "Lambda" {
  s3_bucket     = var.bucket_id
  s3_key        = "on-sign-in-trigger.zip"
  handler       = "on-sign-in-trigger/index.handler" // add directory path here
  runtime       = "nodejs18.x"
  role          = aws_iam_role.lambda_iam_role.arn
  function_name = "on-sign-in-trigger-${var.env_name}"
}

I guess on-sign-in-trigger/index.handler is the path the lambda looks for inside the zipped file

Stretch0
  • 8,362
  • 13
  • 71
  • 133