aws-lambda-dotnet: Can not connect to MongoDB Atlas

I wrote a simple AWS Serverless ASP Net Core 2.0 WebAPI This connects to a MongoDB Atlas Database. But it is not working. It is giving timeout. I test locally and it Works. I publish to Azure it Works.

I suspect this is being caused by AWS CloudFormation firewall .

When I publish to AWS Serverless , it gives error:

502 Bad Gateway

{ “message”: “Internal server error” }

I checked the logs,

Unhandled Exception: System.TimeoutException: 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-3lmjx.mongodb.net:27017" }", EndPoint: "Unspecified/cluster0-shard-00-00-3lmjq.mongodb.net:27017", State: "Disconnected", Type: "Unknown" }, { ServerId: "{ ClusterId : 1, EndPoint : "Unspecified/cluster0-shard-00-01-3lmjx.mongodb.net:27017" }", EndPoint: "Unspecified/cluster0-shard-00-01-3lmjx.mongodb.net:27017", State: "Disconnected", Type: "Unknown" }, { ServerId: "{ ClusterId : 1, EndPoint : "Unspecified/cluster0-shard-00-02-3lmjq.mongodb.net:27017" }", EndPoint: "Unspecified/cluster0-shard-00-02-3lmjq.mongodb.net:27017", State: "Disconnected", Type: "Unknown" }] }.

How to debug and solve this?

My serveless.template

{
  "AWSTemplateFormatVersion" : "2010-09-09",
  "Transform" : "AWS::Serverless-2016-10-31",
  "Description" : "An AWS Serverless Application that uses the ASP.NET Core framework running in Amazon Lambda.",

  "Parameters" : {
    "ShouldCreateBucket" : {
      "Type" : "String",        
      "AllowedValues" : ["true", "false"],
      "Description" : "If true then the S3 bucket that will be proxied will be created with the CloudFormation stack."
    },  
    "BucketName" : {
        "Type" : "String",
        "Description" : "Name of S3 bucket that will be proxied. If left blank a new table will be created.",
        "MinLength" : "0"
    }
  },

  "Conditions" : {
    "CreateS3Bucket" : {"Fn::Equals" : [{"Ref" : "ShouldCreateBucket"}, "true"]},
    "BucketNameGenerated" : {"Fn::Equals" : [{"Ref" : "BucketName"}, ""]}
  },

  "Resources" : {

    "ProxyFunction" : {
      "Type" : "AWS::Serverless::Function",
      "Properties": {
        "Handler": "AWSServerless1::AWSServerless1.LambdaEntryPoint::FunctionHandlerAsync",
        "Runtime": "dotnetcore2.0",
        "CodeUri": "",
        "MemorySize": 256,
        "Timeout": 30,
        "Role": null,
        "Policies": [ "AWSLambdaFullAccess" ],
        "Environment" : {
          "Variables" : {
            "AppS3Bucket" : { "Fn::If" : ["CreateS3Bucket", {"Ref":"Bucket"}, { "Ref" : "BucketName" } ] }
          }
        },
        "Events": {
          "PutResource": {
            "Type": "Api",
            "Properties": {
              "Path": "/{proxy+}",
              "Method": "ANY"
            }
          }
        }
      }
    },

    "Bucket" : {
        "Type" : "AWS::S3::Bucket",
        "Condition" : "CreateS3Bucket",
        "Properties" : {
            "BucketName" : { "Fn::If" : ["BucketNameGenerated", {"Ref" : "AWS::NoValue" }, { "Ref" : "BucketName" } ] }
        }
    }
  },

  "Outputs" : {
    "S3ProxyBucket" : {
        "Value" : { "Fn::If" : ["CreateS3Bucket", {"Ref":"Bucket"}, { "Ref" : "BucketName" } ] }
    }
  }
}

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 24 (2 by maintainers)

Most upvoted comments

Same here. Trying to run a lambda in AWS to connect via public IP to the free tier. Doesn’t work. Looks like lambda is running my function on linux.

Was there ever a solution @normj @jaydestro ? I’m running into this issue right now.

I was getting this error.

image

I realised the issue was because of no outbound traffic from lambda.

I modified it to be in the VPC that allowed outbound traffic and the connection to Mongo started working.

Any resolution? Same thing here – Azure works fine, but AWS returns:

502 Bad Gateway { "message": "Internal server error" }

And this particular api method does not use the database, so that’s not an issue.

Ditto here. Everything was working great on Atlas’ Azure but when we migrated to Atlas’ AWS this past weekend we are seeing this same issue happening.

Yeah I’m getting this issue too. A fix would be nice 😃

I noticed in the Mongo DB docs that it says for the C# driver on .NET Core it can’t connect to the free tier and a couple other cluster configurations in Linux. Lambda is a Linux environment so could that be the problem?

image

@TonyHenrique I confirm it looks like a connection string issue. Where did you configure it in your code and more importantly, which value did you assign to it? (you should mask the auth credentials in that connection string if you plan to paste it here).

The "Unspecified/cluster0-shard-00-00-3lmjx.mongodb.net:27017" value is the red flag here so we’d have to know where your Lambda function is retrieving this info from. I’d suggest you ping me directly on Slack after you join our Community Slack Network at https://community-slack.mongodb.com