Search code examples
amazon-web-servicesgoaws-lambdadomain-driven-designmicroservices

Organize invoking lambda functions


I've got the order microservice that's written as go AWS lambda function.

The main function named order-service bound to API Gateway. It receives several parameters like user_id:int, product_ids:array of int, creates an order with artifacts and returns serialized order with order_id and total price. This function invokes a function named order-item which creates an order-item and returns them in parallel(per product). These functions invoke product and user functions to retrieve information about user and products by their ids. Then, the order function invokes another lambda called fee-function which takes just total price and user id and gives back fee price. Of course, it calls some other function like user function and so on. Basically, this is a simple example of how the service works in general. Any function calls some others like user-discount, state taxes etc.

The questions are:

  1. Is it good that order function invokes fee function through Amazon, but it can just import fee handler package and run it inside itself?(However, fee function may be called from outside so it must be deployed as a separate function as well)
  2. Is it good that each function receives just user id and loads the user invoking user function? Perhaps, better to preload it and pass it through everywhere? Something else?
  3. Is it good that one function calls other functions and they call some others and so on? is there any better approach in my situation? Use SNS, Step function, dependency injections/aws layers.

The main reason why I asked is to withstand thousands of RPM and not pay a lot.

Thanks for helping. I appreciate this.


Solution

  • This is exactly what Step Functions was created for. You can invoke a Step Functions state machine from API Gateway, as you would a Lambda.

    With Step functions you can:

    • Invoke the state machine with parameters
    • Orchestrate the order that Lambda functions are invoked
    • Use the state to store inputs and outputs from each lambda
    • Have decision points to take different paths based on the output of a previous function

    See the AWS Step Functions Getting Started Guide for a good introduction to the service.