我在node.js中开发了lambda函数,并尝试使用SAM模板部署到带有代理集成的api网关。我的lambda函数正在返回AWS文档中提到的头
const response = {
statusCode: 200,
headers: {
"Access-Control-Allow-Headers" : "Content-Type",
"Access-Control-Allow-Origin": "https://www.example.com",
"Access-Control-Allow-Methods": "OPTIONS,POST,GET"
},
body: JSON.stringify('Hello from Lambda!'),
};
template.yaml代码
GetClientFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: functions/GetClient/
Handler: index.handler
Runtime: nodejs14.x
Events:
GetClientCustomFieldMap:
Type: Api
Properties:
RestApiId: !Ref ClientApi # this is api with authorizer configured
Path: api/Client/GetClient/{name}
Method: get
它正在创建路径中提到的所有资源,如"api"、"Client"、"GetClient",但不使用Options方法,也会出现cors错误。尝试了stackoverflow的多种方法,但无法解决。也关注这个url如何使用AWS SAM 启用CORS
您需要为OPTIONS方法配置Api资源的Cors,请参阅https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-api.html#sam-api cors