5

In one of my step, I am using a lambda function as a task and I want to pass the entire input as the payload so that it can convert to a strong type Java object I defined.

My data type as Object:

@Data
public class JobMetaData {

    public JobMetaData() {

    }

    private String jobName;
    private String jobId;

Lambda Function:

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

Step:

"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"
}

Above step will result in JobMetaData passed in as null. I can only get it work by changing it to:

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

But if I have a lot of fields, this means I need to extract all json field and construct them again to make it a Payload. I am using CDK to define my state machine, it looks like the Payload part is defined as a Map<String, Object>. Is there a way I can just pass the entire input as payload?

chrisTina
  • 2,298
  • 9
  • 40
  • 74

2 Answers2

12

You can actually do the following:

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

The will pass the entire payload without embedding it inside another field.

Uri
  • 131
  • 1
  • 4
0

Yes. you can do this. In payload you can define it like:

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

So at your lambda end, you are gonna receive payload as:

{
"entireInput" : {"jobName":"abc", "jobId": 123, ....so on}
}

So your payload is gonna be Map<String,Object> and then you can parse the object into a Map<String,String> and get all the input values to step function in there. So i think you might have to change a little in your POJO class.

Frosty
  • 560
  • 2
  • 12