【问题标题】:Cannot connect to EC2 PostgreSQL database from ASP.Net Core lambda function无法从 ASP.Net Core lambda 函数连接到 EC2 PostgreSQL 数据库
【发布时间】:2019-07-26 18:32:20
【问题描述】:

我是 AWS 新手,我的问题是关于与 AWS 中作为 EC2 实例托管的 Postgresql 数据库的连接。

我有一个作为 AWS 无服务器应用程序发布的 Asp.net 核心 Web api 和一个连接到上述数据库的端点。在本地主机上运行 api 或从任何数据库客户端连接到数据库时一切正常,但在 AWS 上测试时它不起作用。我假设它连接到 EC2 的安全配置,但不知道如何解决。

这是 serverless.template 代码

{
  "AWSTemplateFormatVersion" : "2010-09-09",
  "Transform" : "AWS::Serverless-2016-10-31",
  "Description" : "Starting template for an AWS Serverless Application.",
  "Parameters" : {
  },
  "Resources" : {
    "DefaultFunction" : {
      "Type" : "AWS::Serverless::Function",
      "Properties": {
        "Handler": "AwsApp.WebApi::AwsApp.WebApi.LambdaEntryPoint::FunctionHandlerAsync",
        "Runtime": "dotnetcore2.1",
        "CodeUri": "",
        "Description": "Default function",
        "MemorySize": 256,
        "Timeout": 30,
        "Role": null,
        "Policies": [ "AWSLambdaFullAccess" ],
        "Events": {
          "ProxyResource": {
            "Type": "Api",
            "Properties": {
              "Path": "/{proxy+}",
              "Method": "ANY"
            }
          }
        }
      }
    }
  },
  "Outputs" : {
    "ApiURL" : {
        "Description" : "API endpoint URL for Prod environment",
        "Value" : { "Fn::Sub" : "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/" }
    }
  }
}

这是错误信息

An exception has been raised that is likely due to a transient failure.
   at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.MoveNext()
   at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable`1 source, Boolean& found)
   at lambda_method(Closure )
   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ResultEnumerable`1.GetEnumerator()
   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider._TrackEntities[TOut,TIn](IEnumerable`1 results, QueryContext queryContext, IList`1 entityTrackingInfos, IList`1 entityAccessors)+MoveNext()
   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
   at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable`1 source, Boolean& found)
   at System.Linq.Enumerable.First[TSource](IEnumerable`1 source)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass15_1`1.<CompileQueryCore>b__0(QueryContext qc)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
   at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source, Expression`1 predicate)
   at Microsoft.EntityFrameworkCore.Internal.EntityFinder`1.Find(Object[] keyValues)
   at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.Find(Object[] keyValues)
   at AwsApp.WebApi.Controllers.ValuesController.Get(Int32 id) in D:\TEST\.NET Core\AwsApp\AwsApp.WebApi\Controllers\ValuesController.cs:line 33

另外奇怪的是为什么错误消息中有文件路径

编辑

PostgreSQL 在 VPC 下运行。 控制器中有一个虚拟的 Get 方法,它返回一个简单的数组:

[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
   return new string[] { "value1", "value2" };
}

当我设置我的 lambda 函数 VPC 时,此函数也会失败并出现错误 500。

解决方案

我能够修复它。 分配给我的 lambda 函数的角色没有必需的策略(请参阅 here)。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "ec2:CreateNetworkInterface",
                "ec2:DescribeNetworkInterfaces",
                "ec2:DeleteNetworkInterface"
            ],
            "Resource": "*"
        }
    ]
}

【问题讨论】:

    标签: postgresql asp.net-core aws-lambda


    【解决方案1】:

    约翰,

    可能有多个问题....

    1. 如果您的 PostgreSQL 在配置了私有子网的 VPC 中运行,请在防火墙中打开端口(入站流量),以便您的 lambda 可以连接。

    2. 您需要创建一个具有 PostgreSQL 权限的新角色并将其分配给 Lambda。

    请尝试发布您的 cmets。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-16
      • 2017-11-27
      • 1970-01-01
      • 2014-03-15
      • 2014-03-30
      • 2013-04-19
      • 1970-01-01
      • 2022-12-03
      相关资源
      最近更新 更多