Support AWS Lambda as target serverless
See original GitHub issueHey š
Thank you a lot @timneutkens for great #5927 PR, amazing work š„
I was going through the code of your PR in order to make it work with AWS Lambda and it was pretty straightforward.
I had to āhackā your next-serverless-loader.ts
function in order to be able to inject serverless-http
module that way:
// next.config.js
const nextServerlessLoader = require("next/dist/build/webpack/loaders/next-serverless-loader");
const { stripIndent } = require("common-tags");
const originalLoader = Object.assign(
{},
{ default: nextServerlessLoader.default }
);
nextServerlessLoader.default = function() {
const transformed = originalLoader.default
.call(this)
.replace(`export async function render`, `async function nextRender`);
const result = stripIndent`
import serverlessHttp from 'serverless-http';${transformed}
export const render = serverlessHttp(nextRender);
`;
return result;
};
module.exports = {
target: "serverless"
}
This is working great, the output can be directly plugged to an handler š
functions:
index:
handler: .next/serverless/pages/index.render
events:
- http: GET /
Here are my questions:
- Are you willing to support other platforms than now.sh?
- If yes could we expose an option to auto-import the
serverless-http
module? - If not, should I write a custom plugin to nextjs?
Cheers āļø
Issue Analytics
- State:
- Created 5 years ago
- Reactions:8
- Comments:26 (11 by maintainers)
Top Results From Across the Web
Lambda functions as targets - Elastic Load Balancing
You can register your Lambda functions as targets and configure a listener rule to forward requests to the target group for your Lambda...
Read more >AWS Lambda Events - ALB - Serverless Framework
The Serverless Framework makes it possible to setup the connection between Application Load Balancers and Lambda functions with the help of the alb...
Read more >Configuring an AWS Lambda Target on an Application Load ...
One type of target that is capable of serving HTTP requests to an application load balancer is an AWS Lambda function. This allows...
Read more >Deploy Serverless Applications with AWS Lambda and API ...
Deploy a serverless web application on AWS with Lambda and API Gateway. Package and deploy a Lambda function to S3, configure an IAM...
Read more >Serverless SSR with React 18 and NextJS 12 in AWS
Unfortunately AWS Lambda does not support Web Streams (Deno does) ... There used to be a 'serverless' build target which bundled NextJS intoĀ ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Iāve been working with Next on AWS Lambda for more than a year, and the lack of official example/support is painful indeed.
Itās really not easy to properly setup next on AWS, and itās even harder to have a local/dev env that properly simulates AWS Lambda.
Also, AWS Lambda is -by far- the most used (compared to GCP, Azure, and others), and it would really make sense for the Next.js community to target this system, as it is one of the cheapest yet most reliable out there. Also, it fits particularly well with the Next 8+ āserverlessā approach, but needs guidance to do it right.
The most advanced way Iāve seen so far is https://github.com/danielcondemarin/serverless-nextjs-plugin. I had made my own AWS Lambda + Next 5 + Express back then https://github.com/Vadorequest/serverless-with-next and Iām still running this in production (and looking for an up-to-date replacement, having a hard time to upgrade myself, see https://github.com/zeit/next.js/issues/7823)
There have been numerous open source attempts to make AWS Lambda work with Next over the past year, I canāt count how many work-in-progress repositories Iāve come across, such as https://github.com/justinwhall/nextjs-serverless-aws
The recent Next 9 update brings a lot of much welcomed capabilities/possibilities that are currently in discussion, see https://github.com/danielcondemarin/serverless-nextjs-plugin/issues/101 and https://github.com/danielcondemarin/serverless-nextjs-plugin/issues/105.
So far, Iāve seen two very different ways to go āserverlessā with Next:
Honestly, I donāt know whatās the best approach here. Both have their pros/cons. I like the express way, because it makes it so much easier to use middlewares and itās a huge time saver. I havenāt seen anything similar to middlewares using the āserverlessā way so far. Itās much heavier, but also easier to warmup. And I donāt know how much faster does the āserverlessā way is for cold starts. Are they really so insignificant that we can ignore them? Not quite sure about that. What I know is that if we need to warmup in āserverlessā mode, itāll be much more difficult/expensive to do so.
Also, I donāt know how many existing apps are out there using the āexpressā way, and itās gonna be hard to migrate apps from āexpressā to āserverlessā, as we must find a proper replacement for all middlewares, and other things.
So, the point of all this talking is to clarify that there are -at least- 2 ways to use Next.js on AWS, and it hasnāt been proved yet that one way is ābetterā than the other. Also, there is a lot of efforts made by the community to make Next and AWS Lambda play together, but very few successful ones due to the complexity around those.
Itād very much be a game changer if the Next.js core team would tackle this issue and provide a reliable way of making Next working on AWS Lambda.
Hi š Iāve been working on a plugin (https://github.com/danielcondemarin/serverless-nextjs-plugin) for the serverless framework that targets nextjs 8 serverless mode. It adds the compat layer between the AWS Lambdas and the Nextjs page handlers at build time and takes care of uploading the static assets to S3. Seems promising so far, but is still a WIP and havenāt been able to test it in anger. Is this something the folks in this thread using the serverless framework would be interested at? Would be good to get some feedback.