- Newest
- Most votes
- Most comments
Please check the below data points
Subnet configuration: Ensure both the DocumentDB and Fargate tasks are in subnets that can communicate with each other Routing Tables: Check the routing tables to ensure there are routes for the subnets to communicate. NACL :Inbound and Outbound Rules: NACLs should allow inbound and outbound traffic on port 27017.
Hello,
When you're trying to connect to an Amazon DocumentDB instance from a Fargate task running in a private subnet, there are a few things you should check:
-
Network ACL (NACL): Ensure that the Network ACL associated with the private subnet allows inbound and outbound traffic on port 27017 (MongoDB port). By default, NACLs allow all outbound traffic, but you should verify that the inbound rule is also configured correctly.
-
Route Tables: Verify that the route table associated with the private subnet has a route to the DocumentDB subnet or the DocumentDB endpoint. This route is typically added automatically when you create the DocumentDB cluster, but it's worth double-checking.
-
DNS Resolution: Make sure that your Fargate task can resolve the DNS name of the DocumentDB instance. You can try connecting using the endpoint URL or the endpoint IP address to isolate any potential DNS resolution issues.
-
Connection Timeout: Check if the connection timeout on your MongoDB client is set appropriately. Sometimes, the default timeout might be too low, especially if there are network latency issues or if the Fargate task needs to establish a new connection.
-
Fargate Task IAM Role: While you mentioned that the Fargate task role has full access to DocumentDB, double-check that the role has the necessary permissions to connect to the DocumentDB instance. The required permissions are typically documentdb:Connect and documentdb:GetItem.
-
Fargate Task Security Group: Ensure that the security group associated with the Fargate task allows outbound traffic on port 27017 to the DocumentDB security group.
-
DocumentDB Cluster Status: Verify that the DocumentDB cluster is in the "Available" state and that there are no issues with the cluster itself.
If you've checked all of these points and are still experiencing timeouts, you can try the following:
- Enable VPC Flow Logs to capture and analyze the network traffic between the Fargate task and the DocumentDB instance.
- Use the AWS CLI or the AWS Management Console to test the connectivity from the Fargate task to the DocumentDB instance using tools like nc (netcat) or telnet.
- Check the DocumentDB logs for any relevant error messages or connection failures.
- If you're using a custom MongoDB client, try connecting with a different client or the MongoDB shell to isolate any potential client-side issues.
Following these steps should help you identify and resolve the problem.
References :
[1] Why can’t I connect to my Amazon DocumentDB cluster? - https://repost.aws/knowledge-center/documentdb-cannot-connect
Hey,
Thanks for your suggestions.
- Network ACL (NACL) allow all traffice on all ports
- I have two private subnets Routes table associate with both of them have route for local traffic
- I created a cloud9 environment in the same vpc and using nc -zv <DocDB endpoint> <port number> returns connected but trying to connect with mongo shell keeps hanging
- I tried adding both the docdb and ecs service the same security group but did not work
- Fargate task role has AmazonDocDBFullAccess managed policy attached. I could not find documentdb:Connect or documentdb:GetItem in policies
- Cluster status is available
What I understand is that the fargate in your private subnet wants to access the db over the internet. If that's the case, you'll need some extra configuration. I have two ideas you can try.
- Use PrivateLink to access db
- If your fargate does not have any public IP, it cannot directly access the Internet. You can try adding a NAT gateway or other methods to side-assign the public IP
Both the docdb and fargate are in the same vpc and subnet selection.
- I do not want to connect over the internet but from within the VPC
You have create VPC endpoint to access DocumentDB because the private subnet might not have access to public internet. Moreover, VPC endpoints are more secure as connection happens within AWS private network (Connection never goes to public internet)

NACL allows traffic all inbound and outbound traffic on all ports