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,429 views

1 answer to this question.

0 votes

Hi@pradip,

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.

Thanks!

answered Nov 3, 2020 by MD
• 95,460 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
Hi@pradip,

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):
    rss_news_parser()
    return {
        'statusCode': 200,
        'body': json.dumps('Scrapping Done!')
    }
def rss_news_parser():
—- code of this function—-

Hi@pradip,

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
15,562 views
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,460 points
50,309 views
+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,460 points
16,892 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP