~/blog/nextjs-on-aws
Published on

Next.JS on AWS

446 words3 min read–––
Views
Authors
Next.js

If you want to host your application in the AWS, there are some ways to accomplish it. If you are using Next.js 12 or newer version, it is not hard to deploy it, since Amplify hosting supports management of apps with Next.js 12 or later. It does not mean you can not deploy your application on AWS which was developed by older versions, since AWS has a lot of services, you can run your application at least in one service.

For Next.js 12 or new versions you can use this link to deploy your application.

There are 3 ways to deploy your Next.js application. One of them is using EC2 services, which likes the old traditional way, you rent server from AWS and build all infrastructure by yourself. For it you have to install NGINX, Node JS language support and so on. The second way is to use ECS which is most popular nowadays, there might be some issues related to containerization process, but it highly abstracts application logic and if you use ECS with Fargate it will be more cheaper than the first approach. Fargate helps to automatically scaling up and down our server speed. You pay for what you use, it just charges for how much time you use. In EC2 you have to pay whether you use its full power or not. There is a very helpful tool called AWS Copilot. Before using copilot just make sure you successfully build your app image in docker without problems. The third way is the easiest way but it has some limitations. Since Next.js is derived from React.js it can build the code as static files as React does. It will not work if you are using custom backend services which can be build in Next.js pages/api folder. It also breaks authorization logic of your application. If you used Next.js optimized Image component, it will not work for static generation, for this one you should use other cloud service to return image with the requested width and height. Benefits of static files is AWS Cloudfront service, which provides fast CDN caching. In AWS some services' cost and latency depends on region, in Cloudfront you do not have to worry about it. Routing is the biggest problem when you deploy it in AWS, since Next.js generates links without file suffixes your S3-bucket can not route your request. However, there is edge functions which helps to write interceptor function to change or proxy your request to desired paths.