简体   繁体   English

为什么我的.NET Core Web API(在AWS Lambda上部署)返回502 Bad Gateway错误?

[英]Why does my .NET Core Web API (deployed on AWS Lambda) return a 502 Bad Gateway error?

I am new to .NET Core, Entity Framework, AWS, and pretty much everything here (please comment and let me know if anything needs to be added to the post). 我是.NET Core,Entity Framework,AWS以及这里几乎所有内容的新手(请发表评论,并让我知道是否需要在帖子中添加任何内容)。

What I am trying to do is deploy a .NET Core Web API with AWS Lambda. 我要尝试的是使用AWS Lambda部署.NET Core Web API。 My API seems to be working locally, as I tested it (I have an Angular frontend app, with which I am able to successfully execute all the APIs). 经过测试,我的API似乎在本地运行(我有一个Angular前端应用程序,通过它我可以成功执行所有API)。 However, when I deploy with AWS and run a few of the APIs on Postman, I get a "502 Bad Gateway" error. 但是,当我使用AWS进行部署并在Postman上运行一些API时,会出现“ 502 Bad Gateway”错误。

My serverless.yml file looks like this: 我的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

My Model Class looks something like this: 我的模型类看起来像这样:

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;}
    }
}

My DbContext file looks like this: 我的DbContext文件如下所示:

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

        }

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

I have two methods in my Controller: 我的控制器中有两种方法:

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


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

The first of which works without a hitch (as expected). 其中第一个工作顺利(如预期)。 However, when I try to return a list of the Students that are in the DB, it seems that I'm encountering issues. 但是,当我尝试返回数据库中的学生列表时,似乎遇到了问题。

I tried using the second API locally as well, and received the appropriate response: 我也尝试在本地使用第二个API,并收到了相应的响应:

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

I also checked the AWS CloudWatch Logs, which showed something like this: 我还检查了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

This time out issue is not seen when I run the API locally, as I immediately get the correct response (as specified above). 当我在本地运行API时,没有看到此超时问题,因为我立即获得了正确的响应(如上所述)。


Update 更新资料

So I now have an RDS Database, which I can successfully connect to and play around with in SSMS. 因此,我现在有了一个RDS数据库,可以成功连接到该数据库并在SSMS中使用它。 Running APIs locally also still work. 在本地运行API仍然有效。 However, after I deploy it using serverless deploy -v , I'm still facing the previous issue. 但是,在使用serverless deploy -v部署它之后,我仍然面临上一个问题。 As suggested, I tried modifying the permissions for the DB, although I'm not sure I'm doing the right thing there either. 按照建议,我尝试修改数据库的权限,尽管我不确定自己在这里是否做对了。 What I've done is: 我所做的是:

  • Went into SSMS and modified the database permissions by right-clicking the server, going to Permissions, selecting my login (that I set up while configuring the RDS), and granted the "Alter any database" option. 进入SSMS并通过右键单击服务器,转到“权限”,选择我的登录名(在配置RDS时设置),并授予“更改任何数据库”选项,来修改数据库权限。 I then went to the database itself and modified some permissions (Delete, Insert and Update). 然后,我进入数据库本身,并修改了一些权限(删除,插入和更新)。
  • I also enabled the Windows Management Instrumentation (WMI-In) Windows Firewall rule. 我还启用了Windows管理规范(WMI-In)Windows防火墙规则。

Neither of these seems to have solved my problem. 这些似乎都没有解决我的问题。

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. 您可能使用了与本地Db中用于测试的连接字符串相同的字符串,因此应将其更改为使用api可通过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. 如果是这种情况,那么您的api会尝试访问本地数据库(显然不会找到),这将解释超时问题。

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. 为了对其进行修复,您可能还应该将数据库部署到AWS上,并将项目的连接字符串更改为AWS内部该数据库的连接字符串。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从另一个 locahost.Net Core API 调用 locahost.Net Core API 时出现错误网关(502)错误 - Bad Gateway (502) error while calling locahost .Net Core API from another locahost .Net Core API ASP.NET Core 2.0 ngrok 502 错误网关错误 - ASP.NET Core 2.0 ngrok 502 Bad Gateway Error 从 Azure 中托管的 Asp.net Core MVC Web App 获取“响应状态代码并不表示成功:502(坏网关)” - Getting "Response status code does not indicate success: 502 (Bad Gateway)" from Asp.net Core MVC Web App hosted in Azure 为什么会发生“(502) Bad Gateway”错误? - Why "(502) Bad Gateway" error happen? Return File from AWS ApiGateway ASP.NET Core 3 Web API running as AWS Lambda function - Return File from AWS ApiGateway ASP.NET Core 3 Web API running as AWS Lambda function 将 asp.net core web api 部署到 aws elastic beanstalk 时出现错误 404 Not Found - Getting Error 404 Not Found when deployed asp.net core web api to aws elastic beanstalk 错误(502)错误的网关:当尝试使用WebChat通道访问Azure上部署的聊天机器人时 - Error (502) Bad Gateway: When trying to access chatbot deployed on Azure using WebChat channel .net core aws Lambda没有错误,但Lambda暂停 - .net core aws Lambda No error, but Lambda halts 502 错误网关 nginx/1.14.1 elasticbeanstalk .net 核心应用程序在 docker 上运行 - 502 Bad Gateway nginx/1.14.1 elasticbeanstalk .net core app running on docker .net core HTTPS 请求返回 502 bad gateway 而 Postman 返回 200 OK - .net core HTTPS requests returns 502 bad gateway while Postman returns 200 OK
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM