首页 > 解决方案 > AWS Lambda 控制器路由不匹配,{proxy+} - 404 Not Found

问题描述

我有一个带有 GET 端点的 .NET core 3.1 项目。本地路由工作正常 - “/api” GET 请求返回 JSON 字符串。

在发布到我的 AWS Lambda 函数并通过 Postman 调用 lambda 后,我收到了对“/api”GET 请求的 404 Not Found 响应。完整 URL:“https://(lambda 域)/default/MyLambda2/api”

在我的项目的 Startup.cs ConfigureServices 和 Configure 方法中,我添加了“LambdaLogger.Log”语句。我的日志行显示在 CloudWatch 中(所以我肯定会到达应用程序)。就在这之后,路线失败了。

在 ConfigureServices 方法中,我有

services.AddControllers();

在配置方法中,我有

app.UseRouting();
app.UseEndpoints(endpoints =>
{
    endpoints.MapControllers();                
});

并在控制器文件中

namespace LambdaTest.Controllers
{    
    [Route("api")]
    public class ValuesController : ControllerBase
    {
        [HttpGet]
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }
    }
}

我为 lambda 定义了一个 API Gateway 触发器,其中

API endpoint: "https://(lambda domain)/default/MyLambda2/{proxy+}"
API type: REST
Authorization: NONE
Method: ANY

在资源选项卡中,我有...

/MyLambda2
ANY
 /{proxy+}
ANY
OPTIONS

...并且我已将所有资源部署到“默认”阶段(通过 Actions > Deploy API)。

这是默认自动生成的 serverless.template 文件...

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Transform": "AWS::Serverless-2016-10-31",
  "Description": "An AWS Serverless Application that uses the ASP.NET Core framework running in Amazon Lambda.",
  "Parameters": {},
  "Conditions": {},
  "Resources": {
    "AspNetCoreFunction": {
      "Type": "AWS::Serverless::Function",
      "Properties": {
        "Handler": "LambdaTest::LambdaTest.LambdaEntryPoint::FunctionHandlerAsync",
        "Runtime": "dotnetcore3.1",
        "CodeUri": "",
        "MemorySize": 256,
        "Timeout": 30,
        "Role": null,
        "Policies": [
          "AWSLambda_FullAccess"
        ],
        "Events": {
          "ProxyResource": {
            "Type": "Api",
            "Properties": {
              "Path": "/{proxy+}",
              "Method": "ANY"
            }
          },
          "RootResource": {
            "Type": "Api",
            "Properties": {
              "Path": "/",
              "Method": "ANY"
            }
          }
        }
      }
    }
  },
  "Outputs": {
    "ApiURL": {
      "Description": "API endpoint URL for Prod environment",
      "Value": {
        "Fn::Sub": "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/"
      }
    }
  }
}

什么可能导致路由没有在 lambda 中被拾取?

谢谢

标签: amazon-web-servicesaws-lambdaasp.net-core-3.1

解决方案


发现问题。我需要我在控制器上的路线是

[Route("MyLambda2/api")]

推荐阅读