Abu Tahir
Abu Tahir

Reputation: 11

How to pass the input of Map state to the Map task or Lambda function being executed as a part of Map task?

I am facing this issue where I am unable to fetch the input that I am passing to the Map state in the underlying lambda function.

Some context of what is happening: I defined two Lambdas, the first lambda takes the input from user and passes to next state, which is mapped_task in the below example.

The Map task is reading the rows from a csv file present in s3 bucket. The problem is the second lambda receives this row as input, but I also need the input that was being passed to the map state from first lambda, which here is bucket_name and processable_key_name

I am unable to get this parameters in the lambda inside the Map state.

 "Comment": "This is my first step functions state machine",
 "StartAt": "InitialState",
 "States": {
   "InitialState": {
     "Type": "Task",
     "Resource": "arn:aws:lambda:us-east-1:1234566632:function:stepfunctions-serverless-framework-dev-firstFunction",
     "Next": "mapped_task"
   },
   "mapped_task": {
     "Comment": "This state will be handling the payout of each event",
     "Type": "Map",
     "ItemReader": {
       "Resource": "arn:aws:states:::s3:getObject",
       "ReaderConfig": {
         "InputType": "CSV",
         "CSVHeaderLocation": "FIRST_ROW"
       },
       "Parameters": {
         "Bucket.$": "$.bucket_name",
         "Key.$": "$.processable_key_name"
       }
     },
     "ItemProcessor": {
       "ProcessorConfig": {
         "Mode": "DISTRIBUTED",
         "ExecutionType": "STANDARD"
       },
       "StartAt": "FirstMapTask",
       "States": {
         "FirstMapTask": {
           "Type": "Task",
           "Resource": "arn:aws:states:::lambda:invoke",
           "ResultSelector": {
             "name.$": "$.Payload.name",
             "email.$": "$.Payload.email"
           },
           "ResultPath": "$.output",
           "Parameters": {
             "Payload.$": "$",
             "FunctionName": "arn:aws:lambda:us-east-1:1234566632:function:stepfunctions-serverless-framework-dev-secondFunction"
           },
           "End": true
         }
       }
     },
     "Label": "Processabledata",
     "MaxConcurrency": 40,
     "ToleratedFailurePercentage": 100,
     "End": true
   }
 }
}

I tried a few things such as trying to fetch data like this $$.Map.Item.Value from context and also tried to get map_input from lambda but it's not working.

Upvotes: 0

Views: 1581

Answers (1)

Ali Hasan
Ali Hasan

Reputation: 591

Can you try something like manual payload for your second lambda? and reference your inputs with variable? like this:

    "Parameters": {
      "FunctionName": "your-lambda-arn",
      "Payload": {
        "sampleKey1": "sampleValue1",
        "key2.$": "$.myStateInput.key",
        "key3": 100
      }
    }

passing parameters example https://docs.aws.amazon.com/step-functions/latest/dg/connect-parameters.html

Upvotes: 0

Related Questions