Namrata N Shahade
Namrata N Shahade

Reputation: 41

Not able to connect with mongo from AWS lambda functionin python

I have aws lambda which is not able to connect with mongo through VPC.

import pymongo

def handler(event, context):
  try:
    client = pymongo.MongoClient(host="xxxxxxx", port=27017, username=x1, password=x2, authsource="x3, authMechanism='SCRAM-SHA-1')
  except pymongo.errors.ServerSelectionTimeoutError as err:
    print(err)

Not able to found the server.

I have created a security group and new roles have given VPC and lambda full access too but not able to connect.

Taken help from https://blog.shikisoft.com/access-mongodb-instance-from-aws-lambda-python/ as well as https://blog.shikisoft.com/running-aws-lambda-in-vpc-accessing-rds/ links.

Please be helpful. Trying since yesterday but no luck.

Upvotes: 2

Views: 1331

Answers (1)

Aaron Putra
Aaron Putra

Reputation: 21

Let me try to help you figured out where the problem is.

1. Are your MongoDB EC2 Instance and your Lambda hosted on the same VPC?

  • If this the cause of your problem, you should move your services into the same VPC.

2. Is your Security Group that attached to your MongoDB EC2 Instance and your Lambda has whitelisted/include the default sg?

  • You have to include the default sg into your Security Group so, services/instances within that VPC can communicate.

3. Is your hostname publicly or privately accessed ?

Hope these answers are helpful to you.

Upvotes: 2

Related Questions