如何在 AWS Step Functions 中的 Lambda 函数之间传递参数

How to pass parameters between Lambda Functions in AWS Step Functions

我试图浏览互联网,但 none 的解决方案似乎在 python 中对我有用。基本上我想将一些参数从一个 lambda 函数传递到步进函数中的下一个 lambda 函数。我有点惊讶要找到一个如何做到这一点的简单示例是多么困难?

所以我的第一个 lambda 函数的输出如下:

return {"user_name" : user_name, "region" : region, "instance_id" : instance_id}

我的第二个 lambda 函数尝试导入:

user_name = event["user_name"]
region = event["region"]
instance_id = event["instance_id"]

我的 stepfunction 看起来像:

{
         "StartAt": "CreateEC2Instance",
         "States": {
           "CreateEC2Instance": {
             "Type": "Task",
             "Resource": "arn:aws:states:::lambda:invoke",
             "Parameters": {
               "FunctionName": "MY_ARN_1",
               "Payload": {
                  "region": "eu-central-1",
                  "user_name": "MY_USERNAME"
              }},
             "Next": "AttachEFStoEC2"
           },
            "AttachEFStoEC2": {
             "Type": "Task",
             "Resource":"MY_ARN_2",
             "End": true
         }

错误是:

{
  "error": "KeyError",
  "cause": {
    "errorMessage": "'user_name'",
    "errorType": "KeyError",
    "stackTrace": [
      "  File \"/var/task/lambda_function.py\", line 7, in lambda_handler\n    user_name = event[\"user_name\"]\n"
    ]
  }
}
{
         "StartAt": "CreateEC2Instance",
         "States": {
           "CreateEC2Instance": {
             "Type": "Task",
             "Resource": "arn:aws:states:::lambda:invoke",
             "ResultPath": "$.randomstring",
             "Parameters": {
               "FunctionName": "MY_ARN_1",
               "Payload": {
                  "region": "eu-central-1",
                  "user_name": "MY_USERNAME"
              }},
             "Next": "AttachEFStoEC2"
           },
            "AttachEFStoEC2": {
             "Type": "Task",
             "Resource":"MY_ARN_2",
                   "Parameters": {
                       "outputfromearlierfunction.$": "$.randomstring.Payload"
                   },
             "End": true
         }

然后尝试在函数内访问

receivedinput = event.get('outputfromearlierfunction')
user_name = receivedinput["user_name"]
region = receivedinput["region"]
instance_id = receivedinput["instance_id"]