1

Using Step Functions to Orchestrate a Series of Lambda Functions

 1 year ago
source link: https://nodogmablog.bryanhogan.net/2023/04/using-step-functions-to-orchestrate-a-series-of-lambda-functions/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Using Step Functions to Orchestrate a Series of Lambda Functions

Download full source code.

This a simple example of using step functions to orchestrate a series of Lambda functions. The Lambda functions execute quickly, return a result, and trigger the next step.

In a follow-up post, I’ll show how to use step functions where a step pauses and needs to be manually resumed at a later time.

This post is not a step-by-step guide, I wrote some earlier posts with more background information, you can look those up.

Overview of the state machine

Though the service is called Step Functions, what you build is referred to as a state machine.

state-machine_hu042733eb99acf17b0cede49d3034023d_54000_e8b3f5ff3745fe1c63e516661c896e6e.webp

This state machine is a simple one. It uses Lambda functions to decide if the Person is an adult or a child, then process the person accordingly.

The first step sends the Person to a Lambda function that determines based on the date of birth whether the person is an adult or a child. The Lambda function returns the Person with the IsAdult property set to true or false.

The next step examines the IsAdult property and calls the appropriate Lambda function to process the child/adult, passing the Person as input.

Definition of the state machine

Here is the definition, you can use this to create the state machine in the AWS console. Name the state machine “ProcessAdultOrChild”. You must replace the account id with your own.

It refers to three Lambda functions, you will see how to create them in the next section.

{
  "Comment": "Determine if a person is adult/child and process accordingly",
  "StartAt": "Assign adult/child to input",
  "States": {
    "Assign adult/child to input": {
      "Type": "Task",
      "Resource": "arn:aws:states:::lambda:invoke",
      "OutputPath": "$.Payload",
      "Parameters": {
        "Payload.$": "$",
        "FunctionName": "arn:aws:lambda:us-east-1:012345678910:function:AdultOrChild:$LATEST"
      },
      "Retry": [
        {
          "ErrorEquals": [
            "Lambda.ServiceException",
            "Lambda.AWSLambdaException",
            "Lambda.SdkClientException",
            "Lambda.TooManyRequestsException"
          ],
          "IntervalSeconds": 2,
          "MaxAttempts": 6,
          "BackoffRate": 2
        }
      ],
      "Next": "Choice"
    },
    "Choice": {
      "Type": "Choice",
      "Choices": [
        {
          "Variable": "$.IsAdult",
          "BooleanEquals": false,
          "Next": "Process child"
        }
      ],
      "Default": "Process adult"
    },
    "Process child": {
      "Type": "Task",
      "Resource": "arn:aws:states:::lambda:invoke",
      "OutputPath": "$.Payload",
      "Parameters": {
        "Payload.$": "$",
        "FunctionName": "arn:aws:lambda:us-east-1:012345678910:function:ProcessChild:$LATEST"
      },
      "Retry": [
        {
          "ErrorEquals": [
            "Lambda.ServiceException",
            "Lambda.AWSLambdaException",
            "Lambda.SdkClientException",
            "Lambda.TooManyRequestsException"
          ],
          "IntervalSeconds": 2,
          "MaxAttempts": 6,
          "BackoffRate": 2
        }
      ],
      "End": true
    },
    "Process adult": {
      "Type": "Task",
      "Resource": "arn:aws:states:::lambda:invoke",
      "OutputPath": "$.Payload",
      "Parameters": {
        "Payload.$": "$",
        "FunctionName": "arn:aws:lambda:us-east-1:012345678910:function:ProcessAdult:$LATEST"
      },
      "Retry": [
        {
          "ErrorEquals": [
            "Lambda.ServiceException",
            "Lambda.AWSLambdaException",
            "Lambda.SdkClientException",
            "Lambda.TooManyRequestsException"
          ],
          "IntervalSeconds": 2,
          "MaxAttempts": 6,
          "BackoffRate": 2
        }
      ],
      "End": true
    }
  }
}

The Lambda functions

The Lambda functions are simple (see the attached zip). The first AdultOrChild takes in a Person and returns a Person with the Age and IsAdult properties set.

The other two, ProcessChild and ProcessAdult take in the Person. They return a Response with a message and a bool indicating if the person is an adult or child.

Deploy the three functions with the names mentioned here.

For instructions on deploying Lambda functions, see this post

Testing the state machine

You can test the state machine in the AWS console. Open the ProcessAdultOrChild state machine then click Start Execution.

open-start-execution_hudd6275546d98d6adb67fbf8b6b9689f3_47622_a012a676186968d14db02778c89ed30f.webp

Paste in JSON and click Start Execution. The JSON should look like this:

{
  "FirstName": "Melissa",
  "LastName": "Conner",
  "DateOfBirth": "2000-11-09"
}
start-execution_huda49d4e00e47a4ac3a6da1eaa50c036e_36895_f0b5913ebebdf52a662028a43d3ebb17.webp

This will kick off the state machine.

Then you will see the execution complete.

execution-complete_hu838976d940b3648df78a5127abbf1824_22332_8b8f53495dad7a64b07ee736c6a6dcde.webp

Try with a different date of birth to see the state machine execute the other path.

Conclusion

Step functions and state machines are a powerful way to orchestrate a series of Lambda functions. This example showed how to orchestrate a series of functions that respond quickly.

In a follow-up post, I will show how a state machine can be paused while a long-running task is performed. Then the state machine can be resumed when the long-running task is complete.

Download full source code.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK