简体   繁体   中英

How to run nextjs in AWS lambda with `experimental-edge` runtime

I'm trying to find a way to run Next.js (v13.0.6) with OG image generation logic (using @vercel/og ) in AWS Lambda

Everything works fine locally (in dev and prod mode) but when I try execute lambda handler getting "statusCode": 500, It only fails for apis that involve ImageResponse (and runtime: 'experimental-edge' as a requirement for @vercel/og )

I'm pretty sure the problem is caused by Edge Runtime is not being configured correctly

There is my handler code

  1. next build with next.config.js output: 'standalone' creates folder .next/standalone

  2. insde standalone handler.js

const { parse } = require('url');
const NextServer = require('next/dist/server/next-server').default
const serverless = require('serverless-http');
const path = require('path');
process.env.NODE_ENV = 'production'
process.chdir(__dirname)

const currentPort = parseInt(process.env.PORT, 10) || 3000

const nextServer = new NextServer({
  hostname: 'localhost',
  port: currentPort,
  dir: path.join(__dirname),
  dev: false,
  customServer: false,
  conf: {...} // copied from `server.js` in there same folder
});


const requestHandler = nextServer.getRequestHandler();

// this is a AWS lambda handler that converts lambda event
// to http request that next server can process
const handler = serverless(async (req, res) => {
  // const parsedUrl = parse(req.url, true);
  try {
    await requestHandler(req, res);
  }catch(err){
    console.error(err);
    res.statusCode = 500
    res.end('internal server error')
  }
});

module.exports = {
  handler
}

testing it locally with local-lambda , but getting similar results when test against AWS deployed lambda

what is confusing is that server.js (in.next/standalone) has a similar setup, it only involves http server on top of of it

update:

aws lambda logs show ERROR [Error [CompileError]: WebAssembly.compile(): Compiling function #64 failed: invalid value type 'Simd128', enable with --experimental-wasm-simd @+3457 ]

update 2:

the first error was fixed by selecting Node 16 for AWS lambda, now getting this error

{
  "errorType": "Error",
  "errorMessage": "write after end",
  "trace": [
    "Error [ERR_STREAM_WRITE_AFTER_END]: write after end",
    "    at new NodeError (node:internal/errors:372:5)",
    "    at ServerlessResponse.end (node:_http_outgoing:846:15)",
    "    at ServerlessResponse.end (/var/task/node_modules/next/dist/compiled/compression/index.js:22:783)",
    "    at NodeNextResponse.send (/var/task/node_modules/next/dist/server/base-http/node.js:93:19)",
    "    at NextNodeServer.handleRequest (/var/task/node_modules/next/dist/server/base-server.js:332:47)",
    "    at processTicksAndRejections (node:internal/process/task_queues:96:5)",
    "    at async /var/task/index.js:34:5"
  ]
}

At the moment of writing Vercel's runtime: 'experimental-edge' seems to be unstable (run into multiple issues with it)

I ended up recreating @vercel/og lib without wasm and next.js dependencies, can be found here

and simply use it in AWS lambda. It depends on @resvg/resvg-js instead of wasm version, which uses binaries, so there should not be much perf loss comparing to wasm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM