Overview
The Terraform Next.js module for AWS is a zero-config Terraform module designed for self-hosting Next.js sites serverless on AWS Lambda. This module supports various features like unlimited parallel deployments of Next.js apps, support for different types of pages (static, SSG, Lambda, and API pages), automatic expiration of old static assets, rewrites & redirects, and image optimization support.
Features
- Supports any version of Next.js
- Terraform v0.15+
- Unlimited parallel deployments of Next.js apps (atomic deployments)
- Static, SSG, Lambda, and API pages (with dynamic routes)
- Automatic expiration of old static assets
- Rewrites & Redirects
- Image Component & Image Optimization support
- Incremental Static Regeneration (under development)
- Middleware (Not supported by Lambda@Edge / CloudFront functions) (planned)
Installation
- Prerequisites: Make sure you have Terraform, Node.js, Bash, and curl installed, and a public Route53 Hosted Zone associated with your AWS account.
- Setup the Next.js Terraform module:
- Create a new
main.tf
file in an empty folder or existing Terraform stack. - Run commands to create resources in your AWS account.
- Utilize
api_endpoint
for creating new deployments. - Use
api_endpoint_access_policy_arn
to create new users with restricted access.
- Create a new
- Deploy a Next.js App:
- Install the CLI tool
tf-next
. - Build the Next.js app using
tf-next build
. - Deploy the app with
tf-next deploy
.
- Install the CLI tool
Summary
The Terraform Next.js module for AWS provides a convenient way to host Next.js sites on AWS Lambda with various features like support for different page types, automatic asset expiration, and image optimization. By following the installation guide, users can easily set up the Terraform stack and deploy their Next.js apps using the CLI tool provided. This module simplifies the process of managing and hosting Next.js applications on AWS.