AWS Lambda unable to connect to Atlas Serverless

The error I receive :

  "errorMessage": " [Errno -5] No address associated with hostname, Timeout: 30s, Topology Description: <TopologyDescription id: 64207dd34375c6ac1adb4bf4, topology_type: Unknown, servers: [<ServerDescription ('', 27017) server_type: Unknown, rtt: None, error=AutoReconnect(' [Errno -5] No address associated with hostname')>]>",
  "errorType": "ServerSelectionTimeoutError"

Resources I’ve set up :

  • VPC with 2 private subnets, and security group that allows all outbound access
  • Lambda function running in this VPC and the 2 private subnets, and the security group
  • MongoDB serverless instance with access
  • Added ARN of the Lambda role to Database Access in MongoDB with permission readWriteAnyDatabase
  • Private endpoint for MongoDB (followed
  • Endpoint in AWS created with create-vpc-endpoint call that uses the endpoint service created above

How I’m calling MongoDB (code snippet) :

import pymongo

client = pymongo.MongoClient(os.getenv("MONGODB_CONNECTION_URI"))

def handler(event, context):
    data = some-data-to-insert

    db = client.myDatabase

where MONGODB_CONNECTION_URI = mongodb://

This seems to be a connectivity issue - what am I missing?

I needed to add +srv to my connection URI. The documentation on Authentication Examples — PyMongo 4.3.3 documentation is incorrect.

Hi @nefariousdream thanks for pointing this out. While the documentation is correct (because srv connection URIs and MONGODB-AWS authentication are unrelated features), I will update the docs since using srv is the most common thing nowadays. I opened

Hey @Shane, for my knowledge, why was the lambda unable to connect to the cluster without the srv connection URI, and what I should have done differently if not using it?

Your solution to add back the “+srv” is correct. mongodb+srv:// use a different hostname than mongodb://. It is expected that DNS will fail with NXDOMAIN or “No address associated with hostname” errors when the “+srv” portion is removed from a URI without also changing the hostname. Feel free to read more about this here:

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.