to Function Lambda AWS to parameters additional inject to How

huangapple go评论59阅读模式
英文:

How to inject additional parameters to AWS Lambda Function when it invoked by Step Function

问题

I have defined AWS Step Function using Terraform and that Step Function has 2 Tasks. And if any error occurs in the 1st task, it goes to the CATCH block, and from there we are calling the 2nd Task. The 2nd Task triggers a Java Lambda Handler that handles the error that occurred in the 1st task.

我已经使用Terraform定义了AWS Step Function,并且这个Step Function有2个任务。如果在第一个任务中发生任何错误,它会进入CATCH块,然后从那里调用第二个任务。第二个任务触发一个Java Lambda处理程序,用于处理第一个任务中发生的错误。

I have already added the error I got in the 1st task to the State using "ResultPath" as shown in the below code and the Java Lambda Handler in the 2nd Task is reading that error and processing it. The entire state which contains the error will be passed to the "Java Lambda Error Handler" as a Parameter as shown in the below code.

我已经使用下面的代码将第一个任务中发生的错误添加到了状态中,并且第二个任务中的Java Lambda处理程序正在读取该错误并处理它。包含错误的整个状态将作为参数传递给下面的代码中的“Java Lambda错误处理程序”。

So my question here is: Once the control reaches the "Second Task", I want to add a couple of additional fields that should go as inputs to the Lambda Handler. I want to add a couple of additional fields to the existing State (that contains error details) that are already going as input to the Lambda Handler.

所以我的问题是:一旦控制到达“第二个任务”,我想添加一些附加字段,这些字段应该作为输入传递给Lambda处理程序。我想要向已存在的状态(其中包含错误详情)中添加一些附加字段,这些字段已作为输入传递给Lambda处理程序。

Could someone please help me with how I can add a couple of additional fields in my below code?

请问有人能帮助我如何在下面的代码中添加一些附加字段吗?

"States": {
"First_Task": {
...
...
"Catch": [
{
"ErrorEquals": [
"States.All"
],
"Next": "Second_Task",
"ResultPath": "$.error"
}
],
"OutputPath": "$.Payload",
"End": true
},
"Second_Task": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"OutputPath": "$.Payload",
"Parameters": {
"Payload.$": "$", // -> here we are passing the error details to Lambda
// -> Can I pass some additional fields here as input to the Lambda Handler
"FunctionName": "${}"
},
......
}
}

英文:

I have defined AWS Step Function using Terraform and that Step Function has 2 Tasks. And if any error occurs in the 1st task, it goes to the CATCH block, and from there we are calling the 2nd Task. The 2nd Task triggers a Java Lambda Handler that handles the error that occurred in the 1st task.

I have already added the error I got in the 1st task to the State using "ResultPath" as shown in the below code and the Java Lambda Handler in the 2nd Task is reading that error and processing it. The entire state which contains the error will be passed to the "Java Lambda Error Handler" as a Parameter as shown in the below code.

So my question here is: Once the control reaches the "Second Task", I want to add a couple of additional fields that should go as inputs to the Lambda Handler. I want to add a couple of additional fields to the existing State (that contains error details) that are already going as input to the Lambda Handler.

Could someone please help me with how I can add a couple of additional fields in my below code?

"States": {
    "First_Task": {
        ...
        ...
        "Catch": [
         {
            "ErrorEquals": [
               "States.All"
            ], 
            "Next": "Second_Task",
            "ResultPath": "$.error"
         }
        ],
        "OutputPath": "$.Payload", 
        "End": true
    },
    "Second_Task": {
       "Type": "Task",
       "Resource": "arn:aws:states:::lambda:invoke",
       "OutputPath": "$.Payload", 
       "Parameters": {
          "Payload.$": "$",  // -> here we are passing the error details to Lambda
          // -> Can I pass some additional fields here as input to the Lambda Handler
          "FunctionName": "${<ErrorHandlerLambdaFunctionName>}"
       },
       ......
    }
}

答案1

得分: 1

你可以使用参数字段来构建发送到Lambda函数的有效载荷,通过从输入、Step Functions上下文对象和文字值组合而成。以下是一个示例状态机:

{
  "StartAt": "FIrst Task",
  "States": {
    "FIrst Task": {
      "Type": "Task",
      "Resource": "arn:aws:states:::lambda:invoke",
      "OutputPath": "$.Payload",
      "Parameters": {
        "Payload.$": "$",
        "FunctionName": "arn:aws:lambda:ca-central-1:123456789012:function:function_that_will_fail"
      },
      "End": true,
      "Catch": [
        {
          "ErrorEquals": [
            "States.ALL"
          ],
          "Next": "Second Task",
          "ResultPath": "$.error"
        }
      ]
    },
    "Second Task": {
      "Type": "Task",
      "Resource": "arn:aws:states:::lambda:invoke",
      "OutputPath": "$.Payload",
      "Parameters": {
        "Payload": {
          "error.$": "$.error",
          "full_input.$": "$",
          "step_functions_execution_id.$": "$$.Execution.Id",
          "static_stuff": {
            "some_array": [
              1,
              2,
              3,
              4
            ],
            "some_key": "some_value"
          }
        },
        "FunctionName": "arn:aws:lambda:ca-central-1:123456789012:function:return_what_was_passed"
      },
      "End": true
    }
  }
}

当我使用一个简单的注释作为输入执行这个状态机时,这是我将收到的内容:

{
  "static_stuff": {
    "some_array": [
      1,
      2,
      3,
      4
    ],
    "some_key": "some_value"
  },
  "step_functions_execution_id": "arn:aws:states:ca-central-1:123456789012:execution:so-inject-data:faf01f02-10f0-4207-94f0-2b5b1c2b5ba2",
  "full_input": {
    "Comment": "这是输入执行的注释",
    "error": {
      "Error": "ValueError",
      "Cause": "{\"errorMessage\":\"这个函数失败了\",\"errorType\":\"ValueError\",\"requestId\":\"1e569a61-38e6-432f-8f77-bae1331d6d11\",\"stackTrace\":[\"File \\\"/var/task/lambda_function.py\\\", line 4, in lambda_handler\\n raise ValueError(\\\"这个函数失败了\\\")\\n\"]}"
    }
  },
  "error": {
    "Error": "ValueError",
    "Cause": "{\"errorMessage\":\"这个函数失败了\",\"errorType\":\"ValueError\",\"requestId\":\"1e569a61-38e6-432f-8f77-bae1331d6d11\",\"stackTrace\":[\"File \\\"/var/task/lambda_function.py\\\", line 4, in lambda_handler\\n raise ValueError(\\\"这个函数失败了\\\")\\n\"]}"
  }
}
英文:

You can use the Parameters fields to construct the payload that you send to your Lambda function by composing from the input, Step Functions Context Object, and literal values. See an example state machine below:

{
  "StartAt": "FIrst Task",
  "States": {
    "FIrst Task": {
      "Type": "Task",
      "Resource": "arn:aws:states:::lambda:invoke",
      "OutputPath": "$.Payload",
      "Parameters": {
        "Payload.$": "$",
        "FunctionName": "arn:aws:lambda:ca-central-1:123456789012:function:function_that_will_fail"
      },
      "End": true,
      "Catch": [
        {
          "ErrorEquals": [
            "States.ALL"
          ],
          "Next": "Second Task",
          "ResultPath": "$.error"
        }
      ]
    },
    "Second Task": {
      "Type": "Task",
      "Resource": "arn:aws:states:::lambda:invoke",
      "OutputPath": "$.Payload",
      "Parameters": {
        "Payload": {
          "error.$": "$.error",
          "full_input.$": "$",
          "step_functions_execution_id.$": "$$.Execution.Id",
          "static_stuff": {
            "some_array": [
              1,
              2,
              3,
              4
            ],
            "some_key": "some_value"
          }
        },
        "FunctionName": "arn:aws:lambda:ca-central-1:123456789012:function:return_what_was_passed"
      },
      "End": true
    }
  }
}

When I execute this with a simple comment as input, this is what I'll get back

{
  "static_stuff": {
    "some_array": [
      1,
      2,
      3,
      4
    ],
    "some_key": "some_value"
  },
  "step_functions_execution_id": "arn:aws:states:ca-central-1:123456789012:execution:so-inject-data:faf01f02-10f0-4207-94f0-2b5b1c2b5ba2",
  "full_input": {
    "Comment": "This is a comment for input to the execution",
    "error": {
      "Error": "ValueError",
      "Cause": "{\"errorMessage\":\"This function failed\",\"errorType\":\"ValueError\",\"requestId\":\"1e569a61-38e6-432f-8f77-bae1331d6d11\",\"stackTrace\":[\"  File \\\"/var/task/lambda_function.py\\\", line 4, in lambda_handler\\n    raise ValueError(\\\"This function failed\\\")\\n\"]}"
    }
  },
  "error": {
    "Error": "ValueError",
    "Cause": "{\"errorMessage\":\"This function failed\",\"errorType\":\"ValueError\",\"requestId\":\"1e569a61-38e6-432f-8f77-bae1331d6d11\",\"stackTrace\":[\"  File \\\"/var/task/lambda_function.py\\\", line 4, in lambda_handler\\n    raise ValueError(\\\"This function failed\\\")\\n\"]}"
  }
}

huangapple
  • 本文由 发表于 2023年5月26日 11:41:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76337514.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定