WHOATEMYNOODLES
WHOATEMYNOODLES

Reputation: 2731

What are the pros and cons of having one lambda function with "smaller functions" vs multiple functions

I have a lambda function with 10 "smaller functions" inside that single lambda which gets accessed by 10 api gateway endpoints.

What are the pros and cons of designing the backend this way? I know it's more difficult to manage all those functions when testing, but how would it compare speedwise to 10 seperate lambda functions?

exports.handler =  (event) => {

    const path = event.path;
    const method = event.httpMethod;

    if(path === '/getmail' && method === 'GET'){
        return mailQuery(event);
    }

   if(path === '/getmessagethread' && method === 'GET'){
        return getMessageThread(event);
    }

   if(path === '/replytomessage' && method === 'POST'){
        return replyToMessage(event);
    }

    if(path === '/sendmessage' && method === 'POST'){
        return sendMessage(event);
    }

    . . .
};

Upvotes: 0

Views: 876

Answers (1)

jmp
jmp

Reputation: 2375

imo do whatever you feel comfortable with and that works best for the use case. Here is just my 2 cents on the subject though:

Pros:

  • Only need to manage one function and it's code
  • This might be easier to test then using SAM Local as we could have multiple event objects to test with

Cons:

  • If multiple requests come in that function's reserved concurrency(if it has that) could be exhausted since it is only that one function serving all those requests
  • API Gateway's max timeout is 30 seconds, so if this function executes longer due to this extra code then the request will fail
  • Could also increase the deployment package size which Lambda has the following limit https://docs.aws.amazon.com/lambda/latest/dg/limits.html

    50 MB (zipped, for direct upload) 250 MB (unzipped, including layers) 3 MB (console editor)

Speedwise I don't think it would make much difference. I don't believe there would be much difference in the time to execute if a function was called as opposed to having the code directly in the handler function.

HTH

-James

Upvotes: 4

Related Questions