Instructor: In order to configure a lambda function, we need to create a serverless.yml file. In here, we define multiple fields. First, we define a service name, myApp, then set the provider and the runtime. Next up, we allow a functions section, and add a Hello World function.
The handler is referring to the file handler, which should export a function, run. Since we don't have that file yet, we create handler.js. We export the function run, accepting three parameters, event, context, and callback.
The event is an object and contains all the necessary request data. Context is an object as well. It contains a couple of AWS-specific values, like AWS request ID, log group name, and so on. Callback is a function and should be invoked with either an error response as the first argument or a valid response as the second argument.
Now we have all we need to deploy our first function. We can run SLS deploy from our terminal. This will package up to the function to a ZIP file, and then deploy the whole thing with a cloud formation stack.
Once deployed, you can invoke the lambda function directly using SLS invoke. As expected, it returns Hello World. One cool feature about invoke is that you can also log out a debug statement. Let's try this by adding a console log, redeploy our application.
Once finished, invoke with the --log flag. As an alternative, we also can run SLS logs, which will just return the past logs.
Instead of using the callback function, we simply can return a promise as well.
Now we could redeploy the whole stack, but we are not going to do this, because what we are going to use is SLS deploy function. This is way faster, as it doesn't go for cloud formation, but instead just replaces the code ZIP for the specific function.
Once redeployed, we can invoke the function, and see that it only returns hello. Since Node 8 already supports async await, you also can declare a function as async and simply return the desired result. It works like a charm.