Sample Next.js application configured for AWS Lambda deployment.
This project demonstrates how to build and deploy a Next.js application to AWS Lambda. It includes:
- Server-side rendering (SSR)
- API Routes
- Standalone output mode for Lambda compatibility
- Serverless Framework configuration
- Node.js 18.x or later
- AWS account
- AWS CLI configured with credentials
- Serverless Framework (optional, for deployment)
npm installRun the development server:
npm run devOpen http://localhost:3000 with your browser to see the application.
Build the application for production:
npm run buildThe standalone output will be generated in .next/standalone/.
nextjs-lambda/
├── src/
│ └── app/
│ ├── api/
│ │ └── hello/
│ │ └── route.ts # Example API route
│ ├── layout.tsx # Root layout
│ └── page.tsx # Home page
├── next.config.js # Next.js configuration
├── package.json
├── serverless.yml # Example Serverless config (requires adapter)
├── template.yaml # Example AWS SAM config (requires adapter)
└── tsconfig.json
Note: The included serverless.yml and template.yaml files are example configurations. Next.js doesn't generate Lambda-compatible handlers out of the box. For production deployments, you'll need to use a Lambda adapter solution.
OpenNext is the recommended solution for deploying Next.js to AWS Lambda:
npm install -g open-next
open-next buildThen deploy using AWS CDK, Terraform, or SST.
- Install required dependencies:
npm install -g serverless
npm install serverless-http- Create a custom handler to wrap Next.js
- Deploy using
serverless deploy
- Build the Next.js application:
npm run build- Add a Lambda Web Adapter or custom handler
- Deploy using AWS SAM CLI
- Standalone Output: Configured with
output: 'standalone'innext.config.jsfor Lambda compatibility - API Routes: Example API endpoint at
/api/hello - TypeScript: Full TypeScript support
- SSR: Server-side rendering enabled
The next.config.js file is configured for Lambda deployment:
module.exports = {
reactStrictMode: true,
output: 'standalone',
}Create a .env.local file for local development:
NODE_ENV=development
GET /api/hello- Returns a JSON response with a greeting message
MIT