Using Lambda with multiple handlers in AWS Step Functions

Problem

Using lambda function with multiple handlers is non-trivial in AWS Step Functions

Solution

Lambda is great; however one of the limitations is that only a single handler function is used.  Maintaining multiple lambda functions gets cumbersome quickly.

One common approach is to have a meta  handler that then routes to one of the sub-handlers; typically this is done by passing a name/reference to a handler to be used.

Now the problem arises when one wants to use meta-handlers  with AWS Step Functions.  In particular, it is not possible to edit the message (input/out) that is passed around states.

While editing message is not possible; one can alter it by moving different parts of it around.  It is a little bit tricky since one would have to use a combination of InputPath , ResultPath and OutputPath .  Currently a way to achieve is as following.

{
  "handlers": {
    "h1": {
      "name": "h1"
    },
    "h2": {
      "name": "h2"
    }
  },
  "handler": {
    "name": "doMyThing"
  },
  "details": {
    "path": "yourPath"
  }
}

In your request include handlers that lists the ones that you are planning to use.

Then you you can add a pass state in the AWS Step Function’s State machine to select which of the handlers should be used; here is an example (selecting h1 to be used as a handler for the next state)

{
 "StartAt": "test",
 "States": {
 "test": {
 "Type": "Pass",
 "InputPath": "$.handlers.h1", 
 "ResultPath": "$.handler", 
 "OutputPath": "$",
 "End": true
 }
 }
}

 

 

 

About Neil Rubens

see http://ActiveIntelligence.org
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*