Question:
I’m new to AWS Lambda and interested in trying it. I have a MongoDB instance that I want to connect to through my AWS lambda function. How would I connect to my mongo instance? I can’t load pymongo onto AWS Lambda so how would I get this to work in the Lambda function?
1 2 3 |
client = MongoClient() client = MongoClient("mongodb://xxxxxx:27017 username user --password") |
Answer:
You have to use Pymongo, you can download it using pip install pymongo -t <your_location>
after that zip it with your code and any dependancy then upload it to the Lambda console
1 2 3 4 5 6 7 8 9 10 11 |
import pymongo name = "db_username" password = "db_password" db_name = "db_name" db_host = "db_host" mongo_link = "mongodb://"+name+":"+password+"@"+db_host+"/"+db_name def handler(event, context): client = pymongo.MongoClient(mongo_link) # Get the sampleDB database db = client.sampleDB |