6

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")
4
  • Is it mandatory for you to use pymongo? Commented Jul 26, 2016 at 1:18
  • Cann you provide more details? Commented Jul 26, 2016 at 14:43
  • No, don't need to use pymongo. Just really want to connect that's all. I have a connection string to my database that I want to pass to the Lambda function. Just not sure how to do it. Commented Aug 1, 2016 at 0:17
  • Why can't you load pymongo? Commented Sep 22, 2017 at 17:08

6 Answers 6

3

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
Sign up to request clarification or add additional context in comments.

Comments

1

MongoClient can be used to connect to MongoDatabase from Lambda.

MongoClientURI mongoClientURI = new MongoClientURI(mongoURl);
MongoClient mongoClient = new MongoClient(mongoClientURI);
MongoDatabase db = mongoClient.getDatabase(mongoDB);

Comments

0

first you need to connect via SSH, then it will set a connection between to your AWS lambda and Mongodb instance

if your mongodb hosts in EC2 instances, then you can pass this way

let dbDetails ={
    username:'', //Ubuntu or ec2-user
    dstHost:'localhost',
    host:'',  //IP Address of your instance
    dstPort:27017,
    privateKey:fs.readFileSync(''), //your pem file
    port:22
}

So, after this you need to use SSH client like ssh tunnel

   let server = tunnel(config, function (error, server) {
                   //handle error
        }

  {
                   //success message
}

Comments

0

Connect with SSH. 'ssh tunnel' npm modules is there. install it and pass the correct value to it like IP address, port, username, DB port with PEM file(Must). it will connect.

Comments

0
  1. make a virutalenv something like "venv" and run pip3 install pymongo in your local system
  2. then make a new folder that has all the packages of the venv installed (copy from venv --> lib --> python 3.x --> site packages)
  3. copy your lambda python file also into the same folder you have the pacakges
  4. then zip the folder and you see a upload zip file in aws lambda function, upload it and save
  5. note that, your lambda function py file is named as "lambda_file" and lambda function inside it is named as "lamda_function" then change the name in handler info in aws lambda placed in third column
  6. like this lambda_file.lambda_function

Comments

0

You can build a layer for PyMongo and reference it within your Lambda function. Please refer to https://www.kodyaz.com/aws/create-lambda-layer-for-pymongo-to-connect-amazon-documentdb-using-python.aspx for creating Lambda function Layer with Python and PyMongo and you can find a sample Lambda function utilizing this Layer for connecting to Amazon DocumentDB at https://www.kodyaz.com/aws/connect-amazon-documentdb-using-python-from-aws-lambda-function.aspx

Hope these two helps,

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.