pymongo.errors.ServerSelectionTimeoutError when connect to MongoDB Atlas from Lambda

Hi, I am having problem connecting to MongoDB Atlas from my AWS Lambda Function.

My Lambda function looks like this:

...

db_url = os.environ.get('DB_URL')
db = os.environ.get('DB')
db_collection = os.environ.get('DB_COLLECTION')


client = pymongo.MongoClient(db_url)
database = client[db]

def lambda_handler(event, context):
    try:
        logger.debug(f"Event captured: {json.dumps(event)}")

        user = /* Some code to get data*/
        collection = database[db_collection]

        collection.insert_one(user)
      
        return user
    except Exception as e:
        logger.exception(f"Exception: {e}")

        return {
            'statusCode': 500
        }

This code works well when I run in on local machine but when I run it from Lambda Function, I got the error:

[ERROR]	2022-04-20T08:20:23.344Z	718b9e10-cedc-4d22-b6f3-a6f3230471d8	Exception: smslogcluster-shard-00-01.cnoue.mongodb.net:27017: timed out,smslogcluster-shard-00-00.cnoue.mongodb.net:27017: timed out,smslogcluster-shard-00-02.cnoue.mongodb.net:27017: timed out, Timeout: 30s, Topology Description: <TopologyDescription id: 625fc22906289886d8d3bdfa, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('smslogcluster-shard-00-00.cnoue.mongodb.net', 27017) server_type: Unknown, rtt: None, error=NetworkTimeout('smslogcluster-shard-00-00.cnoue.mongodb.net:27017: timed out')>, <ServerDescription ('smslogcluster-shard-00-01.cnoue.mongodb.net', 27017) server_type: Unknown, rtt: None, error=NetworkTimeout('smslogcluster-shard-00-01.cnoue.mongodb.net:27017: timed out')>, <ServerDescription ('smslogcluster-shard-00-02.cnoue.mongodb.net', 27017) server_type: Unknown, rtt: None, error=NetworkTimeout('smslogcluster-shard-00-02.cnoue.mongodb.net:27017: timed out')>]>
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 30, in lambda_handler
    collection.insert_one(user)
  File "/opt/python/pymongo/collection.py", line 606, in insert_one
    self._insert_one(
  File "/opt/python/pymongo/collection.py", line 547, in _insert_one
    self.__database.client._retryable_write(acknowledged, _insert_command, session)
  File "/opt/python/pymongo/mongo_client.py", line 1398, in _retryable_write
    with self._tmp_session(session) as s:
  File "/var/lang/lib/python3.8/contextlib.py", line 113, in __enter__
    return next(self.gen)
  File "/opt/python/pymongo/mongo_client.py", line 1676, in _tmp_session
    s = self._ensure_session(session)
  File "/opt/python/pymongo/mongo_client.py", line 1663, in _ensure_session
    return self.__start_session(True, causal_consistency=False)
  File "/opt/python/pymongo/mongo_client.py", line 1608, in __start_session
    self._topology._check_implicit_session_support()
  File "/opt/python/pymongo/topology.py", line 519, in _check_implicit_session_support
    self._check_session_support()
  File "/opt/python/pymongo/topology.py", line 535, in _check_session_support
    self._select_servers_loop(
  File "/opt/python/pymongo/topology.py", line 227, in _select_servers_loop
    raise ServerSelectionTimeoutError(
pymongo.errors.ServerSelectionTimeoutError: smslogcluster-shard-00-01.cnoue.mongodb.net:27017: timed out,smslogcluster-shard-00-00.cnoue.mongodb.net:27017: timed out,smslogcluster-shard-00-02.cnoue.mongodb.net:27017: timed out, Timeout: 30s, Topology Description: <TopologyDescription id: 625fc22906289886d8d3bdfa, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('smslogcluster-shard-00-00.cnoue.mongodb.net', 27017) server_type: Unknown, rtt: None, error=NetworkTimeout('smslogcluster-shard-00-00.cnoue.mongodb.net:27017: timed out')>, <ServerDescription ('smslogcluster-shard-00-01.cnoue.mongodb.net', 27017) server_type: Unknown, rtt: None, error=NetworkTimeout('smslogcluster-shard-00-01.cnoue.mongodb.net:27017: timed out')>, <ServerDescription ('smslogcluster-shard-00-02.cnoue.mongodb.net', 27017) server_type: Unknown, rtt: None, error=NetworkTimeout('smslogcluster-shard-00-02.cnoue.mongodb.net:27017: timed out')>]>END RequestId: 718b9e10-cedc-4d22-b6f3-a6f3230471d8

I’ve tried setting the IP for network access is 0.0.0.0/0, creating user, adding this tag

?ssl=true&ssl_cert_reqs=CERT_NONE

to the host string, … but none of those works.
Can anyone help me figure out what’s the problem? Thanks in advance.

Are you sure you’re providing the authentication credentials?

Yes.
My connection string looks like this:

mongodb+srv://<username>:<password>@smslogcluster.cnoue.mongodb.net/test?retryWrites=true&w=majority

And now I think the problem comes from the VPC which I’m connecting my Lambda function to cuz when I remove the VPC, my lambda function can connect to Mongo. So is there any configuration needed to connect Mongo to Lambda function in a VPC?

Sorry for the delay: as a general rule if you’re using Lambda in a VPC then you can take advantage of either VPC Peering or AWS PrivateLink (Atlas private endpoints) to connect to your Atlas cluster: this works for M10+ dedicated clusters.