0

I have created a Lambda Function which intends to connect to MongoDB running on EC2.

I have followed some tutorials and guaranteed that:

  • Lambda and EC2 run in the same VPC
  • Lambda has configured EC2's subnet
  • Lambda has its own security group my-lambda-sg
  • Lambda's security group is allowed in EC2's security group inbounds rules for MongoDB's port as a "Custom TCP Rule"
  • Lambda's role has assigned permission AWSLambdaVPCAccessExecutionRole

However, I am stil unable to connect from the lambda to the MongoDB in EC2. When I run a Test in the Lambda I get:

START RequestId: f0869292-5207-11e8-85d2-cba0eb99208c Version: $LATEST
2018-05-07T15:04:29.117Z    567951c4-5207-11e8-ba43-0314179d2bf5    { MongoNetworkError: failed to connect to server [IP:port] on first connect [MongoNetworkError: connection 0 to IP:port timed out]
    at Pool.<anonymous> (/var/task/node_modules/mongodb-core/lib/topologies/server.js:503:11)
    at emitOne (events.js:116:13)
    at Pool.emit (events.js:211:7)
    at Connection.<anonymous> (/var/task/node_modules/mongodb-core/lib/connection/pool.js:326:12)
    at Object.onceWrapper (events.js:317:30)
    at emitTwo (events.js:126:13)
    at Connection.emit (events.js:214:7)
    at Socket.<anonymous> (/var/task/node_modules/mongodb-core/lib/connection/connection.js:256:10)
    at Object.onceWrapper (events.js:313:30)
    at emitNone (events.js:106:13)
    at Socket.emit (events.js:208:7)
    at Socket._onTimeout (net.js:420:8)
    at ontimeout (timers.js:482:11)
    at tryOnTimeout (timers.js:317:5)
    at Timer.listOnTimeout (timers.js:277:5)
  name: 'MongoNetworkError',
  message: 'failed to connect to server [IP:port] on first connect [MongoNetworkError: connection 0 to IP:port timed out]' }
END RequestId: f0869292-5207-11e8-85d2-cba0eb99208c

From API Gateway I get

You do not have permission to perform this action

And from the browser I get:

{"message": "Endpoint request timed out"}

Other Lambda's not accessing EC2 are working fine.

Any idea what could I be missing?

pellyadolfo
  • 981
  • 10
  • 23

1 Answers1

0

I found the answer. All requisites above are ok. I just needed to use the internal IP of the EC2 instead the external one in the MongoDB URL connection, to be considered a call within the VPC, as I found here AWS Lambda unable to access EC2 port within the same VPC

pellyadolfo
  • 981
  • 10
  • 23