AWS Lambda unable to connect to Atlas Serverless

The error I receive :

  "errorMessage": "dev-pe-0.vvwclmg.mongodb.net:27017: [Errno -5] No address associated with hostname, Timeout: 30s, Topology Description: <TopologyDescription id: 64207dd34375c6ac1adb4bf4, topology_type: Unknown, servers: [<ServerDescription ('dev-pe-0.vvwclmg.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('dev-pe-0.vvwclmg.mongodb.net:27017: [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 0.0.0.0/0 access
  • Added ARN of the Lambda role to Database Access in MongoDB with permission readWriteAnyDatabase
  • Private endpoint for MongoDB (followed https://www.mongodb.com/docs/atlas/security-serverless-private-endpoint/)
  • 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
    db.myCollection.insert_many(data)

where MONGODB_CONNECTION_URI = mongodb://dev-pe-0.vvwclmg.mongodb.net/?authMechanism=MONGODB-AWS

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 https://jira.mongodb.org/browse/PYTHON-3643

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: https://www.mongodb.com/docs/manual/reference/connection-string/#dns-seed-list-connection-format

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