Why we self-host our serverless Next.js site on AWS with Terraform

Image for post
Image for post

Becoming independent

A diagram of how AWS services must be connected to serve a serverless Next.js application
A diagram of how AWS services must be connected to serve a serverless Next.js application
The architecture of our serverless Next.js deployment

Understanding the Next.js routing

Preparing Next.js for a serverless environment

Image for post
Image for post
The flow of a request to a serverless Next.js site

Deploy Next.js to AWS

Prerequisites

Build the Next.js app

npx tf-next build

Deploy the Next.js app

# Make the AWS credentials available for Terraform
export AWS_ACCESS_KEY_ID=<your-access-key>
export AWS_SECRET_ACCESS_KEY=<your-secret-access-key>
# Initialize Terraform
terraform init
# Deploy your app
terraform apply
> Apply complete!
>
> Outputs:
>
> cloudfront_domain_name = "xxx.cloudfront.net"

House of Internet. https://felix.house

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store