Search code examples
mongodbamazon-ec2aws-lambdaamazon-vpcaws-security-group

How to enable aws lambda to access ec2 mongo instance in the same aws account


I have an EC2 instance for mongoDB and i created a lambda function to query this DB , used mongoClient for connection, This connection is successfull and i can query the DB when my mongo security group set as anywhere but i need some security for my DB so i changed it to custom IP and port, then i couldn't connect my lambda function to mongoDB , both this lambda function and EC2 instance are in same aws account. Is VPC configuration has any role in this ? Is there any secure way to make the connection successfull?


Solution

  • Place the Lambda function inside your VPC. Then modify the MongoDB instance's security group to allow inbound connections (on the MongoDB port) from the security group assigned to the Lambda function.