How to pass parameters to serverless invoke local

Lasitha Yapa picture Lasitha Yapa · Sep 10, 2018 · Viewed 23.8k times · Source

I'm working on a aws serverless project and need to test the lambda functions locally.
I am using serverless invoke local -f {function_name} command to test the API calls that does not request any path or query parameters.
My question is how can I use this command to pass any path or query parameter to the function?

Example serverless description

getFoodDetails:
    handler: handler.getFoodDetails
    events:
      - http:
          method: get
          path: /foods/{food_id}
          cors: true
          request:
            parameters:
              paths:
                food_id: true

Answer

maxrodrigo picture maxrodrigo · Oct 12, 2018

Data string

As being said you can use the --data option to pass a string data as an event to your function.

serverless invoke local -f {function_name} --data '{ "queryStringParameters": {"id":"P50WXIl6PUlonrSH"}}'

Data file

What you can also do is to pass a --path to a json file with data as the event, and within the "event file" define the data you want.

serverless invoke --function {function_name} --path event_mock.json

You could somehow return the event from a call and save it in a JSON file or grab one from Amazon. They provide some examples: https://docs.aws.amazon.com/lambda/latest/dg/eventsources.html

Keep in mind that if you pass both --path and --data, the data included in the --path file will overwrite the data you passed with the --data flag.

Documentation: https://serverless.com/framework/docs/providers/aws/cli-reference/invoke#invoke-local