AWS Step Function在lambda java中将整个输入作为有效负载传递

时间:2020-04-25 14:12:25

标签: aws-step-functions

在第一步中,我将lambda函数用作任务,并且希望将整个输入作为payload传递,以便它可以转换为我定义的强类型Java对象。

我作为对象的数据类型:

@Data
public class JobMetaData {

    public JobMetaData() {

    }

    private String jobName;
    private String jobId;

Lambda函数:

@Override
public JobMetaData handleRequest(final JobMetaData jobMetaData,
                                 final Context context) {

步骤:

"Preparing Job": {
  "Next": "Submitting Job",
  "InputPath": "$",
  "OutputPath": "$.bakeJobResult",
  "Type": "Task",
  "Comment": "Preparing Job",
  "Parameters": {
    "FunctionName": "MyLambdaFunctionName",
    "Payload": {
      "$": "$"
    }
  },
  "Resource": "arn:aws:states:::lambda:invoke",
  "ResultPath": "$.bakeJobResult"
}

以上步骤将导致JobMetaData作为null传递。 我只能将其更改为:

    "Payload": {
      "jobName.$": "$.jobName",
      "jobId.$": "$.jobId"
    }

但是如果我有很多字段,这意味着我需要提取所有json字段并再次构造它们以使其成为有效负载。我正在使用CDK定义状态机,看起来Payload部分定义为Map<String, Object>。有什么办法可以将整个输入作为有效载荷传递?

2 个答案:

答案 0 :(得分:2)

您实际上可以执行以下操作:

  "Parameters": {
    "FunctionName": "MyLambdaFunctionName",
    "Payload.$": "$"
  }

它将传递整个有效负载,而无需将其嵌入另一个字段中。

答案 1 :(得分:0)

是的。你可以这样做。在有效负载中,您可以像这样定义它:

#include <stdio.h>

int playerscores(int n) //function asking how many points each player has
{
    int p = 0; //variable storing the points
    printf("how many points does player %d have?\n", n+1);
    scanf("%d", &p); //asking how many points and writing to variable p
    return p; //return p for the points
}

int main()
{
    int n = 0; //number of players
    int scoret = 0; //score total
    printf("How many players are on your team?\n");
    scanf("%d", &n); //asking number of players
    int score[n];  // integer array to store player scores

    // using a for loop -> don't need to worry about incrementing the index
    for (int a = 0; a < n; a++)
    {
        score[a] = playerscores(a); //calling function and define array at position a
        scoret += score[a]; //adding up scores thus far
    }

    printf("Total score: %d\n", scoret);

    return scoret; //final score
}

因此,在lambda端,您将收到以下有效负载:

"Payload":{
    "entireInput.$": "$"
}

因此您的有效载荷将为{ "entireInput" : {"jobName":"abc", "jobId": 123, ....so on} } ,然后您可以将对象解析为Map<String,Object>,并获取所有输入值以在其中执行step函数。因此,我认为您可能需要在POJO类中进行一些更改。