0

Working on product which is served as PaaS, backend of product is completely developed for serverless using NodeJS Serverless Framework and deployed on AWS Lambda.

When I started working on it, I found there are lots of duplicate code, LambdaFunctionOne and LambdaFunctionTwo both have same function performing same operation, and this is the problem if we have to change the logic of function then we will have to change the function in all lambda function.

Wanted to remove the duplicated, so if LambdaFunctionTwo required the some function which is LambdaFunctionOne then instead of replicating, should invoke LambdaFunctionOne and call its function. Suppose created a lambda function for utils and used that utils in every function of Lambda by invoking lambda instead of replicating.

  1. How does it will affect the pricing?
  2. Is it good practice to call the lambda from another lambda in terms of cost?
  3. Is it good practice to develop such product in serverless?
  • You cannot use a lambda `layer` for the common code for different functions? This is how this issue is usually handled. This is cheaper and faster then putting the common code in separate function. – Marcin Jun 06 '20 at 05:18
  • The down-side of calling a separate function is that you are paying for two simultaneous functions (the calling function, which is waiting, and the called function). It would be better to have the code accessible in the one function. – John Rotenstein Jun 06 '20 at 06:02
  • You can have a single library where you implement the common utility functions and use the library in your code which will be deployed in Lambda. – Alok Singh Jun 06 '20 at 06:33

3 Answers3

2

The down-side of calling a separate function is that you are paying for two simultaneous functions (the calling function, which is waiting, and the called function). It would be better to have the code accessible in the one function.

An exception to this is if you are designing microservices, where each microservice 'owns' a process or some particular data. In this case, it can be better to call another service via a defined API without exposing the inner details of the function.

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
2

How does it will affect the pricing?

You pay for the lambda function execution costs. So if your lambda functions calls another function, you will be charged for both executions. There are no special discounts for such use case unfortunately.

Is it good practice to call the lambda from another lambda in terms of cost?

Its basically same question as the first one. You pay for each execution.

Is it good practice to develop such product in serverless?

Generally you shouldn't tightly couple functions like this. In an ideal case all functions would work independently and would be synchronized using Step Functions, and/or communicate through distributed message systems, such as SQS.

I can recommend AWS white paper on best practices of using lambda:

I found there are lots of duplicate code, LambdaFunctionOne and LambdaFunctionTwo both have same function performing same operation,

This seems as a good use case for lambda layers. You could put all the common code in the layer shared by all the functions.

Marcin
  • 215,873
  • 14
  • 235
  • 294
0

Same as external invocation. Check out the CloudWatch logs, it's still in 100ms increments.

f.ardelian
  • 6,716
  • 8
  • 36
  • 53