C# Asp Core 3.1 AWS Lambda Error Connection Timeout 30s

Hello!

Follow my problem:
Scenario:

I have a Lambda function on AWS that fires when an SQS message is placed in the queue.
This lambda function was developed using C # AspNet Core 3.1.
The MongoDb Driver used is version 2.11.6.
The problem occurs sporadically when inserting the record in MongoDb Server and I get a timeout for more than 30 seconds.

The error logged on the AWS CloudWatch is as follows:

A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }. Client view of cluster state is { ClusterId : “1”, ConnectionMode : “ReplicaSet”, Type : “ReplicaSet”, State : “Disconnected”, Servers : [{ ServerId: “{ ClusterId : 1, EndPoint : “Unspecified/cluster0-shard-00-00.dbzdi.mongodb.net:27017” }”, EndPoint: “Unspecified/cluster0-shard-00-00.dbzdi.mongodb.net:27017”, ReasonChanged: “Heartbeat”, State: “Disconnected”, ServerVersion: , TopologyVersion: , Type: “Unknown”, HeartbeatException: “MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server.
—> System.TimeoutException: Timed out connecting to 35.173.82.104:27017. Timeout was 00:00:30.
at MongoDB.Driver.Core.Connections.TcpStreamFactory.ConnectAsync(Socket socket, EndPoint endPoint, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Connections.TcpStreamFactory.CreateStreamAsync(EndPoint endPoint, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Connections.SslStreamFactory.CreateStreamAsync(EndPoint endPoint, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken cancellationToken)
— End of inner exception stack trace —
at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken cancellationToken)
at MongoDB.Driver.Core.Servers.ServerMonitor.InitializeConnectionAsync(CancellationToken cancellationToken)
at MongoDB.Driver.Core.Servers.ServerMonitor.HeartbeatAsync(CancellationToken cancellationToken)”, LastHeartbeatTimestamp: “2021-02-23T13:24:36.9134812Z”, LastUpdateTimestamp: “2021-02-23T13:24:36.9134815Z” }, { ServerId: “{ ClusterId : 1, EndPoint : “Unspecified/cluster0-shard-00-01.dbzdi.mongodb.net:27017” }”, EndPoint: “Unspecified/cluster0-shard-00-01.dbzdi.mongodb.net:27017”, ReasonChanged: “Heartbeat”, State: “Disconnected”, ServerVersion: , TopologyVersion: , Type: “Unknown”, HeartbeatException: “MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server.
—> System.TimeoutException: Timed out connecting to 54.205.128.107:27017. Timeout was 00:00:30.
at MongoDB.Driver.Core.Connections.TcpStreamFactory.ConnectAsync(Socket socket, EndPoint endPoint, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Connections.TcpStreamFactory.CreateStreamAsync(EndPoint endPoint, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Connections.SslStreamFactory.CreateStreamAsync(EndPoint endPoint, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken cancellationToken)
— End of inner exception stack trace —
at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken cancellationToken)
at MongoDB.Driver.Core.Servers.ServerMonitor.InitializeConnectionAsync(CancellationToken cancellationToken)
at MongoDB.Driver.Core.Servers.ServerMonitor.HeartbeatAsync(CancellationToken cancellationToken)”, LastHeartbeatTimestamp: “2021-02-23T13:24:36.9745216Z”, LastUpdateTimestamp: “2021-02-23T13:24:36.9745219Z” }, { ServerId: “{ ClusterId : 1, EndPoint : “Unspecified/cluster0-shard-00-02.dbzdi.mongodb.net:27017” }”, EndPoint: “Unspecified/cluster0-shard-00-02.dbzdi.mongodb.net:27017”, ReasonChanged: “Heartbeat”, State: “Disconnected”, ServerVersion: , TopologyVersion: , Type: “Unknown”, HeartbeatException: “MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server.
—> System.TimeoutException: Timed out connecting to 35.174.57.64:27017. Timeout was 00:00:30.
at MongoDB.Driver.Core.Connections.TcpStreamFactory.ConnectAsync(Socket socket, EndPoint endPoint, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Connections.TcpStreamFactory.CreateStreamAsync(EndPoint endPoint, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Connections.SslStreamFactory.CreateStreamAsync(EndPoint endPoint, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken cancellationToken)
— End of inner exception stack trace —
at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken cancellationToken)
at MongoDB.Driver.Core.Servers.ServerMonitor.InitializeConnectionAsync(CancellationToken cancellationToken)
at MongoDB.Driver.Core.Servers.ServerMonitor.HeartbeatAsync(CancellationToken cancellationToken)”, LastHeartbeatTimestamp: “2021-02-23T13:24:36.8938358Z”, LastUpdateTimestamp: “2021-02-23T13:24:36.8938361Z” }] }.: TimeoutException
at MongoDB.Driver.Core.Clusters.Cluster.ThrowTimeoutException(IServerSelector selector, ClusterDescription description)
at MongoDB.Driver.Core.Clusters.Cluster.WaitForDescriptionChangedHelper.HandleCompletedTask(Task completedTask)
at MongoDB.Driver.Core.Clusters.Cluster.WaitForDescriptionChangedAsync(IServerSelector selector, ClusterDescription description, Task descriptionChangedTask, TimeSpan timeout, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Clusters.Cluster.SelectServerAsync(IServerSelector selector, CancellationToken cancellationToken)
at MongoDB.Driver.MongoClient.AreSessionsSupportedAfterSeverSelctionAsync(CancellationToken cancellationToken)
at MongoDB.Driver.MongoClient.AreSessionsSupportedAsync(CancellationToken cancellationToken)
at MongoDB.Driver.MongoClient.StartImplicitSessionAsync(CancellationToken cancellationToken)
at MongoDB.Driver.MongoCollectionImpl1.UsingImplicitSessionAsync[TResult](Func2 funcAsync, CancellationToken cancellationToken)
at AWSLambda.Function.ProcessMessageAsync(SQSMessage message, ILambdaContext context) in C:\Repositorios\dtx\Gupy-Metricas-Lambda\app\Gupy.Metricas.AWSLambda\Function.cs:line 140
at AWSLambda.Function.FunctionHandler(SQSEvent evnt, ILambdaContext context) in C:\Repositorios\dtx\Gupy-Metricas-Lambda\app\Gupy.Metricas.AWSLambda\Function.cs:line 76

I don’t know what else to do, since I have already released access to my free tier cluster in Atlas MongoDb for any IP, that is, the problem does not seem to be access.

The volume of data (json) that I send is extremely small and the insertion should not take more than 2 seconds.

Could someone help me to solve this problem?

Oscar_Filho - We reached out to you over MongoDB Atlas’ in-app chat service so that we can help you with this issue. Could you kindly log into https://cloud.mongodb.com/ and look for the chat that we opened?

1 Like