将输入(参数)从阶跃函数传递并使用到 lambda 任务

Ili*_*rin 13 python amazon-web-services aws-lambda aws-step-functions

我有一个启动 lambda 的简单步骤函数,我正在寻找一种将参数(事件/上下文)传递给几个后续任务中的每一个的方法。我的步骤函数如下所示:

{
  "Comment": "A Hello World example of the Amazon States Language using an AWS Lambda function",
  "StartAt": "HelloWorld",
  "States": {
    "HelloWorld": {
      "Type": "Task",
      "Parameters": {
        "TableName": "table_example"
      },
      "Resource": "arn:aws:lambda:ap-southeast-2:XXXXXXX:function:fields_sync",
      "End": true
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

在用 Python 编写的 lambda 中,我使用了一个简单的处理程序,它是:

def lambda_handler(event, context):
    #...
Run Code Online (Sandbox Code Playgroud)

事件和上下文如下所示(检查日志):

开始请求 ID:f58140b8-9f04-47d7-9285-510b0357b4c2 版本:$LATEST

我找不到将参数传递给此 lambda 并在脚本中使用它们的方法。本质上,我想要做的是运行相同的 lambda,将几个不同的值作为参数传递。

有人可以指出我正确的方向吗?

N.N*_*vic 12

根据您所说的:“寻找一种将参数(事件/上下文)传递给每个后续任务的方法”,我假设您想要将非静态值传递给 lambda。

通过状态机传递参数有两种方法。通过InputPathParameters. 如需了解差异,请查看此处

如果您没有任何要传递给 lambda 的静态值,我将执行以下操作。将所有参数以 json 格式传递给步骤函数。

状态机的输入 JSON

{
  "foo": 123,
  "bar": [
    "a",
    "b",
    "c"
  ],
  "car": {
    "cdr": true
  },
  "TableName": "table_example"
}
Run Code Online (Sandbox Code Playgroud)

在步骤函数中,您使用 显式地将整个 JSON 传递给 lambda "InputPath": "$",除了隐式传递的第一步之外。有关$路径语法的更多信息,请查看此处。您还需要使用多种方法之一来处理任务结果ResultPath。对于大多数情况,最安全的解决方案是将任务结果保存在特殊变量中"ResultPath": "$.taskresult"

{
  "Comment": "A Hello World example of the Amazon States Language using an AWS Lambda function",
  "StartAt": "HelloWorld",
  "States": {
    "HelloWorld": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:ap-southeast-2:XXXXXXX:function:fields_sync",
      "Next": "HelloWorld2"
    },
    "HelloWorld2": {
      "Type": "Task",
      "InputPath": "$",
      "ResultPath": "$.taskresult"
      "Resource": "arn:aws:lambda:ap-southeast-2:XXXXXXX:function:fields_sync_2",
      "End": true
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

在 lambda 中它成为事件变量并且可以作为 python 字典访问

{
  "Comment": "A Hello World example of the Amazon States Language using an AWS Lambda function",
  "StartAt": "HelloWorld",
  "States": {
    "HelloWorld": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:ap-southeast-2:XXXXXXX:function:fields_sync",
      "Next": "HelloWorld2"
    },
    "HelloWorld2": {
      "Type": "Task",
      "InputPath": "$",
      "ResultPath": "$.taskresult"
      "Resource": "arn:aws:lambda:ap-southeast-2:XXXXXXX:function:fields_sync_2",
      "End": true
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

通过这种方法,您可以使用多个步骤函数和不同的 lambda,并且通过移动 lambda 中的所有逻辑仍然保持它们干净且小。此外,它更容易调试,因为所有事件变量在所有 lambda 中都是相同的,因此通过简单的操作,您可以看到整个状态机所需的所有参数以及可能出现的问题。print(event)


Far*_*hat 6

我遇到过这个,显然,当Resource设置为 lambda ARN(例如"arn:aws:lambda:ap-southeast-2:XXXXXXX:function:fields_sync")时,您不能使用 来Parameters指定输入,而是传递步骤函数的状态(可能是状态函数的输入,如果没有走在它前面)。

要通过参数传递函数输入,您可以将资源指定为并在参数部分中"arn:aws:states:::lambda:invoke"提供:FunctionName

{
    "StartAt": "HelloWorld",
    "States": {
        "HelloWorld": {
            "Type": "Task",
            "Resource": "arn:aws:states:::lambda:invoke",
            "Parameters": {
                "FunctionName": "YOUR_FUNCTION_NAME",
                "Payload": {
                    "SOMEPAYLOAD": "YOUR PAYLOAD"
                }
            },
            "End": true
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以在此处找到调用 Lambda 函数的文档:https://docs.aws.amazon.com/step-functions/latest/dg/connect-lambda.html

您还可以使用 inputPath,或者也可以使用步骤函数状态函数中的元素:https://docs.aws.amazon.com/step-functions/latest/dg/input-output-inputpath-params.html