user1187968
user1187968

Reputation: 7986

AWS Step function process output from lambda

I have a AWS step function need to call 3 lambda in sequence, but at the end of each call, the step function need to process the response from a lambda, and determine the next lambda call.

So how can the step function process the response from a lambda? Can you show an example please?

Upvotes: 0

Views: 2262

Answers (2)

Dhevendhiran M
Dhevendhiran M

Reputation: 1263

Assuming that, you have called a lambda function as the first step of your step function. Based on the response from the lambda, you need to decide on which lambda should be triggered next.

This workaround is pretty straightforward, you can return an attribute (eg: next_state) in the lambda response, create a "Choice" flow, in the step function, and give this next_state attribute as an input.

"Choice" flow is nothing but an if-else condition and you can redirect the next step to the expected lambda.

For example,

enter image description here

and the definition will be something like below,

{
  "Comment": "A description of my state machine",
  "StartAt": "Lambda Invoke 1",
  "States": {
    "Lambda Invoke 1": {
      "Type": "Task",
      "Resource": "arn:aws:states:::lambda:invoke",
      "OutputPath": "$.Payload",
      "Parameters": {
        "Payload.$": "$",
        "FunctionName": "<your lambda name>"
      },
      "Retry": [
        {
          "ErrorEquals": [
            "Lambda.ServiceException",
            "Lambda.AWSLambdaException",
            "Lambda.SdkClientException"
          ],
          "IntervalSeconds": 2,
          "MaxAttempts": 6,
          "BackoffRate": 2
        }
      ],
      "Next": "Choice"
    },
    "Choice": {
      "Type": "Choice",
      "Choices": [
        {
          "Variable": "$.next_state",
          "StringEquals": "Lambda Invoke 2",
          "Next": "Lambda Invoke 2"
        },
        {
          "Variable": "$.next_state",
          "StringEquals": "Lambda Invoke 3",
          "Next": "Lambda Invoke 3"
        }
      ],
      "Default": "Lambda Invoke 3"
    },
    "Lambda Invoke 2": {
      "Type": "Task",
      "Resource": "arn:aws:states:::lambda:invoke",
      "OutputPath": "$.Payload",
      "Parameters": {
        "Payload.$": "$",
        "FunctionName": "<your lambda name>"
      },
      "Retry": [
        {
          "ErrorEquals": [
            "Lambda.ServiceException",
            "Lambda.AWSLambdaException",
            "Lambda.SdkClientException"
          ],
          "IntervalSeconds": 2,
          "MaxAttempts": 6,
          "BackoffRate": 2
        }
      ],
      "End": true
    },
    "Lambda Invoke 3": {
      "Type": "Task",
      "Resource": "arn:aws:states:::lambda:invoke",
      "OutputPath": "$.Payload",
      "Parameters": {
        "Payload.$": "$",
        "FunctionName": "<your lambda name>"
      },
      "Retry": [
        {
          "ErrorEquals": [
            "Lambda.ServiceException",
            "Lambda.AWSLambdaException",
            "Lambda.SdkClientException"
          ],
          "IntervalSeconds": 2,
          "MaxAttempts": 6,
          "BackoffRate": 2
        }
      ],
      "End": true
    }
  }
}

Upvotes: 1

vumdao
vumdao

Reputation: 621

There are two ways of catching response of lambda function from step function.

  1. Using add_retry and add_catch to handle any exception from lambda function eg.
            .start(record_ip_task
                   .add_retry(errors=["States.TaskFailed"],
                              interval=core.Duration.seconds(2),
                              max_attempts=2)
                   .add_catch(errors=["States.ALL"], handler=notify_failure_job)) \
  1. Response value from lambda function such as return '{"Result": True} then the step function job will check that value for next task, eg.
                            .next(
                                is_block_succeed
                                    .when(step_fn.Condition.boolean_equals('$.Result', False), notify_failure_job)
                                    .otherwise(send_slack_task)
                            )

Ref: https://dev.to/vumdao/aws-guardduty-combine-with-security-hub-and-slack-17eh https://github.com/vumdao/aws-guardduty-to-slack

Upvotes: 1

Related Questions