AWS Lambda 上带有 netcoreapp2.0 的 Npgsql 无法连接(超时)——我该如何解决?

Npgsql with netcoreapp2.0 on AWS Lambda is unable to connect (has timeout) - how can I resolve?

我正在尝试将 netcoreapp2.0 应用程序部署到连接到 RDS 上的 Postgres 的 AWS Lambda。以下代码失败:

    let testConn = "Host=hostNameHere;Username=userNameHere;Password=passwordHere;Database=postgres";
    let conn = new NpgsqlConnection(testConn)
    try
        printfn "Trying to open a connection"
        conn.Open()
    with ex ->
        printfn "Exception trying to open conn:\n%O" ex

我得到以下堆栈跟踪:

System.TimeoutException: The operation has timed out.
at Npgsql.NpgsqlConnector.Connect(NpgsqlTimeout timeout)
at Npgsql.NpgsqlConnector.<RawOpen>d__153.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Npgsql.NpgsqlConnector.<Open>d__149.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Npgsql.ConnectorPool.<AllocateLong>d__19.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Npgsql.NpgsqlConnection.<>c__DisplayClass32_0.<<Open>g__OpenLong|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Npgsql.NpgsqlConnection.Open()
at Masse.Common.SQL.query[T](String connectString, SqlQuery q) in /home/nat/Projects/shopmasse-backend/fsharp/src/Masse.Common/Sql.fs:line 40

有谁知道为什么部署到 AWS lambda 可能会失败,但可以在我的机器上本地运行?

这里有一些 GitHub 问题可以提供额外的上下文:

一些额外的细节: - 数据库引擎:PostgreSQL 9.6.6 - Npgsql 版本:4.0 - .NET版本:netcoreapp2.0

如果我可以提供任何其他信息,请告诉我。谢谢!

连接超时通常表示防火墙有问题。

您的连接请求被您的RDS实例的安全组拦截了。

创建实例后,计算机的 IP 运行 AWS 控制台会自动列入白名单。

访问 PostgreSQL 的任何其他外部或内部资源也必须列入白名单。

希望您会喜欢 AWS RDS 运行 PostgreSQL,我觉得它很棒。