8

I'm having a lambda function which makes use of Graphql and webscraping to return some data as response. Sometimes it works without giving an error like 502, sometimes it doesn't. To me it looks like the 502 errors are completely random, because I send the same requests with the same data all the time.

My timeout is set to 5 minutes in AWS console.

These are the errors I get (in browser):

Failed to load [BIG URL] No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '[mydomainurl]' is therefore not allowed access. The response had HTTP status code 502.

spread.js:25 GET [BIG URL] 502 ()

However when I go to the Network tab in Chrome and view the copied link address in browser, I get a JSON response back with the data I want. I cannot find the error in CloudWatch.

Headers

Response headers

content-length: 36
content-type: application/json
date: Fri, 13 Apr 2018 06:17:06 GMT
status: 502
via: 1.1 67284fcf464f6f1529cc1e521669622c.cloudfront.net (CloudFront)
x-amz-apigw-id: FRC2oEiuDoEF_YQ=
x-amz-cf-id: O1VfzIVPySw657r6WV34EvcPMTyeT7eFUBnM3P30NXBmdjTeWHfryw==
x-amzn-requestid: 4766f279-3ee2-11e8-9e72-bdddab0f02d6
x-cache: Error from cloudfront

Request headers

:authority: wqbgu8c3ql.execute-api.eu-west-1.amazonaws.com
:method: GET
:scheme: https
accept: application/json, text/plain, */*
accept-encoding: gzip, deflate, br
accept-language: nl-NL,nl;q=0.9,en-US;q=0.8,en;q=0.7
origin: [url]
referer: [url]
user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36

Response: {"message": "Internal server error"}

My handler.js

'use strict'

const {
  graphql,
  GraphQLSchema,
  GraphQLObjectType,
  GraphQLString,
  GraphQLNonNull
} = require('graphql')

const schema = require('./schema/schema');


module.exports.query = (event, context, callback) => graphql(schema, event.queryStringParameters.query)
.then(
  result => callback(null, {
    statusCode: 200,
    headers: {
      "Access-Control-Allow-Origin" : "*"
    },
    body: JSON.stringify(result)
  }),
  err => callback(err)
)

Maybe there's something wrong in the way I return the err in the callback?

Elvira
  • 1,410
  • 5
  • 23
  • 50
  • 1
    You can turn on API Gateway logging and you'll see more information in CloudWatch. – Noel Llevares Apr 13 '18 at 07:23
  • 1
    Thank you I will take a look at the API Gateway logging – Elvira Apr 13 '18 at 07:50
  • 1
    Note also, for reference, that in your case, `No 'Access-Control-Allow-Origin' header is present on the requested resource...` is only a *symptom* of the error, not the cause. You don't want to base your troubleshooting on this, and this will not be found in the API Gateway logs. – Michael - sqlbot Apr 13 '18 at 12:01
  • Note also that if you hit `err => callback(err)` in your Lambda code, this does **not** return the error to the requester, at least not by default -- it throws a 502, which is what you are seeing. – Michael - sqlbot Apr 13 '18 at 12:05
  • 1
    See this answer: https://stackoverflow.com/a/43578149/80434 > Lambda functions are limited to a maximum execution time of 5 minutes. The actual limit is configured when the Lambda function is created. The limit is in place because Lambda functions are meant to be small and quick rather than being large applications. Your error message says Task timed out after 15.00 seconds. This means that AWS intentionally stopped the task once it hit a run-time of 15 seconds. It has nothing to do with what the function was doing at the time, nor the file that was being processed. – Korayem Jan 09 '19 at 18:09
  • Have you found a fix for this issue? I'm also currently experiencing it but I don't think it's due to the limit because the response comes back no more than a second. But when I make the same request with exactly the same headers, it then works. Really weird and scary. – PenguinBlues Jun 05 '20 at 07:22

2 Answers2

0

For Python use the following

def returnresponse(status_code, message):
    myjson = {}
    myjson['statusCode']=status_code
    myjson['body']=json.dumps(message)
    myjson['headers']={}
    myjson['headers']['Access-Control-Allow-Origin']="*"
    myjson['headers']['Content-Type']="application/json"
    return myjson

you can remove the Content-Type header from the above. At the end of your lambda type :

return returnresponse(200, "hello world")

Also check your code where you make the API call. In my case I used javascript. If you're using javascript remove the "datatype: json" unless you're returning json data in body.

Showmik Bose
  • 109
  • 5
0

If you use api gateway this one waits 29.1 sec before return error 5XX response even if your lambda has more timeout