A.K.
A.K.

Reputation: 559

Unable to connect AWS lambda to Mongodb through VPC

AWS Lambda MongoDB Atlas Connection Timeout Issue (VPC Peering) I have an AWS Lambda function that connects to a MongoDB Atlas cluster. To enable this connection, I have:

  1. Configured a VPC and peered it with MongoDB Atlas.
  2. Added subnets and a security group to the VPC.
  3. Verified DNS settings (DNS hostnames and DNS resolution are enabled for the VPC).
  4. Ensured that MongoDB Atlas and AWS Lambda are in the same region.
service: Test-api
provider:
  name: aws
  runtime: nodejs18.x
  region: ap-south-1
  memorySize: 128
  timeout: 10
  stage: ${opt:stage, 'dev'} 
  environment:
    MONGO_URI: ${self:custom.mongoUris.${self:provider.stage}}

custom:
  mongoUris:
    dev: "mongodb+srv://URL"
    staging: "mongodb+srv://staging-uri"
    prod: "mongodb+srv://prod-uri"

functions:
  createUserStatus:
    handler: serverless/functions/userStatus/create.handler
    vpc:
      securityGroupIds:
        - sg-**********
      subnetIds:
        - subnet-**********
    events:
      - http:
          path: user-status
          method: post
          cors: true

plugins:
  - serverless-offline

MongoDb VPC Peering

VPC

route] Route

Despite these configurations, I am still getting a MongoDB connection timeout error when my Lambda function attempts to connect.

What additional configurations should I check in my AWS VPC setup to ensure connectivity?

Upvotes: 1

Views: 47

Answers (0)

Related Questions