Blog>
Snippets

Deploying Next.js 14 to AWS using Serverless Framework

Showcase how to deploy a serverless Next.js 14 application to AWS Lambda using the Serverless Framework.
const { withServerlessTrace } = require('@vercel/next-serverless/trace');

module.exports = withServerlessTrace({
  // Your Next.js config.
});
This piece of code modifies your Next.js configuration to include serverless tracing, which is necessary for deploying Next.js apps with the Serverless Framework.
const serverless = require('serverless-http');
const next = require('next');

const app = next({
  dev: false
});
const handle = app.getRequestHandler();

app.prepare().then(() => {
  module.exports.handler = serverless(async (req, res) => {
    // The page rendering is happening here.
    return handle(req, res);
  });
});
This is the serverless handler for our Next.js app. It uses the serverless-http package to create an AWS Lambda compatible version of our Next.js application.
service: nextjs-app

provider:
  name: aws
  runtime: nodejs14.x
  lambdaHashingVersion: 20201221
  region: us-east-1

functions:
  nextApp:
    handler: handler.handler
    events:
      - http:
          path: /{proxy+}
          method: any

plugins:
  - '@vercel/next-serverless/trace'
This is the `serverless.yml` configuration file for the Serverless Framework. It sets up the AWS provider, specifies the runtime to match your Next.js deployment, and defines the function that will run your Next.js app with the HTTP trigger set up to pass all paths to the handler.