Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm trying to deploy a netcoreapp2.0 application to AWS Lambda that connects to Postgres on RDS. The following code fails:

    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

I get the following stack trace:

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

Does anyone know why this might fail with deployed to AWS lambda, but work locally on my machine?

Here are some GitHub issues that may provide additional context:

  • https://github.com/aws/aws-lambda-dotnet/issues/288
  • https://github.com/npgsql/npgsql/issues/1984

Some additional details: - DB Engine: PostgreSQL 9.6.6 - Npgsql version: 4.0 - .NET version: netcoreapp2.0

Please let me know if I can provide any additional information. Thank you!

like image 363
Nathaniel Elkins Avatar asked Jun 07 '18 14:06

Nathaniel Elkins


People also ask

Can't connect to server connection timed out lambda?

Make sure that you confirm that there's a valid connection before attempting to use the connection. If there's not a valid connection, then create a new connection before continuing. As a test, launch an Amazon Elastic Compute Cloud (Amazon EC2) instance with the same Amazon VPC configuration as your Lambda function.

What happens when AWS Lambda timeout?

When the specified timeout is reached, AWS Lambda terminates execution of your Lambda function. As a best practice, you should set the timeout value based on your expected execution time to prevent your function from running longer than intended. This feature is available in all regions where AWS Lambda is available.


1 Answers

A connection timeout usually indicates a firewall problem.

Your connection request is blocked by the security group of your RDS instance.

When you've created the instance, the IP of the computer running the AWS console is automatically whitelisted.

Any other external or internal resource accessing PostgreSQL must be whitelisted as well.

I hope you'll enjoy AWS RDS running PostgreSQL, I find it awesome.

like image 135
Olivier MATROT Avatar answered Oct 25 '22 20:10

Olivier MATROT