Resolving Cors Issues


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) => {
      statusCode: 200,
      headers: {
        "Access-Control-Allow-Origin": "*"
  cb(null, {context, event});

My yaml file includes the following:

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

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 
(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:

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

module.exports.echo = (event, context, 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: