How to pass parameters to serverless invoke local

前端 未结 6 1855
北荒
北荒 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-29 19:16

    There is also an alternative to all the other options here. I wrote a blog article about this and will link it after going through some of the detail.

    There are two basic aspects you need to handle:

    1. Execute the code in your handler and business logic by passing event objects to your handler
    2. Handling requests to AWS services in a way that makes it easy to test

    For the first one, I just added Mocha to the project and used the unit testing framework as a way to be able to click a button in my IDE and have my code be executed with test data. I can also do local debugging, step through code etc without issue. The added advantage is, if you just set this up, like I have, purely to execute your code, you still have the beginnings of a unit test suite you can flesh out later if you wish

    For the second one, its even easier. There is an npm module called aws-sdk-mock and it allows you to register a listener for a specific AWS service and method (such as DynamoDB.query or S3.putItem) and respond to that request with whatever you wish, even errors if you wish to test how your code handles the unthinkable; an AWS service going down.

    With the setup of these two elements, I can locally test any handler I develop. Ultimately you will always need to do some integration testing in the cloud as there is just no local substitute, no matter how elaborate or awesome it seems, for the actual cloud services you will be using, but this can get you quite far.

    https://serverless.com/blog/serverless-local-development

提交回复
热议问题