I have a sam file trying to build an API Gateway to a lambda function. I am following the latest AWS documentation for configuring cors. This documentation is quoted here:
As you may have guessed, that isn’t working for me. If I use "http://localhost:3000" or a variation of that as the "AllowOrigin" property, then I get the following error:
Access to XMLHttpRequest at ‘xxx’ from origin ‘http://localhost:3000’ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.
Checking the API gateway, that property is indeed present. Although it seems to be only present for the options and not for the "ANY" section.
If I change that to be AllowOrigin: "’*’", I get the following error:
Access to XMLHttpRequest at ‘xxxx’ from origin ‘http://localhost:3000’ has been blocked by CORS policy: The ‘Access-Control-Allow-Origin’ header contains multiple values ‘*, *’, but only one is allowed.
In addition to this, there is a separate "Stage" stage created which I don’t understand and I am not sure that the proxy needs to be there or where it is created in the SAM file.
Here is my yaml file for sam.
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: Storygraf backend API
Globals:
Function:
Timeout: 3
Resources:
ExpressApi:
Type: AWS::Serverless::Api
Properties:
StageName: dev
Cors:
AllowOrigin: "'*'"
AllowMethods: "'POST, GET, PUT, DELETE'"
AllowHeaders: "'X-Forwarded-For, Content-Type'"
ExpressLambdaFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./
Handler: lambda.handler
MemorySize: 512
Runtime: nodejs14.x
Timeout: 30
Events:
ProxyApiRoot:
Type: Api
Properties:
RestApiId: !Ref ExpressApi
Path: /
Method: ANY
ProxyApiGreedy:
Type: Api
Properties:
RestApiId: !Ref ExpressApi
Path: /{proxy+}
Method: ANY
Update
It looks like the test works when going through the proxy, but nothing is available when using the non proxy GET command.
Update
I discovered that the package process is changing the quotes, but this doesn’t seem to be the problem.
2
Answers
From your logs your
access-control-allow-origin
header returned from yourGET
test returns an array with['*','*']
. It should not be an array and should just be'*'
.Edit
This is the template I use, which has a few more things defined. Also I’ve got an API key there, which you can easily remove.
Make sure in your lambda response your headers match those defined in the cloud formation OPTIONS response, for the template I’ve given the response from the lambdaHandler function may look like this (if using js):
Hope this is somewhat helpful.