Unable to import module lambda function No module named pymongo for mongodb with python in aws lambda

0 votes

I am getting exception in AWS lambda function as below for python program with MongoDB:-

  "errorMessage": "Unable to import module 'lambda_function': No module named 'pymongo'",
  "errorType": "Runtime.ImportModuleError"

But Pymongo added as a layer in lambda & also associated with the lambda function.

Nov 3, 2020 in AWS by pradipchoudhary
• 120 points

recategorized Nov 3, 2020 by MD 6,050 views

1 answer to this question.

0 votes


I think you need to import some modules in your lambda script. Or you didn't create the lambda function. It will ask for a Lambda function handler. You have to name it as your python_filename.lambda_handler.

Hope this helps!!

To know more about Mongodb, it's recommended to join Mongodb course online today.


answered Nov 3, 2020 by MD
• 95,440 points
Hi @MD,

Thanks for your reply.

I have checked the file name and handler name both are correct. Sharing the lambda function name & file name also for your reference

File Name : lambda_function

Function Name : lambda_handler

and final name as : lambda_function.lambda_handler

Ok. Can you share your script that you are trying to run in the lambda function?
import json
import pymongo
import requests
import os
from bs4 import BeautifulSoup
from pymongo import MongoClient
import datetime
db_userName = “*******”
db_password = “********”
db_name = “******”
db_host = “********”
db_port = “*******”
mongo_link = "mongodb://" + db_userName + ":" + db_password + "@" + db_host +":"+ db_port
def lambda_handler(event, context):
    return {
        'statusCode': 200,
        'body': json.dumps('Scrapping Done!')
def rss_news_parser():
—- code of this function—-


Your code seems totally fine. But your mongo_url seems not ok. Do one thing, just go to your database and copy the URL and paste it into your code.

let me check

Related Questions In AWS

+1 vote
1 answer
+1 vote
1 answer
0 votes
2 answers

How do I add python libraries to an AWS lambda function for Alexa?

If you get lucky (it depends what ...READ MORE

answered Nov 3, 2020 in AWS by anonymous
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
+1 vote
1 answer

Unable to import module 'lambda_function': No module named lambda_function

Hi@akhtar, This error appears when you haven’t named ...READ MORE

answered Apr 28, 2020 in AWS by MD
• 95,440 points
+1 vote
1 answer

'No module named 'requests'' on the AWS Python Lambda function?

Hi@akhtar, For a detailed, You can even check ...READ MORE

answered Apr 15, 2020 in AWS by MD
• 95,440 points
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP