Can't access my private database from API Gateway

I have an AWS API Gateway, it’s a node serverless api, i have set the MONGO_URL like this:

mongodb+srv://<AWS access key>:<AWS secret key>@devcluster.6k6ngpc.mongodb.net/?authSource=%24external&authMechanism=MONGODB-AWS&retryWrites=true&w=majority&authMechanismProperties=AWS_SESSION_TOKEN:<session token (for AWS IAM Roles)>

However, I get this error when trying to access it from the api:

MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist

Is there a way to bypass this without allowing access from anywhere?

Hi there and welcome to the MongoDB forums!

One thing I might consider is utilizing Elastic IP addresses to create a static outbound IP address for your lambda functions. You will then be able to allowlist only these IP addresses for your Atlas project.

Another option is utilizing Private Endpoints in Atlas, which will result in network traffic transiting only within AWS’s network and therefore not require you to allowlist all addresses.

2 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.