By using AWS re:Post, you agree to the Terms of Use
/ECS tasks stops server while connecting to mongo atlas/

ECS tasks stops server while connecting to mongo atlas


“message”:“Server selection timed out after 30000 ms”,“reason”:{“type”:“ReplicaSetNoPrimary”,“servers”:{},“stale”:false,“compatible”:true,“heartbeatFrequencyMS”:10000,“localThresholdMS”:15,“setName”:“atlas-uhi03q-shard-0”}

This is error I am receiving when I connect to mongo atlas instance from ECS tasks (EC2 instance running on private subnet). Runs well when I run this application from localhost. I have also enabled VPC peering and IP under network access is open to the world. Cannot figure out the issue. Appreciate some help here.

  • Issue with outbound network connection from ec2 instance. I had created VPC endpoints for AWS related services, but for mongo atlas I had to create NAT gateway. With the registration of NAT gateway and allowing all routes in the routing table established mongo atlas connection from private ec2 instance. VPC peering might work, but as I was on shared cluster in mongo it didn't work for me.

1 Answers

A few things on top of my head:

  • Do you connect with the mongo+srv:// address ? If not, you should and make sure to have open the security group ports on your VPC Private Link (I presume that's what you meant by peering). That way the clients in reply will get both IP address and port to connect to. That's how we do and connection works great
  • Did you create a MongoDB user with your EC2 IAM task role ? If not, I'd recommend that over username/password. Checkout in CloudFormation -> Public Extensions -> 3rd Party -> look for MongoDb::Atlas::AwsIamDatabaseUser. Of course, that's if you use CFN.
answered a month ago
  • Yes I connect using mongo+srv:// address.. VPC peering is the latest option looks like but didn't work for me, as I am on the shared cluster. I have enabled all HTTP for my alb. What are the security ports I should enable? Can you share any links or elaborate a bit here?

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.

Guidelines for Answering Questions