You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

通过VPC连接MongoDB Atlas与Lambda失败,请求超时如何解决?

Troubleshooting Lambda Timeout When Connecting to MongoDB Atlas

Hey there, that 30-second timeout error usually points to a network connectivity issue between your Lambda function and your Atlas cluster—let’s break down the key checks to fix this:

1. Verify VPC Peering & Network Routes

  • Double-check that all Lambda subnets are associated with the VPC peering connection you set up with Atlas. It’s easy to miss one subnet when configuring.
  • Confirm your Lambda subnet’s route table includes a route for your Atlas cluster’s VPC CIDR range, with the target set to the VPC peering connection ID. Without this route, traffic can’t reach Atlas.
  • Ensure Atlas’s route table also has a return route pointing to your Lambda VPC’s CIDR range.

2. Security Group Rules

  • Lambda’s Security Group: Make sure it allows outbound traffic to port 27017 (MongoDB’s default port) to the Atlas cluster’s VPC CIDR or security group.
  • Atlas Cluster’s Security Group: It needs to explicitly allow inbound traffic on port 27017 from your Lambda VPC’s CIDR range or Lambda’s security group. A common mistake is restricting access to a single IP instead of the entire VPC.
  • Test connectivity directly: Spin up an EC2 instance in the same VPC/subnet as your Lambda, then run nc -zv <your-atlas-cluster-endpoint> 27017. If this fails, your network setup is the problem—not Lambda itself.

3. Lambda Configuration Checks

  • Even though you’re hitting the 30-second timeout, confirm your Lambda’s timeout setting is sufficient (you can bump it temporarily to 60 seconds to rule out execution time issues, but this is unlikely the root cause here).
  • Ensure your Lambda’s IAM role has the necessary VPC permissions: ec2:CreateNetworkInterface, ec2:DescribeNetworkInterfaces, and ec2:DeleteNetworkInterface. Without these, Lambda can’t create the network interfaces needed to connect to your VPC.

4. Atlas-Specific Settings

  • Make sure you’re using the private endpoint for your Atlas cluster (not the public one) when connecting from a peered VPC. Atlas provides a private DNS name for VPC peering—using the public endpoint will fail if you’ve restricted Atlas to VPC-only access.
  • Confirm your Atlas cluster is deployed in the same region as your Lambda’s VPC, or that cross-region VPC peering is properly configured (cross-region can add latency, but shouldn’t cause a full timeout).
  • Check Atlas’s Network Access settings to ensure you haven’t accidentally disabled VPC peering access or added conflicting restrictions.

5. Private Subnet Considerations

If your Lambda is in a private subnet:

  • You don’t need a NAT Gateway for VPC peering traffic, but ensure your subnet’s route table doesn’t route all traffic through NAT (which would bypass the peering connection).
  • Verify there are no network ACLs blocking traffic between the two VPCs on port 27017. Network ACLs are stateless, so you need both inbound and outbound rules allowing the traffic.

Once you’ve worked through these checks, test your Lambda again—most timeout issues in this setup boil down to a missing route, misconfigured security group, or incorrect endpoint.

内容的提问来源于stack exchange,提问作者pelcomppl

火山引擎 最新活动