Thanks for the awesome tool!
I've been having issues with sending a response after opening a mongo connection. In my implementation, I'm connecting to mlab and can verify that it actually works / connects. The issue is sending the response after Mongoose connects to mongodb.
I've put together some sample code below that you should be able to replace the mongodbUrl with a valid connections string to replicate.
FWIW, I'm using mlab; however, I did see connection rejections when attempting to connect to localhost even thought my mongo instance was running locally and other applications could use it.
Sample code:
index.js
exports.handler = (event, context, callback) => {
const mongodbUrl = 'mongodb://?:?@mongodbUrl:port/myMongoDb';
mongoose.Promise = Promise;
mongoose.connect(mongodbUrl, { useMongoClient: true });
const db = mongoose.connection;
db.once('open', () => callback(null, { statusCode: 200, body: "OK"} ));
}
template.yml
AWSTemplateFormatVersion : '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: |
An example RESTful service
Resources:
ExampleFunction:
Type: AWS::Serverless::Function
Properties:
Runtime: nodejs6.10
Timeout: 5
Handler: index.handler
Events:
ListCustomers:
Type: Api
Properties:
Path: /
Method: any
Error logged to the console:
2017/09/28 12:25:24 Invoking index.handler (nodejs6.10)
START RequestId: dd17310c-21de-18a9-7c3f-311a8ba489a3 Version: $LATEST
2017/09/28 12:25:30 Function index.handler timed out after 5 seconds
2017/09/28 12:25:30 Error invoking nodejs6.10 runtime: io: read/write on closed pipe
i did not work with mongoose but straight to an aws DynamoDB local instance and used following code.
var AWS = require('aws-sdk');
var config = {"endpoint":"http://10.0.75.1:8000", "region": "eu-west-1", "accessKeyId": "abcde", "secretAccessKey": "abcde"};
var client = new AWS.DynamoDB(config);
where http://10.0.75.1:8000 is my internal ip adress.
do you have permission to acces your DynamoDB sinse you can connect?
try and paste:
Policies: AmazonDynamoDBFullAccess
under your Runtime and see if that fixes anything
Can you add this line before a connection is made to MongoDB?
context.callbackWaitsForEmptyEventLoop = false;
see Optimizing AWS Lambda performance with MongoDB Atlas and Node.js
You can also diagnose connectivity issues by executing a shell inside the docker container:
docker run -v $(pwd):/var/task -i -t --rm --entrypoint /bin/bash lambci/lambda:java8
(replace java8 with your runtime of choice)
@olingern Looks like you are not able to open connection to mongo instance. Can you handle db.on("error") and print what error you get? Increase Lambda timeout to 300 seconds first, so your lambda waits for any client-side timeouts to hit.
Just curious, are there any networking settings in your Docker settings that is preventing outbound connections? And which OS are you running in?
Closing because of inactivity
Most helpful comment
Can you add this line before a connection is made to MongoDB?
context.callbackWaitsForEmptyEventLoop = false;see Optimizing AWS Lambda performance with MongoDB Atlas and Node.js