微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

我试图通过互联网查看,但在 python 中似乎没有一个解决方案对我有用。基本上我想将一些参数从一个 lambda 函数转移到一个 step 函数中的下一个 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"]

我的阶梯函数看起来像:

{
         "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"]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。