Skip to content

Connection Error With MongoDB Atlas Via ECS

0

I am trying to access from a AWS ECS container to a cluster in MongoDB Atlas. I am getting the same error over and over again ("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: https://docs.atlas.mongodb.com/security-whitelist/"). I have followed two Stack Overflow tips, where they say that when creating a Nat Gateway, which connects to the IGW, will allow the outbound traffic to eventually reach the MongoDB cluster. I did that, by creating a new routing table, with 3 subnets created by default with Fargate (the latter ones had a routing table to point to the IGW directly), so what I did is that now, this private subnets point to the Nat Gateway, but also created a new "public subnet", where the Nat Gateway resides and goes to the IGW. Also, the security group rules have inbound and outbound traffic accepted by all IP's. As well, added a new security rule into the same security group, to allow outbound traffic to port 27017, as it is main's MongoDB communication port. After this steps, I "whitelisted" in MongoDB the Elastic IP Address given by the Nat Gateway creation, but still get the previously mentioned error, I have also give all access to the cluster "0.0.0.0/0"......

EDITED Enter image description here Here is the resource map for the net configuration. The MongoDB configuration should not be wrong, as I connected locally to the MongoDB Cluster, and the env. variables are the same. So, the resource map, I think is understandable, as each "private subnet" created by Fargate have a created routing table (these are the subnets used by ECS service), which destination is 0.0.0.0/0 and target is the NAT Gateway. The NAT Gateway has inside of it the "public subnet", which points then to the default IGW (created by ECS). The other "public subnet" marked as "B" with a green circle, points to the default routing table created by ECS which has destination 0.0.0.0/0 and target the default IGW. Hopefully this is understandable so I can get help on this.

2 Answers
1

Hello.

Is it correct to recognize that the route for the NAT Gateway is set in the route table of the subnet where the ECS task starts?
Are you setting up your route table in the wrong subnet?
By the way, it is also possible that the MongoDB authentication information, cluster name, user name, etc. specified in the container application are incorrect and you are trying to connect to a different cluster.
Therefore, please make sure that the connection information is correct.

If the network settings on the AWS side are configured correctly, you can check the connection log to see if a connection is occurring from the NAT Gateway.
If there is no connection from NAT Gateway, I think there is a problem with the settings such as the route table.
https://www.mongodb.com/docs/atlas/access-tracking/

EXPERT

answered 2 years ago

EXPERT

reviewed 2 years ago

  • I tried to explain more the issue I am having by updating the question, I shared the VPC's resource map...

  • Thank you for sharing the resource map. It seems that the route table etc. are set without any problems. By the way, is it possible to start EC2 in the subnet where ECS is started and connect to the cluster using the "mongo" command? If you can connect using the "mongo" command, there may be a problem with ECS or the application.

0

The issue you're experiencing with connecting to MongoDB Atlas from your AWS ECS container is likely related to network configuration. Here are some points to consider and potential solutions:

  1. NAT Gateway Configuration: Your approach of using a NAT Gateway for outbound traffic from private subnets is correct. Ensure that the NAT Gateway is properly configured in a public subnet with a route to the Internet Gateway.

  2. Route Tables: Verify that the route tables for your private subnets (where your ECS tasks are running) have a route to the NAT Gateway for internet-bound traffic (0.0.0.0/0).

  3. Security Groups: While you've mentioned allowing all inbound and outbound traffic, double-check that the security group associated with your ECS tasks allows outbound traffic on port 27017 to 0.0.0.0/0.

  4. MongoDB Atlas Whitelist: Although you've whitelisted the NAT Gateway's Elastic IP and 0.0.0.0/0, ensure this change has propagated. Sometimes, changes in MongoDB Atlas can take a few minutes to take effect.

  5. DNS Resolution: Ensure that your ECS task can resolve the DNS name of the MongoDB Atlas cluster. You might want to try connecting using the IP address instead of the hostname to isolate any potential DNS issues.

  6. Connection String: Verify that you're using the correct connection string format for MongoDB Atlas. It should look something like: mongodb+srv://username:password@cluster.mongodb.net/dbname

  7. VPC Peering or PrivateLink: For a more secure and potentially more reliable connection, consider setting up VPC peering or using AWS PrivateLink to connect to MongoDB Atlas. This can provide a private connection that doesn't traverse the public internet.

  8. Fargate Network Mode: If you're using Fargate, ensure you're using the awsvpc network mode, which provides each task with its own elastic network interface.

  9. Task IAM Role: Verify that the IAM role associated with your ECS task has the necessary permissions to access external resources.

If you've verified all these points and are still experiencing issues, you may want to enable VPC Flow Logs to analyze the network traffic and identify any potential blockages. Additionally, you could try running a test container with network debugging tools to further diagnose the connection issue from within the ECS environment.
Sources
Can not connect to Atlas Mongo server from Fargate container without public IP | AWS re:Post
Connection timeout white connecting from fargate to document db | AWS re:Post
setting connection between aws glue and mongodb atlas | AWS re:Post

answered 2 years ago

EXPERT

reviewed 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.