You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS CDK中LambdaRestApi的CORS配置问题求助

AWS CDK API Gateway CORS 问题

我有一个以Lambda为后端的AWS CDK API Gateway项目,主域名mydomain.com指向CloudFront分发,子域名api.mydomain.com指向我的REST API。在浏览器中调用POST api.mydomain.com/api/sendEmail时出现CORS问题,尝试多种配置均未解决。

已尝试的解决方案

  • 为资源添加响应方法:
const api = new apigw.LambdaRestApi(this, 'APIGateway', {...});
const items = api.root.addResource('sendEmail');
    items.addMethod('POST', new apigw.LambdaIntegration(mailerLambdaFunction), {
      methodResponses: [{
        statusCode: '200',
        responseParameters: {
          "method.response.header.Access-Control-Allow-Headers": true,
          "method.response.header.Access-Control-Allow-Methods": true,
          "method.response.header.Access-Control-Allow-Credentials": true,
          "method.response.header.Access-Control-Allow-Origin": true,
        }
      }]
    })
  • 在Lambda函数中返回预定义响应:
{
    body: JSON.generate(message: 'Thank you for reaching out! I\'ll contact you as soon as I can'),
    statusCode: 200,
    headers: {
      'Content-Type': 'application/json',
      'Access-Control-Allow-Headers': 'Content-Type',
      'Access-Control-Allow-Origin': '*',
      'Access-Control-Allow-Methods': 'OPTIONS,POST,GET'
    },
    isBase64Encoded: false
  }
  • 添加defaultCorsPreflightOptions配置(参考Stack Overflow方案),但无效。

更新信息

手动点击Enable CORS时,无法为POST方法添加Access-Control-Allow-Origin方法响应头(已在代码中添加),且集成响应选项为灰色不可用。

内容的提问来源于stack exchange,提问作者jedi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 09:00:40