0

I have set up an AWS Lambda function using the AWS SAM app. I have also downloaded local MongoDB on my machine. I am trying to make a connection between AWS Lambda and MongoDB. You can see my code below:

import json
import pymongo
client = pymongo.MongoClient('mongodb://localhost:27017/')
mydb = client['Employee']

def lambda_handler(event, context):
    information = mydb.employeeInformation
    record = {
        'FirstName' : 'Rehan',
        'LastName' : 'CH',
        'Department' : "IT" 
    }
    information.insert_one(record)
    print("Record added")

    return {
        "statusCode": 200,
        "body": json.dumps(
            {
                "message": "hello world",
                # "location": ip.text.replace("\n", "")
             }
    ),
}

When I run the function using sam local invoke it throws an error that you can see below:

[ERROR] ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused, Timeout: 30s, Topology Description: <TopologyDescription id: 62b16aa14a95a3e56eb0e7cb, topology_type: Unknown, servers: [<ServerDescription ('localhost', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('localho    raise ServerSelectionTimeoutError(, line 227, in _select_servers_looprtn_support

I also have searched for this error and eventually, I found some but didn't get help from them. That's why I have to post it again.

Its my first time interacting with MongoDB. Can someone tell me how do I resolve this error, or where I am doing wrong?

M Rehan
  • 13
  • 3
  • What do you mean with local lambda & local mongodb?? Lambda is a serveless function, so it does not have localhost – nacho Jun 21 '22 at 07:29
  • Also, don't call AWS Lambda a lambda function, which is a different beast in Python. You can [edit] your question to clarify that. Also, as a new user, read [ask] and take the [tour]. – Ulrich Eckhardt Jun 21 '22 at 07:30
  • I have edited the question, hopefully, it is clear now. And thanks for guidance. – M Rehan Jun 21 '22 at 07:47
  • To connect to a instance of MongoDB in your computer from AWS cloud, don't use localhost (that is to connect from your computer to your computer). You will need your IP and open firewall ports in your router – nacho Jun 21 '22 at 09:13
  • I have replaced localhost with ip address, still facing same error. – M Rehan Jun 21 '22 at 09:42
  • Did you open the port 27017 on you router?? – nacho Jun 21 '22 at 12:39
  • Can you please tell me how I can open port 27017 on my router? Apparently, I have no idea how to do it? – M Rehan Jun 21 '22 at 12:51
  • @MRehan you should not need to open any ports on your router for local dev. Maybe check out these 2 questions on how to connect to local mysql container from local lambda for development. Seems like you hitting very similar issue. My guess is has to do with docker network. https://stackoverflow.com/questions/48926260/connecting-aws-sam-local-with-dynamodb-in-docker/50880987 https://stackoverflow.com/questions/51456643/how-do-i-connect-to-host-mysql-from-aws-sam-local-docker-instance/52167375 – Ellery Jun 21 '22 at 15:52

1 Answers1

0

I have resolved my issue. We don't need to handle any port manually. Whenever we are trying to connect our locally created SAM app with local MongoDB then we should set up our MongoDB on Docker. Then set the IP address of the container to the connection string.

Initially, I was trying this with localhost:

client = pymongo.MongoClient('mongodb://localhost:27017/')

What to change? We only have to use the IP address of the dockerized MonogoDB, and it will look like this:

client = pymongo.MongoClient('mongodb://172.17.0.2:27017/')
M Rehan
  • 13
  • 3