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
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With