Attach a HTTP endpoint to an AWS Lambda function using the Serverless Framework

InstructorNik Graf

Share this video with your friends

Send Tweet

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.

Kenneth Francis
~ 6 years ago

Hi, do we have to use async?

module.exports.run = async (event) => {
  return{
    statusCode: 200,
    body: JSON.stringify({
      message: "Hello World"
    })
  }
}

I tried doing the following and i'm getting internal server error:

module.exports.run = (event) => {
  return{
    statusCode: 200,
    body: JSON.stringify({
      message: "Hello World"
    })
  }
}
Nik Grafinstructor
~ 6 years ago

@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);
};
Kenneth Francis
~ 6 years ago

Thank you @Nik

Ugo Arzur
~ 6 years ago

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
Quang Le
~ 5 years ago

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.