Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serverless 502 Bad Gateway

I’m able to follow the documentation to create a simple serverless function, but when I added the http listener I keep getting a 502 Bad Gateway when trying to hit my endpoint.

How can i debug this?

'use strict';

module.exports.hello = async (event, context) => {
  return {
    statusCode: 200,
    body: {
      message: 'Go Serverless v1.0! Your function executed successfully!',
      input: event,
    },
  };
};

serverless.yaml

service: playing-with-serverless # NOTE: update this with your service name

provider:
  name: aws
  runtime: nodejs8.10

functions:
  hello:
    handler: handler.hello
    events:
      - http:
          path: hello
          method: get

I’ve deployed my function

 $ sls deploy
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service .zip file to S3 (423 B)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
..............
Serverless: Stack update finished...
Service Information
service: playing-with-serverless
stage: dev
region: us-east-1
stack: playing-with-serverless-dev
api keys:
  None
endpoints:
  GET - https://1r5mt9wa63.execute-api.us-east-1.amazonaws.com/dev/hello
functions:
  hello: playing-with-serverless-dev-hello
layers:
  None
Serverless: Removing old service artifacts from S3...

cURL

$ curl --request GET \
  --url https://1r5mt9wa63.execute-api.us-east-1.amazonaws.com/dev/hello
{"message": "Internal server error"}%```  
like image 735
Catfish Avatar asked Jan 26 '23 21:01

Catfish


2 Answers

You need to stringify body in the response object:

return {
  statusCode: 200,
  body: JSON.stringify({
    message: 'Go Serverless v1.0! Your function executed successfully!',
    input: event
  })
};

See docs specifically Output Format of a Lambda Function for Proxy Integration

Set up an Integration Response in API Gateway

like image 105
A.Khan Avatar answered Jan 29 '23 11:01

A.Khan


There could be an error on your server side code.

Try testing your request on the AWS console. It will point you exactly to the error in your Javascript code if there is one.

enter image description here

like image 44
live-love Avatar answered Jan 29 '23 12:01

live-love