Out of the box a AWS Lambda function is just an ephemeral container running our code. In order to invoke it and return a result we need to configure the AWS service API Gateway and connect it to our function.
Instructor: [00:02] The Serverless Framework allows us to attach an HTTP endpoint to our lambda function for an event concept. In its simplest version, we define the path and the method. By the way, by default lambdas can only be invoked using the SDK.
[00:15] There are other AWS services like API Gateway that can invoke lambda and under the hood the framework actually is setting up an API Gateway for you, but abstracting away all the overhand. Once you start using the HTTP event, though, the expected response must be an object containing at least a status code as well as the body. In our case, we return a stringified JSON response.
[00:41] Now that everything is set up, we run sls deploy. Note that in this case, we can't run sls deploy function since we changed some configuration. In this case, the API Gateway service needs to be set up. After the deploy succeeded, we can use cURL to invoke the published endpoint.
[01:03] Voila! Works as expected.
@Kenneth you can, but in that case you need to use the third argument callback and call it with the response as second argument:
module.exports.run = (event, context, callback) => {
const response = {
statusCode: 200,
body: JSON.stringify({
message: "Hello",
}),
};
callback(null, response);
};
Thank you @Nik
Hi, I'm not getting any endpoint after deploy. I've copied the yml configuration from the video but It does not work. I went to this page to get the embedded transcript from github, and this one is also not working. I have a None for api keys, endpoints and layers. I can reach my function by calling
sls invoke --function helloWorld -l
For the endpoint, there was some issue and I had to use with sls login
and followed the getting started document https://github.com/serverless/enterprise/blob/master/docs/getting-started.md
. It worked.
Thanks.
Hi, do we have to use async?
I tried doing the following and i'm getting internal server error: