Resolving Cors Issues

Hullo!

I’m trying to get on board with the new Serverless 1.0, and I’m having a bit of an issue with CORS.
I suspect there’s a tiny thing that I’ve missed in my setup.

To test, I’ve made a function called echo that returns the context and event data for a GET request.
My function is the following:

module.exports.echo = (event, context, cb) => {
  context.succeed({
      statusCode: 200,
      headers: {
        "Access-Control-Allow-Origin": "*"
      }
    });
  cb(null, {context, event});
};

My yaml file includes the following:

  echo:
    handler: handler.echo
    events:
      - http:
          path: echo
          method: get
          integration: lambda
          cors:
            origins:
              - '*'

When access by URL I am returned the expected echo.
When I to get the resource by ajax, there is a CORS error:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at 
https://??????.execute-api.us-east-1.amazonaws.com/dev/echo?{%22userId%22:%22dummyID%22}&_=??????. 
(Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

What should I do to fix this?

Can you try removing the succeed call and only calling the callback with the statuscode/headers hash.

The context.* methods are outdated for newer versions of Node: http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html

If you want to return context and event in the body you can do (please check for typos):

module.exports.echo = (event, context, cb) => {
  cb({
      statusCode: 200,
      headers: {
        "Access-Control-Allow-Origin": "*"
      },
      body: JSON.stringify({context, event})
    });
};

If that doesn’t work please post the details of the OPTIONS and Get request so we can look in more detail.

Hey @flomotlik thanks for the reply!
Things are now working :slight_smile: