-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Access event and callback #5
Comments
Hi @tzehe // server code here..
// create the nextServerless handler
const nextServerlessHandler = nextServerless(app, requestHandler, () => {
// create a normal http node server for local usage
createServer(requestHandler).listen(3000, (err) => {
if (err) throw err;
console.log('> Ready on http://localhost:3000');
});
});
// export the nextServerless function
module.exports.handler = function(event, context, callback) {
/** Immediate response for WarmUP plugin */
if (event.source === 'serverless-plugin-warmup') {
console.log('WarmUP - Lambda is warm!')
return callback(null, 'Lambda is warm!')
}
// call the normal logic otherwise
return nextServerlessHandler(event, context, callback);
} If you do not use a custom server, you may be able to import the default logic like this: const { handler as nextServerless } = require('next-serverless/lib/next/server');
// ...
module.exports.lambdaToWarm = function(event, context, callback) {
/** Immediate response for WarmUP plugin */
if (event.source === 'serverless-plugin-warmup') {
console.log('WarmUP - Lambda is warm!')
return callback(null, 'Lambda is warm!')
}
return nextServerless(event, context, callback);
} I didn't test those codes, but I think it can work. Please tell me when it doesn't, I'll then try to debug it and release a patch to be able to hook into the event. Anyway, when we found a solution, I'll add an example to the readme. I didn't yet think about this use-case, so thank you for sharing :) |
I just noticed an error and updated the code above |
Hey @cyrilwanner thank you for your detailed response. I am using a custom server and will try your approach. I will get back to you afterwards. Thank you! |
Hey @cyrilwanner I implemented your suggested solution and it worked perfectly for me. I checked my cloudwatch logs and it shows the log messages if the lambda is warm. So thank you for your help an I am glad to help you with this use case. Would be beneficial to add this to the documentation. |
Perfect, thank you! |
Hi,
first of all thank you for this amazing project! I am using to deploy a small next.js app to lambda in production.
I want to use
serverless-plugin-warmup
to prevent cold starts in production. To reduce costs as stated here (https://serverless.com/blog/keep-your-lambdas-warm/) I try to add this logic to my lambda function:I noticed that the logic is wrapped around in handler.js. Is there a simple solution to achieve this.
Thanks in advance!
The text was updated successfully, but these errors were encountered: