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")
Amazon DocumentDB is a scalable, highly durable, and fully managed database service for operating mission-critical MongoDB workloads.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With