title: AWS Lambda randomly gives back 502 as status
body: <p>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.</p>
  
  <p>My timeout is set to 5 minutes in AWS console.</p>
  
  <p>These are the errors I get (in browser):</p>
  
  <blockquote>
    <p>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.</p>
    
    <p>spread.js:25 GET
    [BIG URL] 502 ()</p>
  </blockquote>
  
  <p>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.</p>
  
  <p><strong>Headers</strong></p>
  
  <p><strong>Response headers</strong></p>
  
  <pre><code>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
  </code></pre>
  
  <p><strong>Request headers</strong></p>
  
  <pre><code>: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
  </code></pre>
  
  <p><strong>Response:</strong>
  {"message": "Internal server error"}</p>
  
  <p><strong>My handler.js</strong></p>
  
  <pre><code>'use strict'
  
  const {
    graphql,
    GraphQLSchema,
    GraphQLObjectType,
    GraphQLString,
    GraphQLNonNull
  } = require('graphql')
  
  const schema = require('./schema/schema');
  
  
  module.exports.query = (event, context, callback) =&gt; graphql(schema, event.queryStringParameters.query)
  .then(
    result =&gt; callback(null, {
      statusCode: 200,
      headers: {
        "Access-Control-Allow-Origin" : "*"
      },
      body: JSON.stringify(result)
    }),
    err =&gt; callback(err)
  )
  </code></pre>
  
  <p>Maybe there's something wrong in the way I return the err in the callback?</p>