Comments (11)
I would like to upload this because I couldn't got mongoose to work until i explicitly call the function
db.close()
Otherwise , lambda continue to process the function , due to Node event loop never ending.
Is this specified anywhere in the documentation ?
from examples.
@leeeomaaax Thank you so much for this !
from examples.
For sure! I'd love to see this
from examples.
@kuashe , I've just been through this. I was trying to make mongoose work with lambda and everything was working well on local, both on invoke test (serverless-mocha-plugin) and invoke local. But on the lambda environment my function was timing out even though it was getting all the way through the code.
There is another way of solving this. Use this context property to prevent the lambda from executing the handler callback.
context.callbackWaitsForEmptyEventLoop = false;
This way you can keep your connection to mongo open and speed up the warm calls to your function.
from examples.
merged #150
https://github.com/serverless/examples/tree/master/aws-node-rest-api-mongodb
from examples.
@kuashe I am using the following https://github.com/serverless/serverless-graphql as a boilerplate, but have integrated mongoDB instead of DynamoDB (using mongo atlas). Everything works fine offline using serverless-offline
plugin. But when I sls log
my lambda functions once i deploy i get the following error
2018-01-28 18:58:59.726 (-08:00) 59696d6e-04a0-11e8-82ec-8974eb6ffd47 MongoDB connection error. Please make sure MongoDb is running. { MongoError: failed to connect to server [undefined:27017] on first connect [MongoError: getaddrinfo ENOTFOUND undefined undefined:27017]
at Pool.<anonymous> (/var/task/node_modules/mongodb-core/lib/topologies/server.js:336:35)
at emitOne (events.js:96:13)
at Pool.emit (events.js:188:7)
at Connection.<anonymous> (/var/task/node_modules/mongodb-core/lib/connection/pool.js:280:12)
at Connection.g (events.js:292:16)
at emitTwo (events.js:106:13)
at Connection.emit (events.js:191:7)
at Socket.<anonymous> (/var/task/node_modules/mongodb-core/lib/connection/connection.js:189:49)
at Socket.g (events.js:292:16)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at connectErrorNT (net.js:1021:8)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickDomainCallback (internal/process/next_tick.js:128:9)
name: 'MongoError',
message: 'failed to connect to server [undefined:27017] on first connect [MongoError: getaddrinfo ENOTFOUND undefined undefined:27017]' }
In my handler.js I have similar code
exports.graphqlHandler = function graphqlHandler(event, context, callback) {
context.callbackWaitsForEmptyEventLoop = false;
function callbackFilter(error, output) {
if (!output.headers) {
output.headers = {};
}
// eslint-disable-next-line no-param-reassign
output.headers['Access-Control-Allow-Origin'] = '*';
output.headers['Access-Control-Allow-Credentials'] = true;
output.headers['Content-Type'] = 'application/json';
callback(error, output);
}
mongoose.connect(mongoString, {useMongoClient: true});
mongoose.connection.on("error", (err) => {
console.log("MongoDB connection error. Please make sure MongoDb is running.", err);
process.exit();
});
const handler = server.graphqlLambda({ schema: myGraphQLSchema });
return handler(event, context, callbackFilter);
};
The context.callbackWaitsForEmptyEventLoop = false;
doesn't seem to be helping in my case.
Were you getting a similar error until you used db.close()
?
from examples.
@kvreem The error seam pretty straight forward to me . It's your database connection .
Fork the repository , replace the mongo string at the top of the file to make sure you database is reachable from a lambda function.
Hence, make sure you are waiting in your graphql handler for the database connection to be open.
from examples.
@kuashe I am aware the connection is the problem, but since my application logic isn't exactly in the handler.js like the repo, I am a little bit confused how to refactor so that I make my handler wait for the database connection to be open. I also have 0.0.0.0 ip whitelisted since I am just testing development cluster with mongo atlas.
const handler = server.graphqlLambda({ schema: myGraphQLSchema });
return handler(event, context, callbackFilter);
Does the above snippet need to be moved in db.once('open'
function?
Or does each query I make with graphql need to have the connection open then close it once it fetches the data? For example my query here:
export function getBookings() {
return new Promise((resolve, reject) => {
BookingModel
.find()
.then((bookings) => {
let bookingMap = [];
bookings = _.orderBy(bookings, ['updatedAt'], ['desc']);
bookings.forEach((booking, index) => {
bookingMap[index] = booking;
});
resolve(bookingMap);
})
.catch((err) => {
reject(err);
});
}
);
}
from examples.
@kvreem Each individual query to Lambda needs to have the database connection open then closed.
The following code should the trick
db.once('open' , () => {
return handler(event, context, callbackFilter);
})
from examples.
@kuashe sorry to keep bugging you, here is my refactored handler, but i still get the same result in my logs when i deploy to lambda.
exports.graphqlHandler = function graphqlHandler(event, context, callback) {
context.callbackWaitsForEmptyEventLoop = false;
function callbackFilter(error, output) {
if (!output.headers) {
output.headers = {};
}
// eslint-disable-next-line no-param-reassign
output.headers['Access-Control-Allow-Origin'] = '*';
output.headers['Access-Control-Allow-Credentials'] = true;
output.headers['Content-Type'] = 'application/json';
callback(error, output);
}
mongoose.connect(mongoString, {useMongoClient: true});
const db = mongoose.connection;
const handler = server.graphqlLambda({ schema: myGraphQLSchema });
db.on("error", (err) => {
console.log("MongoDB connection error. Please make sure MongoDb is running.", err);
process.exit();
});
db.once('open', () => {
return handler(event, context, callbackFilter);
})
};
I am trying to understand how I can keep the Lambda function from timing out, based on the logs it never connects to the database. When I deploy I do get a CORS issue,
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://MY DOMAIN' is therefore not allowed access. The response had HTTP status code 502. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
But this shouldn't stop the database from connecting. Or is this whats causing the issue? I have the headers in my handler.
In my serverless.yml here is how I am defining my lambda functions. (Same as you are)
functions:
graphql:
handler: handler.graphqlHandler
events:
- http:
path: graphql
method: post
cors: true
...
Again I appreciate all your help. Thanks a ton 👍
from examples.
I am trying to connect to mlab from aws lambda, and am not keen to use serverless. Can someone please help me out? My query is posted in detail @ stackoverflow: https://stackoverflow.com/questions/50559243/not-being-able-to-connect-to-mongodb-from-aws-lambda
from examples.
Related Issues (20)
- Value not found at "self" source HOT 8
- Serverless framework with Node ES6 modules HOT 1
- What's the difference between two examples
- Do i have to comment these three lines before i deploy to aws? HOT 1
- aws-python-flask-api - Internal Server Error
- [New example request] AWS Python FastAPI
- [Updated Example] DynamoDB + Typescript + Node v18 HOT 1
- [aws-node-puppeteer] Example missing information HOT 1
- Unsupported template aws-node-http-api-typescript HOT 3
- [aws-node-puppeteer] supports only the Node.js 6.10 or 8.10 runtimes HOT 1
- Trying to invoke the hello function locally for template aws-node-http-api fails using documentation HOT 1
- Wrong serverless-http dependency version in the basic express template HOT 1
- Error running npm start - aws-node-typescript-nest
- The Serverless version (1.26.1) does not satisfy the "frameworkVersion" (3) in serverless.yml
- All AWS Golang examples need to be updated
- Error: The "serverless-plugin-chrome" plugin only supports the Node.js 6.10 or 8.10 runtimes. Your service is using the "nodejs18.x" provider. HOT 6
- serverless-dynamodb-local examples need updating
- Unable to Retrieve Bot Response: ERR_NAME_NOT_RESOLVED and 403 Forbidden Error in Flask Application deployment on AWS
- Curious about the Polyfills issue
- aws-node-mongodb-atlas code outdated HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from examples.