How to pass parameters to serverless invoke local

Use --data and pass is any format of data you want to send it to the local lambda.

String Data Example:

serverless invoke --function functionName --stage dev --region us-east-1 --data "hello world"

JSON Data Example:

serverless invoke --function functionName --stage dev --region us-east-1 --data '{ "property1": "value"}'

JSON Data from file:

serverless invoke --function functionName --stage dev --region us-east-1 --path lib/data.json

Complete documentation can be accessed from here

Hope it helps.


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


I've tried the answers with the attribute --data but neither works.
In fact the problem is that the --data will pass a string value to the framework. So if you write in your javascript file:console.log(typeof(event));, you will get String instead of Object. Which means serverless framework doesn't transform the input to a JSON object correctly. That's why you got xx of undefined error.

My solution is to use the -p(or --path) attribute. In your example, follow these steps:

  1. create a .json file at the current location of your console. eg: test.json
  2. in the json file write: {"pathParameters":{"food_id":"100"}}
  3. in the js file, to get the food_id, use event.pathParameters.food_id
  4. run command: sls invoke local -f yourFunction -p test.json

For future reference. Your case would have been solved like this. Just figured it out thanks to Kannaiyans JSON Example.

sls invoke local -f getFoodDetails --data '{ "queryStringParameters": {"food_id":"123"}}'