Cheapest way to deploy a React app using NextJS SSR on AWS?
NextJS + Serverless is not Free.
NextJS + Serverless is currently deployed on Lambda Edge which is not free. You do not enjoy the free tier from Lambda (not Lambda@Edge).
Vercel + NextJs is Free
If you have a low traffic website I would recommend you to deploy it with Vercel.com which uses Lambda (AWS Network) on the backend.
*Their hobby version is free and gives generous free traffic and invocation comparable to AWS Lambda Free tier.
Deployment of a NextJS App is as easy as uploading to Github + Vercel's auto deployment with GitHub integration. You do not need to bother about S3, or hosting or your static files, everything gets hosted on Vercel the moment you push to Github. You just need to concentrate on development.
When your your requirement goes up (you go past the Hobby package, and you go past the Pro package), then it becomes more cost effective deploying on Serverless@Edge.
By then, all you need to do is to switch your domain over.
Update: Just deploy it on a VPS
Serverless is a nice concept, and the ability to launch your websites for free on various platform is a plus.
However, cold start can be a big problem as it sometimes take 3-4 seconds to load a page for your visitor.
This is not so much of a problem if you are doing static OR static incremental generation. It's just NO GOOD for getServerSideProps.
If you are struggling for cold starts, trust me and move on to a VPS. A $5 VPS can run a site pretty well.
Next.js SSR + Serverless framework + AWS Lambda
For deploying your Next.js SSR App, instead of following a traditional approach of managing and running a whole AWS EC2 instance which keeps running 24x7. There is actually one more cost effective and modern approach which uses AWS lambda and Serverless framework.
Q. What is AWS lambda?
AWS Lambda lets you run code without provisioning or managing servers. You pay only for the compute time you consume.
Q. What is Serverless framework?
Serverless Framework Open Source lets you develop applications with serverless architectures and deploy using AWS Lambda, Azure Functions, Google CloudFunctions & more.
Q. What is Serverless-Next.js?
This is a Serverless component built just for deploying Next.js app. Also any of your assets in the static or public folders are uploaded to S3 and served from CloudFront automatically, so I think this is what you are exactly looking for.
Below is the architecture explaining how it serves your app to the user.
If you are new to serverless framework, I will suggest you to go thru a free course by Serverless community called Serverless for Frontend Developers
EDIT: 03/03/2021
@super7egazi put forward a genuine concern in the comment below. Thankfully there are few ways to keep the Lambda function warm. This is the act of sending scheduled ping events to your functions to keep them alive and idle, ready to serve requests.
You will find various methods and plugins to achieve this, if you just search "How to keep lambda functions warm?" on google.
Below are some links which I'm attaching for reference.
How to keep your lambda functions warm?
How to Minimize AWS Lambda Cold Starts
Keep your lambdas warm
AWS Next.js Terraform module
We created an Open Source Terraform module as a lower cost alternative to the Serverless framework for this use case. Instead of relying only on Lambda@Edge for all SSR operations we use Lambda@Edge only for routing (as some kind of reverse proxy) and then redirect the request internally via API Gateway to an regional Lambda.
Since we use CloudFront as a reverse proxy we can also split most of the requests for static files against _next/static/*
for css, js, etc. and serve them directly by S3 without touching the Lambda@Edge proxy at all.
So the cost per request are different per route:
Requests for static assets: css, js, images
Only costs for CloudFront and S3 (For misses from CloudFront) apply
Requests for HTML: Prerendered HTML-Routes or Routes that need Server-Side-Rendering (SSR)
Costs for Cloudfront, Lambda@Edge (Proxy, metered at 1ms steps) apply.
Rewrite routes that serve pre-rendered HTML
Costs for S3 apply.
Routes that use Server-Side-Rendering (SSR)
Costs for HTTP API-Gateway and regional Lambda (SSR, metered in 1ms steps) apply.
The total costs are usually far below 0.50$ / month for a few thousand requests with this model, while having a fast-serving site powered by CloudFront edge caching.
Find more information on the GitHub repo: https://github.com/dealmore/terraform-aws-next-js
You can use AWS Lightsail: https://aws.amazon.com/lightsail/
In my experience with nextjs
the cloud functions are not a good place to deploy a large application, so, this would be your options:
- Only go with cloud functions if your app is super small and is never gonna grow. Cloud function have limitations on file size, dependencies, bundle size, etc, etc, etc
- AWS Lightsail: https://aws.amazon.com/lightsail/ and a small VPS that you can set up your self with Nginx and node. Is like $5 / month and you can use your credits.
- Same as numbers 1, but on Digital Ocean.
- AWS Ec2