Skip to content

allow origin isn't sufficient to resolve CORS issue #1460

@kimyu-ng

Description

@kimyu-ng

Description

Adding allow origin and allow headers at template.yaml and returning Access-Control-Allow-Origin in the lambda response body seems insufficient to resolve cross origin issue after deploying the lambda service.

Steps to reproduce

sample of template.yaml

Globals:
  Function:
    Timeout: 60
  Api:
    Cors:
      AllowMethods: "'POST,OPTIONS'"
      AllowHeaders: "'*'"
      AllowOrigin: "'*'"
      AllowCredentials: "'*'"

Resources:
  EndpointFunction:
    Type: AWS::Serverless::Function
      CodeUri: api/v1/
      Handler: endpoint.lambda_handler
      Runtime: ruby2.5
      Events:
        Endpoint:
          Type: Api
          Properties:
            Path: /api/v1/endpoint
            Method: post

sample of endpoint.rb

def lambda_handler(event:, context:)
  {
    statusCode: 201,
    headers: {
      'Access-Control-Allow-Origin' => '*',
      'Content-Type' => 'application/json'
    }
  }
end

Observed result

Access to XMLHttpRequest at 'https://<random-sha>.execute-api.<aws-region>amazonaws.com/prod/api/v1/endpoint' from origin 'https://<domain>' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Update:
This turned out to be a url of lambda service is being case sensitive. Aside from that, there is currently no way to set multiple domains for allow origin. Also, the hassle way of defining an option function doesn't seem to work either at least local #1434

Expected result

able to make request from the browser

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

  1. sam --version: 0.22.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions