通过 Golang 中的步骤函数调用 AWS lambda 函数

Invoking an AWS lambda function through step functions in Golang

我有一个调用用 Golang 编写的 lambda 函数的 AWS 步骤函数。出于某种原因,lambda 函数似乎无法读取 step 函数的输入。

lambda 函数 -

package main

import (
    "fmt"

    "github.com/aws/aws-lambda-go/lambda"
)

type InEvent struct {
    Name string `json:"name"`
}

type OutEvent struct {
    Greeting string `json:"greeting"`
}

func HandleRequest(name InEvent) (OutEvent, error) {
    var result OutEvent
    result.Greeting = fmt.Sprintf("Hello %s!", name.Name)
    return result, nil
}

func main() {
    lambda.Start(HandleRequest)
}

阶跃函数-

{
  "Comment": "A Hello World example of the Amazon States Language using Pass states",
  "StartAt": "Invoke Lambda function",
  "States": {
    "Invoke Lambda function": {
      "Type": "Task",
      "Resource": "arn:aws:states:::lambda:invoke",
      "Parameters": {
        "FunctionName": "arn:aws:lambda:us-west-1:301438421794:function:SF1_1:$LATEST",
        "Payload": {
          "Input.$": "$"
        }
      },
      "End": true
    }
  }
}

步进函数的输入-

{
  "name": "ABCDE"
}

步进函数的输出-

{
  "ExecutedVersion": "$LATEST",
  "Payload": {
    "greeting": "Hello !"
  },
  "SdkHttpMetadata": {
    "AllHttpHeaders": {
      "X-Amz-Executed-Version": [
        "$LATEST"
      ],
      "x-amzn-Remapped-Content-Length": [
        "0"
      ],
      "Connection": [
        "keep-alive"
      ],
      "x-amzn-RequestId": [
        "5f297331-8b1a-49a0-9ad9-17a78ec1cfd0"
      ],
      "Content-Length": [
        "22"
      ],
      "Date": [
        "Fri, 25 Dec 2020 19:58:20 GMT"
      ],
      "X-Amzn-Trace-Id": [
        "root=1-5fe6445c-5cb6860d2559230940506a2f;sampled=0"
      ],
      "Content-Type": [
        "application/json"
      ]
    },
    "HttpHeaders": {
      "Connection": "keep-alive",
      "Content-Length": "22",
      "Content-Type": "application/json",
      "Date": "Fri, 25 Dec 2020 19:58:20 GMT",
      "X-Amz-Executed-Version": "$LATEST",
      "x-amzn-Remapped-Content-Length": "0",
      "x-amzn-RequestId": "5f297331-8b1a-49a0-9ad9-17a78ec1cfd0",
      "X-Amzn-Trace-Id": "root=1-5fe6445c-5cb6860d2559230940506a2f;sampled=0"
    },
    "HttpStatusCode": 200
  },
  "SdkResponseMetadata": {
    "RequestId": "5f297331-8b1a-49a0-9ad9-17a78ec1cfd0"
  },
  "StatusCode": 200
}

我希望 Output.Payload.greeting 是“你好 ABCDE!”但 Output.Payload.greeting 是“你好!”

这里出了什么问题?为什么 lambda 函数中的名称变量没有正确存储输入?为什么“ABCDE”在lambda函数中变成空字符串?

当我直接在 lambda 控制台上测试 lambda 函数而不使用步骤函数时,一切正常。

您的状态机定义中的 Payload 部分正在调整传递给 Lambda 的输入:

"Payload": {
    "Input.$": "$"
}

因此 Lambda 的实际输入将是:

{ 
    Input: 
        { 
            name: 'ABCDE' 
    } 
}

因此您需要在您的 Golang 代码中考虑这一点,或者更改状态机定义中的 Payload 部分。


分享代码和示例时要小心,因为您的帐户 ID 也会被暴露

下面是你的 golang 代码的工作状态机

    {
    "Comment": "A Hello World example of the Amazon States Language using Pass states",
    "StartAt": "Invoke Lambda function",
    "States": {
        "Invoke Lambda function": {
        "Type": "Task",
        "Resource": "arn:aws:states:::lambda:invoke",
        "Parameters": {
            "FunctionName": "arn:aws:lambda:us-east-1:301438421794:function:testfunction",
            "Payload": {
            "name.$": "$.name"
            }
        },
        "ResultPath": "$.lambdaOutput",
        "End": true
        }
    }
    }

产生如下输出:

    {
    "name": "suresh",
    "lambdaOutput": {
        "ExecutedVersion": "$LATEST",
        "Payload": {
        "greeting": "Hello suresh!"
        },
        "SdkHttpMetadata": {
    ....
    }

您可以通过这样修改来进一步分割输出

 "OutputPath": "$.lambdaOutput.Payload",

这将为您提供准确的输出

    {
    "greeting": "Hello suresh!"
    }

看看here。这是我可以推荐的最好的资源,用于开始使用步进函数。

对于处理函数之间的数据,keep this handy. For handling the inputs and outputs this 也非常有用。