【发布时间】:2020-08-30 20:08:27
【问题描述】:
如何配置 cors 以在本地运行 lambda 函数
我尝试了不同的方法,但无法得到正确的解决方案。
下面是我的template.yml
Resources:
MyApi:
Type: AWS::Serverless::Api
Properties:
StageName: dev
Cors:
AllowHeaders: "'Content-Type,Authorization,Origin,X-Requested-With,Accept,x-id-token,x-custom-header'"
AllowOrigin: "'*'"
AllowMethods: "'OPTIONS,POST,GET,PUT,DELETE'"
AllowCredentials: "'*'"
Auth:
AddDefaultAuthorizerToCorsPreflight: False
DefaultAuthorizer: MyLambdaTokenAuthorizer
Authorizers:
MyLambdaTokenAuthorizer:
FunctionArn: !GetAtt MyAuthFunction.Arn
HelloFunction:
Type: AWS::Serverless::Function
Properties:
Handler: functions/hello/handler.hello
CodeUri: "."
Events:
HelloAPI:
Type: Api
Properties:
Path: /hello
Method: GET
RestApiId: !Ref MyApi
这是我的处理函数
exports.hello = async (event) => {
const response = {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*", // Required for CORS support to work
"Content-Type": "application/json",
"Access-Control-Allow-Credentials": true, // Required for cookies, authorization headers with HTTPS
},
body: JSON.stringify({ message: "Hello World!" }),
};
callback(null, response);
};
还是报错:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
【问题讨论】:
标签: aws-lambda serverless aws-sam