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

使用AWS CDK配置API Gateway与Lambda集成时遭遇CORS跨域问题求助

AWS CDK配置API Gateway与Lambda集成时遭遇CORS跨域问题求助

我正在尝试通过AWS CDK配置API Gateway,目前已经完成了基础搭建,但无论怎么调整都无法解决CORS跨域问题,导致前端无法调用读取数据库的Lambda函数。我使用的是C#开发,以下是我的代码:

using Constructs;
using Amazon.CDK.AWS.APIGateway;
using Amazon.CDK.AWS.Lambda;
using System.Collections.Generic;

/**
 * This stack creates an API Gateway REST API.
 * The API has a single resource, /items, which supports GET, POST, and PUT methods.
 * Each method is integrated with a Lambda function.
 */

namespace Cdk
{
    public class ApiGatewayStack
    {
        // Need to use RestApiBase instead of RestApi for v1
        public RestApiBase Api { get; }

        public ApiGatewayStack(Construct scope, Function lambdaFunction)
        {
            Api = new RestApi(scope, "ServerlessProjApi", new RestApiProps
            {
                DefaultCorsPreflightOptions = new CorsOptions
                {
                    AllowOrigins = Cors.ALL_ORIGINS,           // Allow all origins
                    AllowMethods = Cors.ALL_METHODS,           // Allow all methods (GET, POST, PUT, etc.)
                    AllowHeaders = new[] { "Content-Type", "X-Amz-Date", "Authorization", "X-Api-Key" } // CORS headers
                }
            });

            SetupApiGateway(Api, lambdaFunction);
        }

        private void SetupApiGateway(RestApiBase api, Function lambdaFunction)
        {
            var lambdaIntegration = new LambdaIntegration(lambdaFunction);
            var options = new MethodOptions
            {
                AuthorizationType = AuthorizationType.NONE,
                MethodResponses = new[] {
                    new MethodResponse
                    {
                        StatusCode = "200",
                        ResponseParameters = new Dictionary<string, bool>
                        {
                            { "method.response.header.Access-Control-Allow-Origin", true },
                            { "method.response.header.Access-Control-Allow-Headers", true },
                            { "method.response.header.Access-Control-Allow-Methods", true }
                        }
                    }
                }
            };

            // Create /items resource
            var items = api.Root.AddResource("items");

            // Add methods for /items resource (GET, POST, PUT)
            items.AddMethod("GET", lambdaIntegration, options);  // GET /items
            items.AddMethod("POST", lambdaIntegration, options); // POST /items
            items.AddMethod("PUT", lambdaIntegration, options);  // PUT /items
        }
    }
}

我一开始用的是RestApi类,后来尝试改成RestApiBase,但问题依然没有解决。我可以直接在浏览器输入API端点访问成功,但通过网页调用就会触发CORS错误,具体错误信息如下:

Access to XMLHttpRequest at 'https://{apicode}.execute-api.us-east-1.amazonaws.com/prod/items' from origin 'http://{my-s3-bucket}.s3-website-us-east-1.amazonaws.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

希望有人能帮忙排查并解决这个问题,非常感谢!

备注:内容来源于stack exchange,提问作者Seth Brock

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.14 11:59:33