Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda and MongoDB

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?

client = MongoClient()
client = MongoClient("mongodb://xxxxxx:27017 username user --password")
like image 796
Yolo49 Avatar asked Jul 26 '16 00:07

Yolo49


People also ask

What is the equivalent of MongoDB in AWS?

Amazon DocumentDB is a scalable, highly durable, and fully managed database service for operating mission-critical MongoDB workloads.

Can I use MongoDB with AWS?

MongoDB is an AWS Partner. To launch a fully managed MongoDB cluster on AWS, try it for free from AWS Marketplace. AWS Service Catalog administrators can add this architecture to their own catalog.

Can you use MongoDB with AWS Lambda?

With AWS IAM authentication, Atlas accesses AWS Lambda through an assumed IAM role , so you don't need credentials in your connection strings. Atlas supports AWS IAM authentication for clusters running MongoDB version 4.4 or higher.

Can AWS Lambda connect to database?

Yes. AWS Lambda can connect to an AWS hosted databases such as RDS or DynamoDB. AWS Lambda can also connect to external databases which are public or grant network access. Dependent on the database you're using (or intending to use) there are some considerations you should address.


1 Answers

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

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
like image 75
Omar Merghany Avatar answered Oct 11 '22 08:10

Omar Merghany