AWS Lambda Finite State Machines

A Python framework for developing finite state machine-based workflows on AWS Lambda.

View project on GitHub
« Architecture Justification »

Overview

A framework for running Finite State Machines (FSMs) on AWS Lambda, AWS Kinesis, AWS DynamoDB, AWS SNS, AWS SQS, AWS CloudWatch, and AWS Elasticache.

The FSM implementation is inspired by the paper:

[1] J. van Gurp, J. Bosch, “On the Implementation of Finite State Machines”, in Proceedings of the 3rd Annual IASTED International Conference Software Engineering and Applications,IASTED/Acta Press, Anaheim, CA, pp. 172-178, 1999. (www.jillesvangurp.com/static/fsm-sea99.pdf)

Define Your Process

You probably already know what your process looks like.

FSM

Write Your Code

And probably already have some code that you can import and wrap in a thin Action shim.

from aws_lambda_fsm.action import Action
from my.product import my_fancy_function
from my.product import my_other_fancy_function

class Action2(Action):
  def execute(self, context, obj):
    arg1 = context['arg1']
    mff = my_fancy_function(arg1)
    context['mff'] = mff # store the result in the context for subsequent states
    return "done"
    
class Action5(Action):
  def execute(self, context, obj):
    mff = context['mff'] # grab result of last state from the context
    my_other_fancy_function(mff)
    return "done"

Hook Up Your FSM

All you need to do is configure your state machine, and aws-lambda-fsm will take care of executing it.

machines:
- name: Example

  states:

  - name: FirstState
    initial: true
    entry_action: path.to.Action1
    do_action: path.to.Action2
    exit_action: path.to.Action3
    transitions:
    - target: SecondState
      event: done
    - target: ThirdState
      event: skip

  - name: SecondState
    entry_action: path.to.Action4
    do_action: path.to.Action5
    exit_action: path.to.Action6
    transitions:
    - target: ThirdState
      event: done

  - name: ThirdState
    final: true
    entry_action: path.to.Action7
    do_action: path.to.Action8
    exit_action: path.to.Action9

Lambda Calls

An AWS Lambda function executes for each transition. A diagram and listing of each entry/exit/do action ran in each AWS Lambda function is shown below.

FSM->Lambda

λ1

FirstState.entry_action
FirstState.do_action

λ2

FirstState.exit_action
SecondState.entry_action 
SecondState.do_action

λ3

FirstState.exit_action
ThirdState.entry_action 
ThirdState.do_action

λ4

SecondState.exit_action
ThirdState.entry_action 
ThirdState.do_action

λ5

ThirdState.exit_action
« Architecture Justification »