Zero-Config Terraform Module to deploy Next.js Apps on AWS using Serverless solutions
- Node: 16+
- Terraform: 1.6.3+
- bash
- zip
Add the following dependencies & script to your package.json file
package.json
{
"scripts": {
"build-serverless-next": "build-serverless-next",
...
},
"dependencies": {
"build-serverless-next": "latest",
"next": "^13",
...
},
...
}
Add the output: "standalone"
option to the next.config.js file
next.config.js
const nextConfig = {
...
"output": "standalone",
...
}
module.exports = nextConfig
Check it on Terraform Registry for more details.
Ensure that the deployment name is unique since its used for creating s3 buckets.
main.tf
provider "aws" {
region = "eu-central-1" #customize your region
}
provider "aws" {
alias = "global_region"
region = "us-east-1" #must be us-east-1
}
module "next_serverless" {
source = "Nexode-Consulting/nextjs-serverless/aws"
deployment_name = "nextjs-serverless" #needs to be unique since it will create s3 buckets
region = "eu-central-1" #customize your region
base_dir = "./" #The base directory of the next.js app
}
output "next_serverless" {
value = module.next_serverless
}
Build the Next.js Code and deploy
npm i build-serverless-next
npm run build-serverless-next
terraform init
terraform apply
- Next.js v13 Complete example with SSR, API, static pages, image optimization & custom domain
- The
build-serverless-next
package's version must match thenext_serverless
module's version - The
app/
folder must be in the root directory (ex. not in thesrc/
directory) - When destroying the
next_serverless
module, Lambda@Edge function need at least 15mins to be destroy, since they're replicated functions - Using the
<img>
tag orbackground-image
css property will face issues rendering images, please use the build-in<Image />
component.
Feel free to improve this module.
Our contributing guidelines will help you get started.
This module is maintained by Nexode Consulting.
We help companies develop and operate enterprise-grade software with startup momentum.
Apache-2.0 - see LICENSE for details.