Skip to content

Document dealing with CORS #252

@mnapoli

Description

@mnapoli

I am opening this issue so that we document this information.

When building an API on Lambda (+ API Gateway) we often need to enable CORS so that it can be called from the browser (e.g. with a SPA).

There is a very simple option in SAM (in template.yaml) to do that:

Globals:
    Api:
        Cors: "'*'"

However it doesn't work with sam local yet! (it works when deployed though)

The issue to track that in the SAM repository: aws/aws-sam-cli#323 There is a pull request as well: aws/aws-sam-cli#1009

Workaround

A workaround is to deal with CORS in the PHP API.

Here is an example for a Slim application:

composer require eko3alpha/slim-cors-middleware
$app = new \Slim\App();
$app->add(new \Eko3alpha\Slim\Middleware\CorsMiddleware([
    '*' => ['GET', 'POST'],
]));

// ...

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions