Task timed out after 5.01 seconds - MongoDB Atlas AWS connection issue

Hello Angela,

I am running into the same issue when my allowed IP access is and i have the timeout, with the exact same setup.
I have checked the credentials in my AWS lambda and everything is setup correctly, do you have any idea where else it can come from ?
It works perfectly fine with Password authentication but times out when using the IAM, even by trying to get the server info (so it’s not connected), but i have no connection errors.

Note : the in app chat session support told me it’s out of scope and i can’t currently subscribe to the developer support plan unfortunately that’s why i am asking again here… Maybe it’s the issue as the above that you have solved privately.

Thanks in advance for any help,

Hi :wave: @Tommy_Deshairs,

Welcome back to the MongoDB Community forums :sparkles:

Have you considered configuring a static IP address for the lambda? You can place it in a private subnet and then use a NAT gateway. This way, you can add the IP to your MongoDB whitelist. Alternatively, you can also keep your IP access list as, and it will still work.

Also, can you please share what steps you followed? Meanwhile, you can also check out the Manage Connections with AWS Lambda documentation.


Hi @Kushagra_Kesav thanks for your reply !

Regarding the static IP, isn’t better to use the “Peering” ? Or is it the same thing ?
I should simply connect my Lambda to a VPC and do the peering i guess, my Lambda should never be exposed on the internet, i have an API Gateway for requests.
I will indeed remove eventually the, it is currently for testing to be sure my setup is correct and once it’s validated i will add network security.

In the meantime, here are the steps i have made so far :

  1. I have created a custom role in my AWS account that is used for my Lambda functions (even before i was using MongoDB Atlas).
  2. I have created a custom role in MongoDB Atlas UI that grants all permission on my database
  3. I have created a database user in MongoDB Atlas UI using AWS IAM.
  4. I have pasted the role ARN of my Lambda role in AWS
  5. I have associated the database user to the custom role
  6. In the lambda code, i was connecting using pymongo :
client = MongoClient(os.environ.get("DB_URL"))
db = client.get_database(os.environ.get("DB_NAME"))

Which is working fine with the DB_URL value using the password string.

  1. I have replaced the above code with :
uri = 'mongodb://' + quote_plus(os.environ.get("AWS_ACCESS_KEY_ID")) + ':' + quote_plus(os.environ.get("AWS_SECRET_ACCESS_KEY")) + os.environ.get("DB_LAST_SEGMENT_URL") + quote_plus(os.environ.get("AWS_SESSION_TOKEN"))
client = MongoClient(uri)
  1. The final uri looks like this and seems to be accepted because before writing this code i have got many issues with the connection string malformed and this one seems to be correct :

Note : i have edited manually the values to give fake IDs, but so you see the structure of the output. the / are escaped in the AWS_SECRET_ACCESS_KEY as requested by pymongo

Thanks again for your help,

Hello @Tommy_Deshairs,

Thanks for sharing the detailed steps.

Can you share the full output of the error message you got after encountering the issue? In addition, can you confirm that you have the required dependencies, pymongo[aws] according to our Authentication Mechanism documentation?


@Kushagra_Kesav thanks for your information (the link) and specifying that i should use pymongo[aws], indeed i was still using pymongo[srv].

However this still times out… But i did read in the link you provided this :

And i notice 2 things compared to my URI string :

  • I have not authMechanism=MONGODB-AWS
  • I should not be using the ENV VARS in the URI

I did the following tests and all failed (i have attached some logs, not all of them) :

  • The uri looks like : uri = "mongodb://example.com/mydatabase?authMechanism=MONGODB-AWS&retryWrites=true"
[INFO]	2023-04-25T18:14:32.183Z	d20d74ef-a284-4102-b4bf-05f6a84bc7e5	Uri after parse : mongodb://myproject.80ebt.mongodb.net/?authMechanism=MONGODB-AWS&retryWrites=true and client : Database(MongoClient(host=['myproject.80ebt.mongodb.net:27017'], document_class=dict, tz_aware=False, connect=True, authmechanism='MONGODB-AWS', retrywrites=True), 'mydatabase')
2023-04-25T18:14:37.190Z d20d74ef-a284-4102-b4bf-05f6a84bc7e5 Task timed out after 5.01 seconds
  • The uri looks like : uri = "mongodb://example.com/mydatabase?authMechanism=MONGODB-AWS" (like the documentation without retryWrites=true)
  • The uri looks like : uri = "mongodb://example.com/?authMechanism=MONGODB-AWS" (like the documentation, without specifing the database name) :
[INFO]	2023-04-25T18:15:17.319Z	783af7f1-6238-4b05-96d5-2e2798888d56	Uri after parse : mongodb://myproject.80ebt.mongodb.net/?authMechanism=MONGODB-AWS and client : Database(MongoClient(host=['myproject.80ebt.mongodb.net:27017'], document_class=dict, tz_aware=False, connect=True, authmechanism='MONGODB-AWS'), 'mydatabase')
2023-04-25T18:15:22.326Z 783af7f1-6238-4b05-96d5-2e2798888d56 Task timed out after 5.01 seconds
  • The uri looks like : mongodb+srv://:@project_name.80ebt.mongodb.net/?authSource=%24external&authMechanism=MONGODB-AWS&retryWrites=true&w=majority&authMechanismProperties=AWS_SESSION_TOKEN:<session token (for AWS IAM Roles)>

All these tests were made using pymongo[aws]==4.3.3.

Really appreciate your help on this,

Hello @Tommy_Deshairs,

I will suggest a couple of workarounds for you to try and see if they work:

I hope it helps!


Hello @Kushagra_Kesav thanks for the following up.

I have triple checked the ARN and also granted specific priviledges (dbAdmin) instead of my custom role (which was allowing insert / remove / update / find) to see if this was a permission issue but no…

Also, i have setup to 10 seconds the time out of my lambda and this is still in time out unfortunately :frowning:

I am attaching here the link of my related SO question if someone has any other ideas.

In the meantime, i will simply delete this DB user and create a new one from scratch to see if there is something wrong with this one.

Best regards,

Hi @Tommy_Deshairs,

If the suggested steps are not working, then I think there might be an issue with parsing the environment variable into your final URI. For example, I noticed a few %2F within the final URI that you shared above.

Could you try embedding the variable directly and see if that resolves the issue?


Hi @Kushagra_Kesav ,
It was a silly mistake.
I have added the +srv field to this url :

Which gives a working solution :

Actually not that silly because the documentation states it’s mongodb://... :frowning:

Thank you very much for your time and effort trying to help me out, really appreciated.
Best regards,

1 Like