首页 > 解决方案 > 为什么我的 .NET Core Web API(部署在 AWS Lambda 上)返回 502 Bad Gateway 错误?

问题描述

我是 .NET Core、Entity Framework、AWS 以及这里的几乎所有内容的新手(请发表评论并让我知道是否需要在帖子中添加任何内容)。

我想要做的是使用 AWS Lambda 部署一个 .NET Core Web API。我的 API 似乎在本地工作,因为我对其进行了测试(我有一个 Angular 前端应用程序,通过它我能够成功执行所有 API)。但是,当我使用 AWS 部署并在 Postman 上运行一些 API 时,我收到“502 Bad Gateway”错误。

我的serverless.yml文件如下所示:

service: angularawardwebapi

provider: 
  name: aws
  runtime: dotnetcore2.1
  region: us-east-1

package:
  artifact: bin/release/netcoreapp2.1/deploy-package.zip

functions:
  api:
    handler: angularawardwebapi::angularawardwebapi.LambdaEntryPoint::FunctionHandlerAsync
    events: 
      - http:
          path: /{proxy+}
          method: ANY

我的模型类看起来像这样:

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace angularawardwebapi.Models {
    public class Student {
        [Key]
        public int StudentId{get;set;}

        [Required]
        [Column(TypeName="nvarchar(50)")]
        public string StudentFirstName{get;set;}

        [Required]
        [Column(TypeName="varchar(50)")]
        public string StudentLastName{get;set;}
    }
}

我的 DbContext 文件如下所示:

public class DatabaseContext:DbContext {
        public DatabaseContext(DbContextOptions<DatabaseContext> options):base(options) {

        }

        public DbSet<Student> Students {get;set;} 
   }
}

我的控制器中有两种方法:

        [HttpGet("temp")]
        public ActionResult<List<String>> GetTemp() {
            return Ok("here");
        } 


        [HttpGet]
        public ActionResult<IEnumerable<Student>> GetStudent()
        {
            return Ok(_context.Students);
        }

其中第一个工作顺利(如预期的那样)。但是,当我尝试返回数据库中的学生列表时,似乎遇到了问题。

我也尝试在本地使用第二个 API,并收到了适当的响应:

[
    {
        "StudentId": 1,
        "StudentFirstName": "John",
        "StudentLastName": "Doe"
    }
]

我还检查了 AWS CloudWatch Logs,显示如下:

[Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request starting GET https://[myurl]/dev/api/Student/ 
[Information] Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker: Route matched with {action = "GetStudent", controller = "Student"}. Executing action WebApi.Controllers.StudentController.GetStudent ([myprojectname]) 
[Information] Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker: Executing action method WebApi.Controllers.StudentController.GetStudent ([myprojectname]) - Validation state: Valid 
[Information] Microsoft.EntityFrameworkCore.Infrastructure: Entity Framework Core 2.1.8-servicing-32085 initialized 'DatabaseContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: None 
END RequestId: f57417d3-4075-4680-a78d-e90e2c710760
REPORT RequestId: f57417d3-4075-4680-a78d-e90e2c710760  Duration: 6006.80 ms    Billed Duration: 6000 ms Memory Size: 1024 MB   Max Memory Used: 141 MB 
2019-06-06T14:19:41.116Z f57417d3-4075-4680-a78d-e90e2c710760 Task timed out after 6.01 seconds

当我在本地运行 API 时,不会出现这个超时问题,因为我会立即得到正确的响应(如上所述)。


更新

所以我现在有了一个 RDS 数据库,我可以成功地连接到它并在 SSMS 中使用它。在本地运行 API 也仍然有效。但是,在我使用 部署它之后serverless deploy -v,我仍然面临上一个问题。正如建议的那样,我尝试修改数据库的权限,尽管我不确定我在那里做的事情是否正确。我所做的是:

这些似乎都没有解决我的问题。

标签: c#entity-frameworkaws-lambdaasp.net-core-webapirds

解决方案


You're probably using the same connection string you used for testing in your local Db, you should change it to use some database that the api can access to from aws. If that's the case then your api is trying to access to a local db (which obviously it's not going to find), that would explain the timeout issue.

In order to fix it you should probably deploy your database to AWS as well and change the connection string of the project to the connection string of that database inside of AWS.


推荐阅读