Npgsql在AWS Lambda上与netcoreapp2.0无法连接(超时)- 我该如何解决?

5

我正在尝试将一个连接到RDS上的Postgres数据库的netcoreapp2.0应用程序部署到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

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


1
这可能与 Npgsql 没有任何关系。我想你忘记在 RDS 实例安全组中指定入站规则了吧? - anon
@OlivierMATROT 谢谢您的参与。 RDS 实例可公开访问,我可以使用 Datagrip 从我的计算机无问题地访问它。 那不是表示它能够接收入站连接吗? - Nathaniel Elkins
2
这要看情况。你的RDS实例的安全组入站规则是什么?很可能你自己的IP已经被加入了白名单。你还需要允许与Lambda相关联的安全组。 - anon
1
天啊,我想你是对的。我的IP已经被加入白名单了!我猜那是自动设置的。我刚刚在列表中添加了另一个入站IP规则,我们会看看它是否能解决问题。 - Nathaniel Elkins
1
是的,当您启动实例时,它会自动设置。 - anon
@OlivierMATROT,问题已解决,万岁!请随意在下面留下答案,我会将其标记为已解决的问题。 - Nathaniel Elkins
1个回答

10

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

您的连接请求被您的RDS实例的安全组阻止。

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

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

我希望您会喜欢在AWS RDS上运行PostgreSQL,我认为它非常棒。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接