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?