Deploying Next.js to AWS Lambda needs a layer of abstraction because Next.js is designed to work as a Node.js server in the first place.
Vercel does this with a thing called now-node-bridge
which basically starts a local Node.js http-server inside the Lambda function and then create a request from the Lambda invoke event.
Here is a simplified flow chart what happens when you run a request against a Next.js app that is deployed on Vercel:

While Next.js is Open Source the proxy that does the conversion from HTTP Requests to Lambda Events at Vercel is not. That is the reason why a serverless deployment to AWS is still a tricky task while Vercel internally also uses AWS Lambda for their deployments.
Deployment to AWS Lambda
While the Serverless framework still might be the most popular choice at the moment, I created a simple to use Terraform module for this task.
Terraform is an Open Source CLI application for managing Cloud-Resources in providers like AWS.
With Terraform installed it is as simple as this:
Go to your Next.js app and install this helper:
npm i -D tf-next # npm or
yarn add -D tf-next # yarn
Add the following script tf-next
to your package.json
of your Next.js app:
{
...
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start",
+ "tf-next": "tf-next build"
}
}
...
Create a new main.tf
with the following content right next to your package.json
:
# main.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.0"
}
}
}
# Main region where the resources should be created in
provider "aws" {
region = "us-east-1"
}
module "tf_next" {
source = "dealmore/next-js/aws"
}
Create an AWS Access Key and expose it in the terminal:
export AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE
export AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
Build the app and deploy it to AWS with terraform:
terraform init # Only needed on the first time running Terraform
yarn tf-next # Build the next.js app
terraform plan # See what resources Terraform will create
terraform apply # Deploy the App to your AWS account
You can change the region in the main.tf
to any AWS region that supports Lambdas and the region that you define there is the region where your Lambdas are deployed to.
For more information please see the GitHub Page: AWS Next.js Terraform module on GitHub
npm run dev
are you able to see your application working? 2nd question: when you say "Above library is not working like doc" what you mean? That you get some error from console or other kind of behaviour?region
problem you could try to runserverless
deploy command with--stage
command line option where you specify which aws profile you want to use. Inside that profile you may try to set the region and see if works.