Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass parameters to serverless invoke local

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 
like image 314
Lasitha Yapa Avatar asked Sep 10 '18 04:09

Lasitha Yapa


People also ask

How do I run serverless locally?

The serverless offline plugin for Node. js allows you to emulate AWS Lambda and API Gateway on a local machine. By using the serverless offline plugin, you can test your serverless applications without deploying them every time you make a change.

How do I invoke lambda function locally?

You can invoke your function locally by using the sam local invoke command and providing its function logical ID and an event file. Alternatively, sam local invoke also accepts stdin as an event. For more information about events, see Event in the AWS Lambda Developer Guide.

Which command must you run to invoke a serverless application locally?

The sam local invoke command is useful for developing serverless functions that handle asynchronous events, such as Amazon Simple Storage Service (Amazon S3) or Amazon Kinesis events. It can also be useful if you want to compose a script of test cases. You can pass in the event body using the --event parameter.


1 Answers

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

like image 179
maxrodrigo Avatar answered Sep 18 '22 10:09

maxrodrigo