我正在使用 AWS Code star 并使用 Express Web 服务模板。代码星创建 Lambda 并创建 API Gateway API。该api有效,它返回我想要的。我正在尝试通过 API 网关启用 CORS,
API Gateway -> API -> 资源 -> 操作 -> 启用 CORS。
由于未为代理生成集成响应,因此在选项上设置 CORS 失败。这是图像。
我想我必须通过 YAML 配置它来生成选项和 GET 的集成响应。
这是我的YAML
AWSTemplateFormatVersion: 2010-09-09
Transform:
- AWS::Serverless-2016-10-31
- AWS::CodeStar
Parameters:
ProjectId:
Type: String
Description: AWS CodeStar projectID used to associate new resources to team members
Resources:
HelloWorld:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: nodejs4.3
Role:
Fn::ImportValue:
!Join ['-', [!Ref 'ProjectId', !Ref 'AWS::Region', 'LambdaTrustRole']]
Events:
GetEvent:
Type: Api
Properties:
Path: /
Method: get
GetById:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: nodejs4.3
Role:
Fn::ImportValue:
!Join ['-', [!Ref 'ProjectId', !Ref 'AWS::Region', 'LambdaTrustRole']]
Events:
GetEvent:
Type: Api
Properties:
Path: /api/getById
Method: get
这是我的 Lambda 处理程序。
'use strict';
const awsServerlessExpress = require('aws-serverless-express')
const app = require('./app')
const server = awsServerlessExpress.createServer(app)
exports.handler = (event, context) => awsServerlessExpress.proxy(server, event, context);
如果有人可以帮助我如何调整 CloudFormation 模板,我将不胜感激。