简体   繁体   中英

how to pass an argument into an aws lambda from an aws state machine/step function

I'm trying to pass an argument into a an aws python lambda from a state machine. I've written the following function:

{
  "Comment": "test lambda execution with state machine",
  "StartAt": "testPassInArgs",
  "States": {
    "testPassInArgs": {
      "Type": "Task",
      "Resource":"arn:aws:lambda:....function:testPassInArgs",
      "Next": "testRecieveArgs"
    },

    "testRecieveArgs": {
      "Type": "Task",
      "InputPath": "$.a",
      "Parameters": {
      "InputPath": "$.a"},
      "Resource": "arn:aws:lambda:....:function:testRecieveArgs",
      "End": true

    }

  }
}

here are the lambda functions testPassInArgs:

import json

def lambda_handler(event, context):        
    stations={}
    stations['a']='A'
    stations['b']='B'
    stations['c']='C'    

    return  {
        'a':stations['a'],
        'b':stations['b'],
        'c':stations['c']
    }

the other lambda 'testRecieveArgs:

import json

def lambda_handler(event, context):
    # TODO implement
    out={}
    if type(event) == dict:
        for item in event:
            out[item+item] = event[item]+event[item]
            print(item)
            print(event[item])



    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!'),
        'event': out
    }

I would have expected that the 'Parameters' argument in the second part of the state machine would have parsed the output from the first lambda and passed in only the first part (namely 'A') to the second lambda. However it doesn't. the output of the second lambda is:

"InputPathInputPath": "$.a$.a"

The input of the second lambda is simply the output of the first.

I would like to parse the output of the first lambda and send each part to a different lambda downstream in parallel.

I must be missing a trick here? If anyone knows it would be a great help?

ps I'm sure I can handle the parallelising part, its just parsing the arguments and then passing them into downstream functions I'm struggling with.

The output of the first Lambda function is

{
  "a": "A",
  "b": "B",
  "c": "C"
}

If your second task definition were:

    "testRecieveArgs": {
      "Type": "Task",
      "InputPath": "$.a",
      "Resource": "arn:aws:lambda:....:function:testRecieveArgs",
      "End": true
    }

the second Lambda would receive "A" as its input. However, you added this

      "Parameters": {
        "InputPath": "$.a"
      },

to your second task definition, which would overwrite the input json with

{
  "InputPath": "$.a"
}

Is it a typo?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM