[英]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时,没有看到此超时问题,因为我立即获得了正确的响应(如上所述)。
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: 我所做的是:
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.