How to pass parameters to serverless invoke local

前端 未结 6 1865
北荒
北荒 2020-12-29 18:59

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 te

6条回答
  •  醉酒成梦
    2020-12-29 19:08

    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

提交回复
热议问题